Egrep multi rows


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Egrep multi rows
# 1  
Old 09-01-2014
Egrep multi rows

I have file input

input.txt

Code:
20140730|13|TORONTO|Pavja|13127|Moto|0|1568|2035|-467|59478|352450|2014-08-11 23:12:15
20140730|13|TORONTO|Pavja|13127|Manual|0|696|742|-46|38826|141449|2014-08-11 23:12:15
20140730|13|TORONTO|Pavja|13127|Semimanual|0|0|3|-3|0|317|2014-08-11 23:12:15
20140730|13|TORONTO|Pavja|13127||0|376|31|345|19171|19776|2014-08-11 23:12:15
20140731|13|TORONTO|Sloven|13122|Moto|0|0|6|-6|0|13140|2014-08-11 23:12:15
20140731|13|TORONTO|Sloven|13122|Manual|0|0|1|-1|0|126|2014-08-11 23:12:15
20140731|13|OTTAWA|City Earth|13140|Moto|0|0|103|-103|0|61086|2014-08-11 23:12:15
20140731|13|OTTAWA|City Earth|13140|Manual|0|0|10|-10|7|12798|2014-08-11 23:12:15
20140731|13|OTTAWA|City Earth|13140|Semimanual|0|0|0|0|0|237|2014-08-11 23:12:15
20140731|13|OTTAWA|City Earth|13140||0|1|0|1|81|189|2014-08-11 23:12:15
20140731|13|OTTAWA|Pavja|13122|Moto|0|1690|2109|-419|57229|354140|2014-08-11 23:12:15
20140731|13|OTTAWA|Pavja|13122|Manual|0|741|785|-44|37458|142190|2014-08-11 23:12:15
20140731|13|OTTAWA|Pavja|13122|Semimanual|0|0|0|0|0|317|2014-08-11 23:12:15
20140731|13|OTTAWA|Pavja|13122||0|375|24|351|18796|20151|2014-08-11 23:12:15


i need to print when column 5 consists of 13127 or 13140
my expected output

Code:
20140730|13|TORONTO|Pavja|13127|Moto|0|1568|2035|-467|59478|352450|2014-08-11 23:12:15
20140730|13|TORONTO|Pavja|13127|Manual|0|696|742|-46|38826|141449|2014-08-11 23:12:15
20140730|13|TORONTO|Pavja|13127|Semimanual|0|0|3|-3|0|317|2014-08-11 23:12:15
20140730|13|TORONTO|Pavja|13127||0|376|31|345|19171|19776|2014-08-11 23:12:15
20140731|13|OTTAWA|City Earth|13140|Moto|0|0|103|-103|0|61086|2014-08-11 23:12:15
20140731|13|OTTAWA|City Earth|13140|Manual|0|0|10|-10|7|12798|2014-08-11 23:12:15
20140731|13|OTTAWA|City Earth|13140|Semimanual|0|0|0|0|0|237|2014-08-11 23:12:15
20140731|13|OTTAWA|City Earth|13140||0|1|0|1|81|189|2014-08-11 23:12:15


i did this
Code:
egrep '13127|13140' input.txt

but i figure it out that line 5 also has value of 13140 in column 12

please help
# 2  
Old 09-01-2014
Try awk:
Code:
awk -F\| '$5==13127 || $5==13140' file

or
Code:
awk -F\| '$5~/^(13127|13140)$/' file

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 09-01-2014
Try grep:
Code:
grep -E "^([^|]*\|){4}(13127|13140)" file

These 2 Users Gave Thanks to RudiC For This Post:
# 4  
Old 09-01-2014
Hello,

One more approach with awk using variables.

Code:
awk -F"|" '$5==s1 || $5==s2 {print}' s1="13127" s2="13140" filename

Output will be as follows.

Code:
20140730|13|TORONTO|Pavja|13127|Moto|0|1568|2035|-467|59478|352450|2014-08-11 23:12:15
20140730|13|TORONTO|Pavja|13127|Manual|0|696|742|-46|38826|141449|2014-08-11 23:12:15
20140730|13|TORONTO|Pavja|13127|Semimanual|0|0|3|-3|0|317|2014-08-11 23:12:15
20140730|13|TORONTO|Pavja|13127||0|376|31|345|19171|19776|2014-08-11 23:12:15
20140731|13|OTTAWA|City Earth|13140|Moto|0|0|103|-103|0|61086|2014-08-11 23:12:15
20140731|13|OTTAWA|City Earth|13140|Manual|0|0|10|-10|7|12798|2014-08-11 23:12:15
20140731|13|OTTAWA|City Earth|13140|Semimanual|0|0|0|0|0|237|2014-08-11 23:12:15
20140731|13|OTTAWA|City Earth|13140||0|1|0|1|81|189|2014-08-11 23:12:15


Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 09-01-2014
Quote:
Originally Posted by RudiC
Try grep:
Code:
grep -E "^([^|]*\|){4}(13127|13140)" file

