grep data on 2nd line and 3rd column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep data on 2nd line and 3rd column
# 1  
Old 03-31-2010
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.

Code:
> ./show_prod
Product   Status   Onhand   Price
shoe   OK   3   1.1


Last edited by Franklin52; 03-31-2010 at 08:06 AM.. Reason: Please use code tags!
# 2  
Old 03-31-2010
What you want to run if that value is >0 ?
Also, show_prod is a script and it prints only the above two lines?
# 3  
Old 03-31-2010
Try this:

Code:
val=`sed -n '2p' show_prod | awk '{print $3}'`
if [ "$val" = "0" ] 
then
  ACTION - commands
else
  QUIT - commands
fi


Last edited by Franklin52; 03-31-2010 at 08:06 AM.. Reason: Please indent your code and use code tags, thank you.
# 4  
Old 03-31-2010
Assuming the 2 lines are the output of the command ./show_prod:
Code:
on-hand=$(./show_prod | awk 'NR==2{print $3}')
if [ $on-hand -gt 0 ]
then
  run_this
else
  exit
fi

# 5  
Old 03-31-2010
Quote:
Originally Posted by Jairaj
Try this:

Code:
val=`sed -n '2p' show_prod | awk '{print $3}'`

That type of sed invocation is _never_ necessary. Look at Franklin52's code to see how to avoid creating an unnecessary process.

Regards,
Alister
# 6  
Old 03-31-2010
Thanks Alister for your input.
Anyway my command also work. But Franklin52 code is excellent...
# 7  
Old 04-01-2010
Thank you everyone for your feedback/help. The code works.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

Print the 1st column and the value in 2nd or 3rd column if that is different from the values in 1st

I have file that looks like this, DIP-17571N|refseq:NP_651151 DIP-17460N|refseq:NP_511165|uniprotkb:P45890 DIP-17571N|refseq:NP_651151 DIP-19241N|refseq:NP_524261 DIP-19241N|refseq:NP_524261 DIP-17151N|refseq:NP_524316|uniprotkb:O16797 DIP-19588N|refseq:NP_731165 ... (2 Replies)
Discussion started by: Syeda Sumayya
2 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. UNIX for Dummies Questions & Answers

Grep -v value in 2nd column

Trying to do a grep -v on a value in the 2nd column of text. So if the word apple appears in a line in the 2nd column, it would not show up when the file was cat. Seems like a simple enough operation but I just can't figure it out. Any help would be appreciated. Thanks in advance. Are apples... (4 Replies)
Discussion started by: jimmyf
4 Replies

4. Shell Programming and Scripting

Insert data in first column(if blank) from previous line first column

Dear Team I need to insert field(which is need to taken from previous line's first field) in first column if its blank. I had tried using sed but not find the way. Detail input and output file as below. Kindly help for same. INPUT: SCGR SC DEV DEV1 NUMDEV DCP ... (7 Replies)
Discussion started by: jaydeep_sadaria
7 Replies

5. Shell Programming and Scripting

Grep special pattern in 3rd column

I have a file (test.dat) that has this pattern: 1000 000001 (92.431343802235503, 90.0) 1000 000002 (87.568656197764497, 80.0) 1000 000003 (150.75815307316083, 150.0) 1000 000004 (29.241846926839159, 20.0) 1000 000005 (110.02128542766, 110.0) 1000 000006 (69.978714572339996, 60.0) 1000... (8 Replies)
Discussion started by: kayak
8 Replies

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

7. Shell Programming and Scripting

get 3rd column of nth line

hi; i have a file.txt and its 9th, 10th and 11th line lines are: RbsLocalCell=S2C1 maxPortIP 4 (this is 9th line) RbsLocalCell=S3C1 maxPortIP 4 (this is 10th line) RbsLocalCell=S1C1 ... (11 Replies)
Discussion started by: gc_sw
11 Replies

8. Shell Programming and Scripting

split on the basis of 2nd and 3rd column

file A aa 22 48 ab 22 48 tcf 50 76 gf 50 76 h 89 100 yh 89 100 how can we split the file on the basis of common 2 and third column output like file A-1 aa 22 48 ab 22 48 file A-2 cf 50 76 gf 50 76 (3 Replies)
Discussion started by: cdfd123
3 Replies

9. Shell Programming and Scripting

Awk multiple lines with 3rd column onto a single line?

I have a H U G E file with over 1million entries in it. Looks something like this: USER0001|DEVICE001|VAR1 USER0001|DEVICE001|VAR2 USER0001|DEVICE001|VAR3 USER0001|DEVICE001|VAR4 USER0001|DEVICE001|VAR5 USER0001|DEVICE001|VAR6 USER0001|DEVICE002|VAR1 USER0001|DEVICE002|VAR2... (4 Replies)
Discussion started by: SoMoney
4 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