1833876 Members
1874 Online
110063 Solutions
New Discussion

Re: awk parsing

 
SOLVED
Go to solution
Thi Vu
Frequent Advisor

awk parsing

Hi all,

I got a list of file like this:

mcdbq/ABCDGEF.P
crtbn/GIFGEO.I
djgei/DKJFEO.VAR
rtbkdjf/fjiefj.p
rgmien/fjeijg.i
rteji/fjeij.d

The .P can be whatever as long as it's in capital. I am trying to get a listing of the files in capital only using this command:

tar -tvf filename.tar |awk -v file="[A-Z]*\.[A-Z]*$" '{if($8 ~ file)print $8'

It prints all the files (including the non-capital file); but if I do this:

tar -tvf filename.tar |awk -v file="[A-Z]*\.P*$" '{if($8 ~ file)print $8'

it only prints the .P file, which is not all the file I want

I even use this command:

tar -tvf filename.tar |awk -FS="/" -v file="[A-Z]*\.[A-Z]*$" '{if($8 ~ file)print $8'

and there is no output at all. Where did I got wrong? Any help is appreciate.

Thi
8 REPLIES 8
RAC_1
Honored Contributor

Re: awk parsing

Not AWK, but simple grep.

tar -tvf filename.tar | grep -e "[A-Z]\$"

Anil
There is no substitute to HARDWORK
Hein van den Heuvel
Honored Contributor
Solution

Re: awk parsing


Here is one way:

awk '{n=split($0,p,"/");file=p[n]; if (file == toupper(file)) { print file}}' your-list-file

split line into parts based on "/"
filename it the last part ("n")
print if filename equals uppercased filename.

Hein.


curt larson_1
Honored Contributor

Re: awk parsing

give this a try

tar -tvf filename.tar |awk -v file="\.[A-Z]+$" '{if($8 ~ file)print $8'

use a + instead of *
* matches zero or more, as in it doesn't have to be there
+ matches one or more, there has to be at least one.
Thi Vu
Frequent Advisor

Re: awk parsing

Hi Anil and Curt,

Your commands show everything (small letter files and capital letter files)

Hein,

Your command work, however thinking further down at the script I would like to know how do I go about if I want this:

I have: mcstm/ABDEGD.P

I would like to assign file=ADBEGD.P and
dir=mcstm where I will call the variable file and dir several times somewhere else in the script. Thanks

Thi
RAC_1
Honored Contributor

Re: awk parsing

tar -tvf filename.tar | grep -e "[A-Z]\$"|awk -F "/" '{print $1, $2}'

Anil
There is no substitute to HARDWORK
Rodney Hills
Honored Contributor

Re: awk parsing

The reason it is not doing what you want is the way "\" is being intrepetted by the shell and awk.

Make the following change (double quotes to single and an extra "\")-

tar -tvf filename.tar |awk -v file='[A-Z]*\\.[A-Z]*$' '{if($8 ~ file)print $8'

HTH

-- Rod Hills

tar -tvf filename.tar |awk -v file="[A-Z]*\.[A-Z]*$" '{if($8 ~ file)print $8'
There be dragons...
Michael Schulte zur Sur
Honored Contributor

Re: awk parsing

Hi,

tar -tvf filename.tar | awk -v file="\.[A-Z]+$" '{if($8 ~ file)print $8}' | while read FILE; do set file=`basename $FILE`;set dir= `dirname $FILE`;done;

Is that what you were looking for?

greetings,

Michael
Hein van den Heuvel
Honored Contributor

Re: awk parsing


> Your command work, however thinking further down at the script

In the awk script or in a shell script that is using n awk command? Check out Micheals example of how to get awk output in a symbol.
In my example the symbols ar ether within the awk script for further processing.
Here are some more samples:

awk:

tar -tvf | awk '{if (match($8,/\/[A-Z\.]+$/)){ dir=substr($8,0,RSTART-1); file=substr($8,RSTART+1,999); print dir,file}}'

perl:

tar -tvf | 'if (/\s(.*)\/([A-Z\.]+)$/) { print "dir=$1, file=$2\n"}'

Hein.