Operating System - Linux
1753487 Members
4532 Online
108794 Solutions
New Discussion юеВ

Re: Extract fields from text file.

 
SOLVED
Go to solution
Rajesh SB
Esteemed Contributor

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
10 REPLIES 10
Piergiacomo Perini
Trusted Contributor

Re: Extract fields from text file.

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

regards
pg
Wouter Jagers
Honored Contributor

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.
Rajesh SB
Esteemed Contributor

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
James R. Ferguson
Acclaimed Contributor

Re: Extract fields from text file.

Hi:

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

Regards!

...JRF...
Peter Nikitka
Honored Contributor
Solution

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"
Rajesh SB
Esteemed Contributor

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,


James R. Ferguson
Acclaimed Contributor

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...

Wouter Jagers
Honored Contributor

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.
Rajesh SB
Esteemed Contributor

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