select fields from a report (123 Views)
Reply
Frequent Advisor
Dave Walley
Posts: 66
Registered: ‎07-02-1998
Message 1 of 9 (123 Views)
Accepted Solution

select fields from a report

Hi.

A program generates a line of test delimited by |, I have now been asked to extract from this report a number of different fields namely the 4,6 and 10 (these may change from time to time). The fields are not of fixed length and the line can be over a 1000 characters long. I attach a portion of the line. Remember although the line may appear on many lines it is all contained in one line.

I look forward to your responses.


Thanking you

Dave
why do i do this to myself
Honored Contributor
Patrick Wallek
Posts: 13,787
Registered: ‎06-21-2000
Message 2 of 9 (123 Views)

Re: select fields from a report

The easiest way I can think of would be to utilize awk, or if awk can't handle a line of that length, then gnu awk (gawk - http://hpux.connect.org.uk/hppd/hpux/Gnu/gawk-3.1.0/ )

I would do something like:

# awk -F| '{ print $4,$6,$10 } filename
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 3 of 9 (123 Views)

Re: select fields from a report

This is trivially simple as long as any one input line does not exceed 3,000 characters - if so you need the Gnu version of awk.

cat myfile | awk -F '|' '{print $4,$6,$10}'
If it ain't broke, I can fix that.
Honored Contributor
harry d brown jr
Posts: 8,418
Registered: ‎12-12-2000
Message 4 of 9 (123 Views)

Re: select fields from a report


cat filename|cut -d"|" -f4,6,10

or

cat filename|awk -F\| '{print $4," ",$6," ",$10;}'

live free or die
harry
Live Free or Die
Honored Contributor
Rodney Hills
Posts: 1,679
Registered: ‎09-09-1996
Message 5 of 9 (123 Views)

Re: select fields from a report

awk can do this simply-

awk -F\| '{print $4,$6,$10}' otfile

-- Rod Hills
There be dragons...
Honored Contributor
Sachin Patel
Posts: 1,180
Registered: ‎05-07-2000
Message 6 of 9 (123 Views)

Re: select fields from a report

use awk for it

cat filename | awk -F| '{print $4 $6 $10}'

Sachin
Is photography a hobby or another way to spend $
Honored Contributor
John Palmer
Posts: 1,842
Registered: ‎07-28-1997
Message 7 of 9 (123 Views)

Re: select fields from a report

Hi Dave,

I agree with Patrick on awk but you'll have to quote the pipe sign. Something like

awk -F '|' '{print $4 " " $6 " " $10}'

Regards,
John
Respected Contributor
Magdi KAMAL
Posts: 578
Registered: ‎04-18-2000
Message 8 of 9 (123 Views)

Re: select fields from a report

Hi Dave,

# awk -F '|' '{print $4, $6, $10}' fileToBeAnalzed

Will produce what you are looking for since each line within your file is not exceeding 3000 characters.

Magdi
Advisor
Raju Joseph (PDI)
Posts: 12
Registered: ‎03-06-2002
Message 9 of 9 (123 Views)

Re: select fields from a report

Hi

awk -F '|' '{print $4 " " $6 " " $10}'

is the simplest way to extract the columns.

Did this solve your problem ? OR you still want to load this extracted data to aother database/table ?

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