How to get 2nd last column of the line- UNIX?


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How to get 2nd last column of the line- UNIX?
# 1  
Old 09-21-2016
How to get 2nd last column of the line- UNIX?

I want to retrieve Status from below example. Columns numbers will be dynamic but Status will always be 2nd last-

Code:
JobName StartTime EndTime Status ExitCode

Code:
autorep -j $jobName | grep '^FR' | awk -F' ' '{print $2}'

The above code gives me the 2nd column from start of the line.

Last edited by Don Cragun; 09-21-2016 at 06:34 AM.. Reason: Change B tags to CODE tags.
# 2  
Old 09-21-2016
Hello Tanu,

Please use code tags as per forum rules for your commands/codes/Inputs which you are using into your posts. Following may help you in same.
Code:
autorep -j $jobName | grep '^FR' | awk  '{print $(NF-1)}'

Where $NFdefines last field of each record/line. So $(NF-1)defines the second last field of each line. Similarly you could get 3rd field from last by $(NF-2)and so on.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 09-21-2016
Quote:
Originally Posted by Tanu
I want to retrieve Status from below example. Columns numbers will be dynamic but Status will always be 2nd last-

Code:
JobName StartTime EndTime Status ExitCode

Code:
autorep -j $jobName | grep '^FR' | awk -F' ' '{print $2}'

The above code gives me the 2nd column from start of the line.
Then
Code:
autorep -j $jobName | grep '^FR' | awk -F' ' '{print $NF}'

will give you the last field and:
Code:
autorep -j $jobName | grep '^FR' | awk -F' ' '{print $(NF - 1)}'

will give you the next to the last field.
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 09-21-2016
If they are space separated and you can be certain that Status will always be the penultimate one, you could use variable substitution:-
Code:
status_line=$(autorep -j $jobname | grep -E "^FR" )                # Catch the whole messages
status="${status_line#${status_line% * *}"                         # Chop off everything except the last two fields
status="${status% *}"                                              # Chop off the last field


There is also the awk way to do this, but it could be expensive if you call this in a loop. The above is all within the shell process.

How will you be running this?




I hope that this helps,
Robin



~~~~~~~~~~~~~~
Bah, Don has beaten me to the punch! Smilie

Last edited by rbatte1; 09-21-2016 at 06:42 AM.. Reason: Don replied faster than I did.
This User Gave Thanks to rbatte1 For This Post:
# 5  
Old 09-21-2016
Thanks R.Singh. I'll make sure I'm using code/tags for my next posts.
The solution provided by you worked and I have learned something (NF). Cheers Smilie

---------- Post updated at 04:48 AM ---------- Previous update was at 04:46 AM ----------

@rbatte1 -Thanks for explaining. Really helpful
# 6  
Old 09-21-2016
Quote:
Originally Posted by rbatte1
If they are space separated and you can be certain that Status will always be the penultimate one, you could use variable substitution:-
Code:
status_line=$(autorep -j $jobname | grep -E "^FR" )        # Catch the whole messages
status="${status_line#${status_line% * *}"                 # Chop off everything except the last two fields
status="${status% *}"                                      # Chop off the last field


There is also the awk way to do this, but it could be expensive if you call this in a loop. The above is all within the shell process.

How will you be running this?




I hope that this helps,
Robin



~~~~~~~~~~~~~~
Bah, Don has beaten me to the punch! Smilie
Hi Robin,
It looks like I beat you by about 3 minutes; but Ravinder also beat me by a few seconds.

What you suggested will be faster than what Ravinder and I suggested as long as there is only one line of output selected by the grep. But, I think you had a slight typo... The line:
Code:
status="${status_line#${status_line% * *}"                 # Chop off everything except the last two fields

has mismatched braces. I think you meant:
Code:
status="${status_line#${status_line% * *}}"                # Chop off everything except the last two fields

If more than one line is selected by the grep the awk solutions will yield the next to the last field on each selected line while your suggestion will just yield the next to the last field on the last line. I have no idea whether or not the command:
Code:
autorep -j "$jobname" | grep -E "^FR"

could produce more than one line of output.
# 7  
Old 09-21-2016
Yes, indeed it was left off in error.

I suppose to deal with multi-record output, one could:-
Code:
while read status_line
   status="${status_line#${status_line% * *}}"                # Chop off everything except the last two fields
   status="${status% *}"                                      # Chop off the last field
   .... do whatever here ....
