Re: Extract fields from text file. (339 Views)
Reply
Esteemed Contributor
Rajesh SB
Posts: 371
Registered: ‎02-24-2001
Message 1 of 11 (339 Views)
Accepted Solution

Extract fields from text file.

Hi Experts,

I have df -k output text
Can any body help me in extracting last 2 column fields(Space Separator) from below text irrespective of no. of coloumns.

mpfs 38G 0K 38G 0% /dev/vx/rdmp
swap 38G 356M 38G 1% /tmp
/dev/vx/dsk/bootdg/opt
32G 15G 17G 48% /opt
/dev/vx/dsk/tempbackup_DG/tempvol
200G 8.6G 179G 5% /tempbackup
/dev/vx/dsk/dbaarch_DG/dbaarch_data_Vol6
98G 28G 69G 29% /opt/oraclea/arch
/dev/vx/dsk/dbabckp_DG/dbabckp_data_Vol5
100G 25G 75G 25% /opt/oraclea/backup
/dev/vx/dsk/dba_DG/dba_data_Vol2
50G 48G 1.6G 97% /opt/oraclea/mnt2

Any tips.

Thanks & Regards,
Rajesh SB
Trusted Contributor
Piergiacomo Perini
Posts: 219
Registered: ‎05-19-2003
Message 2 of 11 (339 Views)

Re: Extract fields from text file.

Hi Rajesh SB,
try this
df -k > dfk.txt
awk â Fâ â â { print $5, $6 }â dfk.txt

regards
pg
Honored Contributor
Wouter Jagers
Posts: 495
Registered: ‎10-01-2002
Message 3 of 11 (339 Views)

Re: Extract fields from text file.

Hi,

This seems to work overhere:

# cat file.txt | awk '{FROM=(NF-2);print $FROM,$(FROM+1),$NF}'

--> returns 3 last fields.

Cheers,
Wout
an engineer's aim in a discussion is not to persuade, but to clarify.
Esteemed Contributor
Rajesh SB
Posts: 371
Registered: ‎02-24-2001
Message 4 of 11 (339 Views)

Re: Extract fields from text file.

Thanks PG for quick response.

Some of the lines are having 6 columns and some are with 5 columns.

I am particularly looking for mount-point and percentage columns.

You command won't work.
Any other tips.

Regards,
Rajesh
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 5 of 11 (339 Views)

Re: Extract fields from text file.

Hi:

# awk '{print $NF,$(NF-1)}' file

Regards!

...JRF...
Honored Contributor
Peter Nikitka
Posts: 1,575
Registered: ‎02-10-2003
Message 6 of 11 (339 Views)

Re: Extract fields from text file.

Hi,

I would add a check to send output only when there are more than 2 columns - this will deal correctly with the bdf output for long device names:

bdf -l | awk 'NF>2 {print $(NF-1),$NF}'

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Esteemed Contributor
Rajesh SB
Posts: 371
Registered: ‎02-24-2001
Message 7 of 11 (339 Views)

Re: Extract fields from text file.

Thanks Peter,
You command is working fine.
Highly appreciate your timely response.

Case-1 Not Worked
cat df-k.op| awk '{FROM=(NF-2);print $FROM,$(FROM+1),$NF}'
awk: The field -1 must be in the range 0 to 199.

The input line number is 2.
The source line number is 1.

Case-2 Not worked:
cat df-k.op|grep -v Filesystem| awk '{FROM=(NF-2);print $FROM,$(FROM+1),$NF}'


Thanks to you all for quick response again

Thanks & Regards,


Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 8 of 11 (339 Views)

Re: Extract fields from text file.

Hi (again) Rajesh:

Avoid the extra 'cat' process just to read a file. Instead of :

# cat myfile | awk '...'

...simply do:

# awk '...' myfile

Regards!

...JRF...

Honored Contributor
Wouter Jagers
Posts: 495
Registered: ‎10-01-2002
Message 9 of 11 (339 Views)

Re: Extract fields from text file.

Very true, I should learn not posting that.

(you know how it goes: make a sample file, cat it to check, use command history and start piping..)

Also, sorry for confusing the numbers two and three. That should not happen too often or I'm going to get myself in trouble one day :-)

Cheers
an engineer's aim in a discussion is not to persuade, but to clarify.
Esteemed Contributor
Rajesh SB
Posts: 371
Registered: ‎02-24-2001
Message 10 of 11 (339 Views)

Re: Extract fields from text file.

Thanks James for your tips on effecting scripting.
It is good to avoid unnecessary extra command usage.

Learning is never ending process!

Cheers,
Rajesh


Esteemed Contributor
Rajesh SB
Posts: 371
Registered: ‎02-24-2001
Message 11 of 11 (339 Views)

Re: Extract fields from text file.

Hi,

I am able to extract the last 2 coloumn using below awk command.

# bdf -l | awk 'NF>2 {print $(NF-1),$NF}'

Cheers,
Rajesh SB
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.