Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I'm stuck.. how to do this in a loop or otherwise? 3

Status
Not open for further replies.

RFC1795

IS-IT--Management
Feb 13, 2003
76
US
Hi all,

I've hit a blank and am not sure how to get around this. (Let alone explain it properly ;-) … I’ll try my best)
I tried searching the forums but found nothing that seems to match what I’m wanting to do.

It’s like this. I have a bunch of directories which I want to ls -lt on and awk the 9th column into a file. This will leave me with a column list which I need to use as a source for another script.
Eg:
ls -lt | awk ‘{print $9}’ > dirlist.txt

dirlist.txt will contain only the directory names then, like so:
directory1
directory2
directory3
directory4
directory5
directory6
directory7
directory8
directory9
directory10

Now I have another file which lists a number of these directory names which I’d like to have excluded or preferably marked with a hash.
Eg:
So the excludefile.txt will contain
directory6
directory8
directory10

So ideally what I’d like to have is the dirlist.txt (or another if a new one needs to be created) looking something like this:

directory1
directory2
directory3
directory4
directory5
# directory6
directory7
# directory8
directory9
# directory10

I’ve tried doing the above with a for loop and sed but I think I’m heading down the wrong route and complicating things.

Anyone have any ideas I should consider or a simple way to do the above?

Thanks … T
 
something like this ?
awk 'NR==FNR{e[$1];next}{print (($1 in e) ? "# " : "") $1}' excludefile.txt dirlist.txt > newlist.txt

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi,

Thanks for the prompt replies.

I had a few problems getting it to work, but then tried nawk. Instant results!! Thank you both kindly... :)

Small modifications to get Feherke's one to do similar to PHV's ... you're both too clever :-D

BTW, this was with awk running on SunOS

Cheers... T
 
Hi

Oops. You answered my unasked question. In my code I used $8, because my poor [tt]ls[/tt] outputed only eight columns. Then, as usally, I forgot to mention it.

Feherke.
 
ls -lt|awk '{print $9}'|grep -f excludefile.txt -v > dirlist.txt

outputs the items not in the excluded list, just to add an answer for the unpreferred solution :)


______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Thanks KarveR ... that's also pretty clever. Nice and clean too. I'll definitely take that one home and freeze it :)

Cheers... T
 
I figured I'd work this one out before checking the other posted solutions. I know BASH better than AWK so i'ts quite a bit longer :). Not sure why you are wanting 'ls -lt' as that lists files as well as directories, plus more columns than just the name.

Code:
#!/bin/bash
dirPath=${1:-~}
allDirs=`ls -F $dirPath | grep / | sed "s./.."`
newDirs=directoryList.txt
excludeList=`cat excludeFile.txt`
mark=0
for dir in $allDirs; do
        for X in $excludeList; do
                if [ "$dir" == "$X" ]; then
                        mark=1
                fi
        done

        if [ $mark == 1 ]; then
                echo "# $dir" >> $newDirs
        elif [ $mark == 0 ]; then
                echo "$dir" >> $newDirs
        fi

        mark=0
done
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top