Print selected lines from file in order


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print selected lines from file in order
# 1  
Old 08-04-2011
Print selected lines from file in order

I need to extract selected lines from a log file, I can use grep to pull one line matching 'x' or matching 'y', how can I run through the log printing both matching lines in order top to bottom.

i.e

Code:
line 1 xyz - not needed
line 2 User01 - needed
line 3 123 - not needed
line 4 Info - needed
line 5 abc - not needed
line 6 .... - not needed
line 7 .... - not needed
line 8 xyz - not needed
line 9 User02 - needed
line 10 123 - not needed
line 11 Info - needed
line 12 abc - not needed
line 13.... - not needed
line 14 .... - not needed
cont .....

so as described above I only want to print lines containing 'User' and 'Info' in consecutive order for hundreds of users from one log

Thanks

Last edited by radoulov; 08-04-2011 at 08:44 PM.. Reason: Code tags.
# 2  
Old 08-04-2011
Code:
grep -E 'x|y'

# 3  
Old 08-04-2011
MySQL

Quote:
Originally Posted by DGPickett
Code:
grep -E 'x|y'

Thanks for reply, I tried your suggestion

Code:
grep -E 'x|y' xyz.log

I am still only getting 'x' printed, no 'y'

---------- Post updated at 07:57 AM ---------- Previous update was at 07:41 AM ----------

Quote:
Originally Posted by DGPickett
Code:
grep -E 'x|y'

Ignore the previous, it worked fine. Case was causing the problem.

Thanks very much for response.Smilie

Last edited by radoulov; 08-04-2011 at 08:44 PM.. Reason: Code tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. UNIX for Dummies Questions & Answers

Compare file1 and file2, print matching lines in same order as file1

I want to print only the lines in file2 that match file1, in the same order as they appear in file 1 file1 file2 desired output: I'm getting the lines to match awk 'FNR==NR {a++}; FNR!=NR && a' file1 file2 but they are in sorted order, which is not what I want: Can anyone... (4 Replies)
Discussion started by: pathunkathunk
4 Replies

3. Shell Programming and Scripting

Deleting selected lines in a file

Hi Guys , I have two files say a1 and a2 having following contents a1 dag wfd a2 dag wfd chire hcm I want to delete only the lines in a2 which are in a1 and final output of a2 should be a2 chire hcm (6 Replies)
Discussion started by: Pradeep_1990
6 Replies

4. Shell Programming and Scripting

Selected lines from a file based on another file

Hello, I am using Awk in Ubuntu 12.04 First file: I have a file like this: SNP1 1 198.2 SNP2 1 124.5 SNP3 1 124.4 . . . Second file: I have another file like this: SNP2 SNP5 SNP10 . . . I want to create a third file like my first file but keeping ONLY the SNPs that... (8 Replies)
Discussion started by: Homa
8 Replies

5. Shell Programming and Scripting

Reading selected lines from a param file

Hi all, I have a question for the Gurus. I apologize if this has bee shared before but I couldn't find the link. I am trying to read parameters from an external parameter file. What I m trying to achieve is read selected lines from an external parameter file into the script. for eg my param... (4 Replies)
Discussion started by: maverick1947
4 Replies

6. Shell Programming and Scripting

trying to print selected fields of selected lines by AWK

I am trying to print 1st, 2nd, 13th and 14th fields of a file of line numbers from 29 to 10029. I dont know how to put this in one code. Currently I am removing the selected lines by awk 'NR==29,NR==10029' File1 > File2 and then doing awk '{print $1, $2, $13, $14}' File2 > File3 Can... (3 Replies)
Discussion started by: ananyob
3 Replies

7. Shell Programming and Scripting

Order file by lines

My script(3 arguments $1 = folder,$2 extension,$3 string) should do the following things: -Enter in the folder of $1(if exists). -Put ls *.$2 > temp.txt ( I use a temp file to store the result of ls command and if $2 = txt in this file I'll have all the .txt files of the folder) -Now I want to... (2 Replies)
Discussion started by: Max89
2 Replies

8. Shell Programming and Scripting

reversing order of lines in a file

how can i reverse the line order in text files? (but total number of the lines is not constant ) for example i have a file like this: line1 line2 line3 . . lineN i wantto make it like this: lineN . . . line3 (26 Replies)
Discussion started by: gfhgfnhhn
26 Replies

9. Shell Programming and Scripting

Compare selected columns from a file and print difference

I have learned file comparison from my previous post here. Then, it is comparing the whole line. Now, i have a new problem. I have two files with 3 columns separated with a "|". What i want to do is to compare the second and third column of file 1, and the second and third column of file 2. And... (4 Replies)
Discussion started by: kingpeejay
4 Replies

10. Shell Programming and Scripting

print selected lines

Hi everybody: I try to print in new file selected lines from another file wich depends on the first column. I have done a script like this: lines=( "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "21" "31" "41" "51" "55" "57" "58" ) ${lines} for lines in ${lines} do awk -v ... (6 Replies)
Discussion started by: tonet
6 Replies
Login or Register to Ask a Question