Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

How to grep a single field from a line?

Shell Programming and Scripting


Tags
shell scipt, unix

Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 11-12-2013
Little Little is offline
Registered User
 
Join Date: Feb 2013
Last Activity: 21 March 2015, 5:38 AM EDT
Posts: 189
Thanks: 32
Thanked 9 Times in 8 Posts
How to grep a single field from a line?

hi,

i have a text file with some fields.
file.txt

Code:
aaa|bbb|ccc|1-sh|2-sh|5-sh
ddd|eee|fff|1-sh|4-sh

i am selecting a line using grep command using the combination of 1st 3 column.

Code:
grep "aaa|bbb|ccc" file.txt

output:
aaa|bbb|ccc|1-sh|2-sh|5-sh

now i want to cut a field from this line.
if i am passing 2 as a parameter to a program, then it should cut "2-sh", if i specify 5 then it should cut 5-sh and so on.
Sponsored Links
    #2  
Old Unix and Linux 11-12-2013
RavinderSingh13 RavinderSingh13 is offline
Registered User
 
Join Date: May 2013
Last Activity: 7 April 2015, 12:22 PM EDT
Location: Chennai
Posts: 1,174
Thanks: 284
Thanked 369 Times in 355 Posts
Hi,

Could you please use the following code.


Code:
echo "aaa|bbb|ccc|1-sh|2-sh|5-sh" | awk -F"|" '{print $5}'

Output will be as follows.


Code:
2-sh

kindly let me know if you have any queries for same.



Thanks,
R. Singh
Sponsored Links
    #3  
Old Unix and Linux 11-12-2013
RudiC RudiC is online now Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 19 April 2015, 9:57 AM EDT
Location: Aachen, Germany
Posts: 5,718
Thanks: 101
Thanked 1,504 Times in 1,413 Posts
Your specification is a bit difficult to interpret. If you, in a line that begins with "aaa|bbb|ccc", want that field that begins with your parameter value (2 or 5), try
Code:
awk '-F|' '/^aaa|bbb|ccc/ {for (i=4; i<=NF; i++) if ($i ~ "^"fn) print $i}' fn=2 file
2-sh


Last edited by RudiC; 11-12-2013 at 06:04 AM..
    #4  
Old Unix and Linux 11-12-2013
Akshay Hegde's Unix or Linux Image
Akshay Hegde Akshay Hegde is offline Forum Advisor  
Forum Advisor
 
Join Date: Nov 2012
Last Activity: 13 April 2015, 5:46 AM EDT
Location: Goa / Bangalore, India
Posts: 1,698
Thanks: 84
Thanked 572 Times in 521 Posts
Try something like this


Code:
$ cat <<eof | awk -F"|" '/^aaa\|bbb\|ccc/{for(i=1;i<=NF;i++)if($i~search"-sh")print $i}' search=1
aaa|bbb|ccc|1-sh|2-sh|5-sh
ddd|eee|fff|1-sh|4-sh
eof     

1-sh
$ cat <<eof | awk -F"|" '/^aaa\|bbb\|ccc/{for(i=1;i<=NF;i++)if($i~search"-sh")print $i}' search=2
aaa|bbb|ccc|1-sh|2-sh|5-sh
ddd|eee|fff|1-sh|4-sh
eof

2-sh
$ cat <<eof | awk -F"|" '/^aaa\|bbb\|ccc/{for(i=1;i<=NF;i++)if($i~search"-sh")print $i}' search=5
aaa|bbb|ccc|1-sh|2-sh|5-sh
ddd|eee|fff|1-sh|4-sh
eof

5-sh

@RudyC I think you missed back slash


Code:
$ cat <<eof | awk -F"|" '/^aaa|bbb|ccc/{for(i=1;i<=NF;i++)if($i~search"-sh")print $i}' search=1
aaa|bbb|ccc|1-sh|2-sh|5-sh
ddd|eee|fff|1-sh|4-sh
aaa|1-sh
eof

1-sh
1-sh

$ cat <<eof | awk -F"|" '/^aaa\|bbb\|ccc/{for(i=1;i<=NF;i++)if($i~search"-sh")print $i}' search=1
aaa|bbb|ccc|1-sh|2-sh|5-sh
ddd|eee|fff|1-sh|4-sh
aaa|1-sh
eof

1-sh

Sponsored Links
    #5  
Old Unix and Linux 11-12-2013
RudiC RudiC is online now Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 19 April 2015, 9:57 AM EDT
Location: Aachen, Germany
Posts: 5,718
Thanks: 101
Thanked 1,504 Times in 1,413 Posts
@Akshay Hegde: Not needed for my mawk. But, doesn't hurt, either. Thanks for pointing out.
Sponsored Links
    #6  
Old Unix and Linux 11-12-2013
Akshay Hegde's Unix or Linux Image
Akshay Hegde Akshay Hegde is offline Forum Advisor  
Forum Advisor
 
Join Date: Nov 2012
Last Activity: 13 April 2015, 5:46 AM EDT
Location: Goa / Bangalore, India
Posts: 1,698
Thanks: 84
Thanked 572 Times in 521 Posts
@ RudiC : I tried in gawk so posted..
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
sed command to grep multiple pattern present in single line and delete that line vivek d r Shell Programming and Scripting 14 05-14-2013 08:07 AM
Compare Field in Current Line with Field in Previous moutaye Shell Programming and Scripting 9 03-27-2012 05:18 AM
Multiple lines in a single column to be merged as a single line for a record Bhuvaneswari Shell Programming and Scripting 1 08-11-2011 03:16 AM
grep multiple words in a single line anduzzi Shell Programming and Scripting 11 06-25-2009 11:39 AM
replace single field out of 60 ajp7701 Shell Programming and Scripting 2 04-21-2008 07:17 PM



All times are GMT -4. The time now is 10:07 AM.