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?
# 8  
Old 09-21-2016
Quote:
Originally Posted by rbatte1
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
As long as your shell supports < <(command_list) command substitution redirection (which recent versions of bash and ksh do; but will be a syntax error in many other shells), that should be fine.

And, of course, we could also get rid of the grep and build that into the shell script as well. Here is a sample doing that just using standard POSIX shell features:
Code:
#!/bin/ksh
jobName="whatever"

autorep -j "$jobName" |
while read -r line
do	case "$line" in
	(FR*)	status=${line% *}	# Drop last field
		status=${status##* }	# Drop all but last remaining field
		printf 'Extracted "%s" from "%s"\n' "$status" "$line"
		;;
	(*)	printf 'Skip "%s"\n' "$line"
		continue
		;;
	esac
	printf '\n*** Do whatever you want with "%s"\n\n' "$status"
done

I have no idea what output autorep produces, but with an autorep that produces the output:
Code:
JobName StartTime EndTime Status ExitCode
FR1 st1 et1 Status_1 eco
FR2 st2 et2 Status_2 ec2
3FR st3 et3 Status_3 eco
FR4 st4 et4 Status_4 ec4

the above script produces the output:
Code:
Skip "JobName StartTime EndTime Status ExitCode"
Extracted "Status_1" from "FR1 st1 et1 Status_1 ec1"

*** Do whatever you want with "Status_1"

Extracted "Status_2" from "FR2 st2 et2 Status_2 ec2"

*** Do whatever you want with "Status_2"

Skip "3FR st3 et3 Status_3 ec3"
Extracted "Status_4" from "FR4 st4 et4 Status_4 ec4"

*** Do whatever you want with "Status_4"

But, of course, unless Tanu gives us more details about the output autorep -j "$jobName" produces, we have no idea how complex a working script needs to be.

Last edited by Don Cragun; 09-21-2016 at 10:21 AM.. Reason: Fix typo: s/built/build/
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