This is fine if the 5th field is always 5 or fewer characters. If there could be more than 5 characters in that field, you'd want an additional field separator at the end of the ERE:
Code:
grep -E "^([^|]*\|){4}(13127|13140)\|" file


Last edited by Don Cragun; 09-01-2014 at 05:53 AM.. Reason: Fix typo "|" -> "\|"
These 2 Users Gave Thanks to Don Cragun For This Post:
# 6  
Old 09-01-2014
@RudiC: Unless the length is always <=5 digits, it would be better to include a \| as an anchor at the back...
These 2 Users Gave Thanks to Scrutinizer For This Post:
# 7  
Old 09-01-2014
Well, I thought about that after having posted; but the "|" needs to be escaped as Scrutinizer did it.
This User Gave Thanks to RudiC For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with reformat single-line multi-fasta into multi-line multi-fasta

Input File: >Seq1 ASDADAFASFASFADGSDGFSDFSDFSDFSDFSDFSDFSDFSDFSDFSDFSD >Seq2 SDASDAQEQWEQeqAdfaasd >Seq3 ASDSALGHIUDFJANCAGPATHLACJHPAUTYNJKG ...... Desired Output File >Seq1 ASDADAFASF ASFADGSDGF SDFSDFSDFS DFSDFSDFSD FSDFSDFSDF SD >Seq2 (4 Replies)
Discussion started by: patrick87
4 Replies

2. Programming

DB2 Query -Convert multi values from column to rows

Hi Team I am using DB2 artisan tool and struck to handle multi values present in columns that are comma(,) separated. I want to convert those column values in separate rows . For example : Column 1 Column2 Jan,Feb Hold,Sell,Buy Expected Result Column1 ... (3 Replies)
Discussion started by: Perlbaby
3 Replies

3. Programming

Multi head/multi window hello world

I am trying to write a large X app. I have successfully modified my xorg.conf to setup 4 monitors on an NVIDIA Quatro5200. I am trying to modify a simple hello world application to open a window on three of the four monitors. depending on the changes to loop the window creation section and event... (2 Replies)
Discussion started by: advorak
2 Replies

4. Shell Programming and Scripting

How to substract selective values in multi row, multi column file (using awk or sed?)

Hi, I have a problem where I need to make this input: nameRow1a,text1a,text2a,floatValue1a,FloatValue2a,...,floatValue140a nameRow1b,text1b,text2b,floatValue1b,FloatValue2b,...,floatValue140b look like this output: nameRow1a,text1b,text2a,(floatValue1a - floatValue1b),(floatValue2a -... (4 Replies)
Discussion started by: nricardo
4 Replies

5. Shell Programming and Scripting

Sort the multi column rows

abc xyz - - - - - - - - - - - How to sort the second column in ascending order. (2 Replies)
Discussion started by: sandy1028
2 Replies

6. UNIX for Dummies Questions & Answers

find and remove rows from file where multi occurrences of character found

I have a '~' delimited file of 6 - 7 million rows. Each row should contain 13 columns delimited by 12 ~'s. Where there are 13 tildes, the row needs to be removed. Each row contains alphanumeric data and occasionally a ~ ends up in a descriptive field and therefore acts as a delimiter, resulting in... (1 Reply)
Discussion started by: kpd
1 Replies

7. UNIX for Dummies Questions & Answers

search ")" with egrep - egrep: syntax error

Hi Guys, we have a shell script which basically query the Database which retrieves huge data and use the data with "egrep" . Now there is some data which contains characters like "abc)" and the same is used like below : "egrep (.+\|GDPRAB16\|GDPR/11702 96 abc)\|$ temp.txt" now while... (7 Replies)
Discussion started by: sagarjani
7 Replies

8. UNIX for Dummies Questions & Answers

Egrep cheat sheet anywhere? Looking for meaning of egrep -c

Hi I've been searching google and have not found what egrep -c means. Does anyone know where I can get a cheat sheet or what that -c means? thanks, Linda (2 Replies)
Discussion started by: leelm
2 Replies

9. UNIX for Dummies Questions & Answers

Multi User Multi Task

Dear Experts Why we always hear that unix operating system is Multi User and Multi task. What does these two means. I have looked at some books and documents but couldn't find aclear explenation. Can we say Windows operating system is also multi user and multi task?? Thanks for your help in... (6 Replies)
Discussion started by: Reza Nazarian
6 Replies

10. UNIX for Dummies Questions & Answers

multi-file multi-edit

Good day! I am trying to learn how to use the "sed" editor, to perform multiple edits on multiple files in multiple directories. I have one script that tries to call up each file and process it according to the edits listed in a second script. I am using a small input text to test these, at... (12 Replies)
Discussion started by: kielitaide
12 Replies
Login or Register to Ask a Question