printing 3rd or 4th feild from last in awk.

Whats up fellas... hope someone can help me with the following...

I am parsing an file that is space delimited, however, in the middle, there is an ugly "Account Name" feild that in itself has multiple varying spaces, and commas which throws off my script.
The 1st 3 feilds I am able to obtain easily as well as the last feld....

This is what he file looks like:

head cr123.rpt

100000 121212 11-May-04 Pete's plumbing inc. CASH 21.50

110000 121323 11-May-04 Mcarthey, Robert CASH 30.00

200000 130909 11-May-04 Bob, Andrew, Blake CHECK 40.00

222000 310902 11-May-04 TGIF CREDIT 50.00

--------------------------------------------------

cat cr123.rpt | while read line
do
Acctnum=`echo $line | awk '{print $1}'`
Refnum=`echo $line | awk '{print $2}'`
Date=`echo $line | awk '{print $3}'`
Ammt-`echo $line | awk '{print $NF}'`

---------------------------------------------------

Is there any way to obtain the 2nd feild to the last? for instance, I would like to generate a file of everything BUT the account name.

Hi djsal,

If you want print everything except first field this will do it.

while read line;
do
echo $line | awk '{$1=""; print $0}';
done < data_file

This looks complicated, but it's very fast because ksh is doing all the work without using external programs...

#! /usr/bin/ksh
exec < data
while read line ; do
          acct=${line%% +(?)}
          line=${line#$acct }
          ref=${line%% +(?)}
          line=${line#$ref }
          date=${line%% +(?)}
          line=${line#$date }
          amt=${line##+(?) }
          line=${line% $amt}
          type=${line##+(?) }
          leftover=${line% $type}
done

sorry guys .. when djsal said "2nd field to the last" i misunderstood it as from second field to, until the last field which is not apparently what djsal asked.

Anyway here is one more solution. I am assuminmg that the data format is going to be stable according to your example.

cat data_file | while read line;
do

acct_name=`echo $line | awk '{for(x=4;x<NF-1;x++) print $x}'`;
echo $acct_name;

done

fields 1,2,3 and NF will be extracted as djsal showed. (if you want you can also extract NF-1 field also).

djsal let me know if it doesnt work.

Where you have...

Ammt=`echo $line | awk '{print $NF}'`

...you can also do...

Mthd=`echo $line | awk '{print $(NF-1)}'`

...to get the second to last field.

Much thanx for everyones responses...Ygor and Rameshonline.... I have tried what you have stated here and I get the following message:

cat file.rpt | awk '{print $(NF-1)}'
awk: cmd. line:1: (FILENAME=- FNR=1) fatal: attempt to access field -1

Note: as this was a project for work, I needed to get this done asap (turning a report with headings, wierd spacings, and fields such as "account name" into a pretty little comma delimitted flatfile) I reverted to the "long way" and used a matrix of if statements and grep commands to generate the flatfile. But I still feel as if this would be a bit of helpful knowledge for the future.

Maybe I catch your word:-)

cat file.rpt | awk '{dl=NF-1;sum[$dl]+=$NF}END{for(item in sum)print item,sum[item]}'

The main problem you are having with this...

cat file.rpt | awk '{print $(NF-1)}'

...is because it's not what I posted! What I posted was...

Mthd=`echo $line | awk '{print $(NF-1)}'`

...this works because the shell while loop removes the blank lines in your file.

By the way, awk is good for processing files like your example...

$ awk 'BEGIN{OFS=","}NF>5{print $1,$2,$3,$(NF-1),$NF}' cr123.rpt
100000,121212,11-May-04,CASH,21.50
110000,121323,11-May-04,CASH,30.00
200000,130909,11-May-04,CHECK,40.00
222000,310902,11-May-04,CREDIT,50.00

...the "NF>5" bit means only process files with more than five fields.

Ok I got ya now! I figured I would test it out by running it with input from a cat, didnt know awk would choke on the empty lines. much thanx, this is definately going in my little book of notes!