Find pattern in file and print


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find pattern in file and print
# 1  
Old 08-08-2012
Find pattern in file and print

Hello,
I want to read a CSV file and look for a pattern on each line. If the pattern is found I want to read another portion/column of that line and print that. Can somebody help me in writing it in CSH?

E.g. CSV file has following lines,
Code:
1,Elephant,500kg,,,,,
2,Tiger,50kg,,,,,
3,Rabbit,1kg,Left,,,,
4,Lion ,50kg,,,,,
5,Hippo,500kg,,,,,

Then if I pass 500kg, to script I should get "Elephant" and "Hippo" printed at output. When I pass 1kg to script I should get "Rabbit" printed at output.


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by zaxxon; 08-08-2012 at 10:36 AM.. Reason: code tags
# 2  
Old 08-08-2012
Code:
grep 500kg infile.csv| cut -d"," -f2

# 3  
Old 08-08-2012
Sounds like a job for awk.

Code:
awk -F, '$3 == "500kg" { print $2; }'

-F, sets the field separator as comma. $2 and $3 refer to the fields. If you want to put it in a script and pass a shell variable into the awk code, you use -v "awkvar=$shellvar", for example.

Code:
awk -F, -v "weight=$weight" '$3 == weight { print $2; }'

awk variables aren't generally preceded with $ like in shell except for getting the strings inside of a field
# 4  
Old 08-09-2012
Quote:
Originally Posted by neutronscott
Sounds like a job for awk.

Code:
awk -F, '$3 == "500kg" { print $2; }'

-F, sets the field separator as comma. $2 and $3 refer to the fields. If you want to put it in a script and pass a shell variable into the awk code, you use -v "awkvar=$shellvar", for example.

Code:
awk -F, -v "weight=$weight" '$3 == weight { print $2; }'

awk variables aren't generally preceded with $ like in shell except for getting the strings inside of a field
Thanks.

---------- Post updated at 03:06 PM ---------- Previous update was at 02:17 PM ----------

Quote:
Originally Posted by deshiashish
Thanks. Currently I see that all matches are printed in single line, how can I get it printed each match on separate line? Also Could you please let me know how do I get them in a variable so that I can do post processing on them.

Last edited by deshiashish; 08-09-2012 at 05:57 AM..
# 5  
Old 08-09-2012
They should be on separate lines.

If it doesn't fit, please explain what you mean -- show the output you want in code tags.

You can put program output into a variable with backticks.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Find matched pattern and print all based on certain conditions

Hi, I am trying to extract data based on certain conditions. My sample input file as below:- lnc-2:1 OnePiece tra_law 500 688 1 . . g_id "R792.8417"# tra_law_id "R792.8417.1"# g_line "2.711647"# KM "8.723820"# lnc-2:1 OnePiece room 500 510 1 . . g_id "R792.8417"# tra_law_id "R792.8417.1"#... (7 Replies)
Discussion started by: bunny_merah19
7 Replies

2. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

3. Shell Programming and Scripting

Find key pattern and print selected lines for each record

Hi, I need help on a complicated file that I am working on. I wanted to extract important info from a very huge file. It is space delimited file. I have hundred thousands of records in this file. An example content of the inputfile as below:- ## ID Ser402 Old; 23... (2 Replies)
Discussion started by: redse171
2 Replies

4. Shell Programming and Scripting

Gawk Find Pattern Print Lines Before and After

Using grep I can easily use: cvs log |grep -iB 10 -A 10 'date: 2013-10-30' to display search results and 10 lines before and after. How can this be accompished using gawk? (4 Replies)
Discussion started by: metallica1973
4 Replies

5. Shell Programming and Scripting

How to find a file with a specific pattern for current sysdate & upon find email the details?

I need assistance with following requirement, I am new to Unix. I want to do the following task but stuck with file creation date(sysdate) Following is the requirement I need to create a script that will read the abc/xyz/klm folder and look for *.err files for that day’s date and then send an... (4 Replies)
Discussion started by: PreetArul
4 Replies

6. Shell Programming and Scripting

Print Pattern from file

cat file | awk '{printf"(t.who = "'"%s",$1}' Regards ADI (1 Reply)
Discussion started by: adisky123
1 Replies

7. Shell Programming and Scripting

Find a pattern and print next all character to next space

Hi, I have a big inventory file that is NOT sorted is any way. The file is have "tagged" information like the ip address "*IP=" or the name "*NM=" . How do I get just the ip address or the name and not the whole line? I have tried to use AWK without any success. I always get the whole line... (8 Replies)
Discussion started by: pierrebjarnfelt
8 Replies

8. UNIX for Dummies Questions & Answers

Print a pattern from a file

Hi :), Pls help me in solving a problem. I have a file containing several lines as shown below: BS30_CFNRC='P';BS30_BAOC='P';BS30_BOIC='P';BS30_BAIC='P'; BS30_BICRO='P';BS30_CAW='P';Loc_Code='VLR known';VLR_address='919457499995';Nbr_indicator='MSC';MSC_MSRN_Nbr='919457499995'; There is no... (2 Replies)
Discussion started by: vanand420
2 Replies

9. Shell Programming and Scripting

searching pattern in another file and print

Suppose u have a file 12 22 73 another file L22D SSS S12J LLL H77K PPP J25O LOP I73S lOP K99O PLO so output shud like S12J LLL L22D SSS I73S lOP Thanks (2 Replies)
Discussion started by: cdfd123
2 Replies
Login or Register to Ask a Question