done< <(autorep -j $jobname | grep -E "^FR" )

Would that be any better?
I could make the substitution a single line, but it would get awfully complicated and a not great to support.


Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare 1st column from 2 file and if match print line from 1st file and append column 7 from 2nd

hi I have 2 file with more than 10 columns for both 1st file apple,0,0,0...... orange,1,2,3..... mango,2,4,5..... 2nd file apple,2,3,4,5,6,7... orange,2,3,4,5,6,8... watermerlon,2,3,4,5,6,abc... mango,5,6,7,4,6,def.... (1 Reply)
Discussion started by: tententen
1 Replies

2. UNIX for Dummies Questions & Answers

Want the UNIX code - I want to sum of the 1st column wherever the first 2nd and 3rd columns r equal

I have the code for the below things.. File1 has the content as below 8859 0 subscriberCreate 18 0 subscriberPaymentMethodChange 1650 0 subscriberProfileUpdate 7668 0 subscriberStatusChange 13 4020100 subscriberProfileUpdate 1 4020129 subscriberStatusChange 2 4020307 subscriberCreate 8831... (5 Replies)
Discussion started by: Mahen
5 Replies

3. Shell Programming and Scripting

Transpose from 2nd column till the last column

Hi I have 5 columns like this a b c d e f g h i j k l m n o From 2nd column till the 5th column of every record, I would like to transpose them as rows, so my output file contains only one row a b c d e f g h i j (9 Replies)
Discussion started by: jacobs.smith
9 Replies

4. Shell Programming and Scripting

Calculate 2nd Column Based on 1st Column

Dear All, I have input file like this. input.txt CE2_12-15 3950.00 589221.0 9849709.0 768.0 CE2_12_2012 CE2_12-15 3949.00 589199.0 9849721.0 768.0 CE2_12_2012 CE2_12-15 3948.00 589178.0 9849734.0 768.0 CE2_12_2012 CE2_12-52 1157.00 ... (3 Replies)
Discussion started by: attila
3 Replies

5. Shell Programming and Scripting

1st column,2nd column on first line 3rd,4th on second line ect...

I need to take one column of data and put it into the following format: 1st line,2nd line 3rd line,4th line 5th line,6th line ... Thanks! (6 Replies)
Discussion started by: batcho
6 Replies

6. Shell Programming and Scripting

Replace 2nd column for each line in a csv file with fixed string+random number

Hi experts, My csv file looks like this U;cake;michael;temp;;;; U;bread;john;temp;;;; U;cocktails;sarah;temp;;;; I'd like to change the value fo 2nd column to cf+random number , which will look maybe something like this U;cf20187;michael;temp;;;; U;cf8926;john;temp;;;;... (7 Replies)
Discussion started by: tententen
7 Replies

7. Shell Programming and Scripting

Replace 2nd column of CSV file with numbers on line

I have a csv file with occasional multiple entries in the second column. 111111,104,07-24-2011,3.15,N, 222222,020 140,07-24-2011,10.00,N,I want the result 111111,104,07-24-2011,3.15,N, 222222,020,07-24-2011,10.00,N, 222222,140,07-24-2011,10.00,N, I know I can get the output of the second... (5 Replies)
Discussion started by: ffdstanley
5 Replies

8. Shell Programming and Scripting

comparing column of two different files and print the column from in order of 2nd file

Hi friends, My file is like: Second file is : I need to print the rows present in file one, but in order present in second file....I used while read gh;do awk ' $1=="' $gh'" {print >> FILENAME"output"} ' cat listoffirstfile done < secondfile but the output I am... (14 Replies)
Discussion started by: CAch
14 Replies

9. Shell Programming and Scripting

grep data on 2nd line and 3rd column

How do I grep/check the on-hand value on the second line of show_prod script below? In this case it's a "3". So if it's > 0, then run_this, otherwise, quit. > ./show_prod Product Status Onhand Price shoe OK 3 1.1 (6 Replies)
Discussion started by: joker_789us
6 Replies

10. Shell Programming and Scripting

Parse 1 column and add 2nd column

I'm racking my brain on this one! :( I have a list like this: Paul 20 Paul 25 Paul 30 Frank 10 Julie 15 Julie 13 etc, etc... I've been trying to figure out a way to have the output display the name in the first column ONCE and add the numbers in the second column and display that... (2 Replies)
Discussion started by: sdlennon
2 Replies
Login or Register to Ask a Question