1753778 Members
7503 Online
108799 Solutions
New Discussion юеВ

Re: awk command

 
Thi Vu
Frequent Advisor

awk command

Hello everyone,

Can someone tell me where this command go wrong:

I have 2 files:

file1: looks like this:

cfxfund/custom/AGUPDAT1.P
rtb_dbup/
rtb_dbup/21862188/
rtb_dbup/21862188/schctrl
rtb_idat/
rtb_idat/compath
rtb_idat/objctrl
rtb_idat/delctrl
rtb_idat/selcomp
rtb_inst/
rtb_inst/v7begin.i
rtb_inst/v7end.i
rtb_inst/objcomp.p
rtb_inst/schupd.p
rtb_inst/schupdp2.p

and in file2, I have almost the same:

cfxfund/custom/ESGACCTINFO.P
cfxfund/custom/ESGCONVFUNC.I
cfxfund/custom/RESPTRS0.P
cfxfund/custom/SAXSIN00.P
cfxfund/custom/SAXSIN01.I
rtb_dbup/
rtb_dbup/21862187/
rtb_dbup/21862187/schctrl
rtb_idat/
rtb_idat/compath
rtb_idat/objctrl
rtb_idat/delctrl
rtb_idat/selcomp
rtb_inst/
rtb_inst/v7begin.i
rtb_inst/v7end.i
rtb_inst/objcomp.p
rtb_inst/schupd.p
rtb_inst/schupdp2.p

Now, when I run this awk command:

cat filename | awk '{n=split($0,p,"/");file=p[n]; if(match(file,"[aA-zZ]*\.[aA-zZ]+")) {print
file}}' > resultfilename

I got 2 different results:

result1 (from file1):
AGUPDAT1.P
v7begin.i
v7end.i
objcomp.p
schupd.p
schupdp2.p


and result2 (from file2):

ESGACCTINFO.P
ESGCONVFUNC.I
RESPTRS0.P
SAXSIN00.P
SAXSIN01.I

I had run this command 2 or 3 times for each file and the result are the same for each file. Can someone point out why or how I can have different results when both files are almost identical? Any help is appreciate. Thanks

Thi

5 REPLIES 5
Hein van den Heuvel
Honored Contributor

Re: awk command

So if I get this right, you are trying to print names of files that look like "word dot word".
And the difference in results you spot it that lack of v7begin.i in the second output right?

Well, cutting and pasting your data from the forum and executing the command _does_ those those also for the second group.

So I speculate that you have spaces or such in the files confusing the data? Try cut & paste yourself? If you still have trouble, please ATTACH a file [Browse...] with the exact input files for testing.

btw... I believe the following command will alsso do what you want:

awk -F/ '/\.[aA-zZ]+$/{ print $NF}' < filename > resultfilename

hth,
Hein.
Nicolas Dumeige
Esteemed Contributor

Re: awk command

I'm guessing you're trying to get the basename of evry line.

To do so, I suggest you use sed :

sed 's+.*/++' $FILENAME | grep -v '^$'

On my box, your awk script do just fine if I use nawk

All different, all Unix
Mark Greene_1
Honored Contributor

Re: awk command

If you are looking to extract just the file names, there is a command "basename" that will do this. So you could do something like this:

for LINE in `cat file1`; do
basename $LINE
done

and redirect the output from the 2nd line to a file if you wanted.

There's also a corresponding command, dirname, that returns the directory path.

mark
the future will be a lot like now, only later
Kent Ostby
Honored Contributor

Re: awk command

Interesting .. when I put the awk command into a file and use awk -f, I get :

rc:temp/script6 $ awk -f useme.awk < file1
AGUPDAT1.P
v7begin.i
v7end.i
objcomp.p
schupd.p
schupdp2.p

awk -f useme.awk < file2

ESGACCTINFO.P
ESGCONVFUNC.I
RESPTRS0.P
SAXSIN00.P
SAXSIN01.I
v7begin.i
v7end.i
objcomp.p
schupd.p
schupdp2.p

That's with /usr/bin/awk.

/usr/bin/awk:
$Revision: 78.14.1.16 $
PATCH_10_20: awk.g.o awk.lx.o b.o main.o tran.o lib.o run.o parse.o pro
ctab.o hpux_rel.o 99/12/17

"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Thi Vu
Frequent Advisor

Re: awk command

Sorry, I want the result2.

Hein, thanks for the tips. I got the problem solve - it was the space issue. Thank you everyone for the quick reply.

Thi