1753797 Members
7093 Online
108799 Solutions
New Discussion юеВ

Re: Field seperator help

 
SOLVED
Go to solution
Gordon_3
Regular Advisor

Field seperator help

Hi all,

Suppose get below output:

0% ( 0%) 0% ( 0%) Apr 20 19:04 backup
1% ( 0%) 0% ( 0%) Apr 20 19:04 backup

How can I get the field of "Apr" in the script? I try awk , but seems not a good solution as it may go to "20" in some case..Thx.

Bgds,
Gordon
Gordon
7 REPLIES 7
Chan 007
Honored Contributor
Solution

Re: Field seperator help

Hi Gordon,

Have you tried,

if the name of the file is test

cat test|awk '{print $5}'

I assume you have only 4 spaces

Chan
Gordon_3
Regular Advisor

Re: Field seperator help

HI Chan,

I suppose u mean $7 :)

OK the thing is, somehow for below output

51% (13%) 0% ( 0%) Apr 20 19:03 backup

$7 will be "20" instead of "Apr", I think the '51' field has some tricky stuff there making it NOT generic to use awk...

Bgds,
Gordon
Gordon
Peter Godron
Honored Contributor

Re: Field seperator help

Gordon,
the problem is the number of spaces.
If you have a fixed number of spaces
echo "0% ( 0%) 0% ( 0%) Apr 20 19:04 backup" | cut -d' ' -f7
or
If you have a fixed length for each field
echo "0% ( 0%) 0% ( 0%) Apr 20 19:04 backup" | cut -c19-22
Gordon_3
Regular Advisor

Re: Field seperator help

Ok some correction, it's due to 13%, coz it fill up all space there and so awk treat that 1 field instead of 2 field... so that's why awk 'not' generic here
Gordon
Gordon_3
Regular Advisor

Re: Field seperator help

Thx peter, seems using character count can deal with this problem effectively !
Gordon
James R. Ferguson
Acclaimed Contributor

Re: Field seperator help

Hi Gordon:

This will extract the field "Apr" (or any three character month) from the format you show:

# perl -nle 'print $1 if /^\d+%.+\)\s+(...)/' file

Regards!

...JRF...

Re: Field seperator help

Hello,

Another way to use awk is to 'count backward', as it seems the first fields are not always equal in length.

awk '{print $(NF-3)}' test

if your file is called 'test'

Greetings,
Philippe