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

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting printing 3rd or 4th feild from last in awk.
# 1  
Old 05-13-2004
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
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.
# 2  
Old 05-13-2004
Hi djsal,

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

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

Last edited by rameshonline; 05-13-2004 at 04:06 PM..
# 3  
Old 05-13-2004
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}

# 4  
Old 05-13-2004
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;

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


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.

Last edited by rameshonline; 05-13-2004 at 04:52 PM..
# 5  
Old 05-13-2004
Where you have...

Ammt=`echo $line | awk '{print $NF}'` can also do...

Mthd=`echo $line | awk '{print $(NF-1)}'` get the second to last field.
# 6  
Old 05-14-2004
Originally posted by Ygor
Where you have...

Ammt=`echo $line | awk '{print $NF}'` can also do...

Mthd=`echo $line | awk '{print $(NF-1)}'` 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.
# 7  
Old 05-15-2004
Maybe I catch your word:-)
cat file.rpt | awk '{dl=NF-1;sum[$dl]+=$NF}END{for(item in sum)print item,sum[item]}'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print the string between 3rd and 4th backslashs to end of line

im trying to get awk to print the string between 3rd and 4th backslashs to end of line test could be any word this >to this > testalso the other way round insert string at end of line... (13 Replies)
Discussion started by: bob123
13 Replies

2. Shell Programming and Scripting

Solution for replacement of 4th column with 3rd column in a file using awk/sed preserving delimters

input "A","B","C,D","E","F" "S","T","U,V","W","X" "AA","BB","CC,DD","EEEE","FFF" required output: "A","B","C,D","C,D","F" "S", T","U,V","U,V","X" "AA","BB","CC,DD","CC,DD","FFF" tried using awk but double quotes not preserving for every field. any help to solve this is much... (5 Replies)
Discussion started by: khblts
5 Replies

3. Shell Programming and Scripting

Changing values only in 3rd column and 4th column

#cat file testing test! nipw asdkjasjdk ok! what !ok host server1 check_ssh_disk!!30!50!/ other host server 2 des check_ssh_disk!!40!30!/ #grep check file| awk -F! '{print $3,$4}'|awk '{gsub($1,"",$1)}1' 50 30 # Output: (6 Replies)
Discussion started by: kenshinhimura
6 Replies

4. UNIX for Dummies Questions & Answers

Search word in 3rd column and move it to next column (4th)

Hi, I have a file with +/- 13000 lines and 4 column. I need to search the 3rd column for a word that begins with "SAP-" and move/skip it to the next column (4th). Because the 3rd column need to stay empty. Thanks in advance.:) 89653 36891 OTR-60 SAP-2 89653 36892 OTR-10 SAP-2... (2 Replies)
Discussion started by: AK47
2 Replies

5. Shell Programming and Scripting

awk Quick Help: printing upto 3rd octet .

Hi Experts, I am trying to print $2 & the IP_address upto 3rd octet only. But unable to do so, Trying # awk '{print $2, substr($4,1,9)}' file . but not correct File: HOST= cmiHOST06 :: /nbu/cmiHOST06/athpx07/aa1 HOST= cmiHOST05 :: (5 Replies)
Discussion started by: rveri
5 Replies

6. UNIX for Dummies Questions & Answers

Write 2nd and 3rd fields to a 4th file name?

I have a flatfile A.txt date|products|notes|location 121117|a108|this is a test|florida 121118|b111|just test it|tampa How do i write an awk to create a file name as location.txt and have products:notes awk -F'|' '{ print $2 ":" $3 }' A.txt > $4.txt I am sure it cannot write to... (5 Replies)
Discussion started by: sabercats
5 Replies

7. Shell Programming and Scripting

How to extract 3rd,4th and 5th element

Hi All! I trying to execute this perl script to extract a particular field in a text file, luckily it works. But, right now I would like to extract 3 more fields in the text file but couldnt get the right syntax on how to do it. command is: perl -pe '$_ = (split(//)) . "\n"' TestDoc.txt... (3 Replies)
Discussion started by: VicNetIT
3 Replies

8. UNIX for Advanced & Expert Users

if 4th and 5th character of sting -ge 45 then add 1 to 3rd character

I want to know how to, given a string like W87151WR71C, if the 4th and 5th character (in this case 15) are greater than 45, then to add 1 to the 3rd character (in this case 7) and assign the revised string the variable name MODSTRING. Thanks in advance. This is ultimately to grab info from... (6 Replies)
Discussion started by: glev2005
6 Replies

9. Shell Programming and Scripting

Awk reporting. Format the output with left justification for every feild

Fallowing is the input file that is pipe seperated. is it possible to generated the report that is alligned left justifed as that of sample output. I apprecitae your help on this. InputFile (temp.txt): 108005555|001|christina.lipski||Submitter... (3 Replies)
Discussion started by: ainuddin
3 Replies

10. Shell Programming and Scripting

How to extract 3rd line 4th column of a file

Hi, Shell script: I would need help on How to extract 3rd line 4th column of a file with single liner Thanks in advance. (4 Replies)
Discussion started by: krishnamurthig
4 Replies
Login or Register to Ask a Question

Featured Tech Videos