Extracting line matching a phrase and then the next lines after it


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting line matching a phrase and then the next lines after it
# 1  
Old 11-19-2009
Extracting line matching a phrase and then the next lines after it

Hi all,

I was wondering if someone could tell me a way to extract from a file lines where you search for a phrase and then also extract the next X lines after it (i.e. take a block of text from the file)?

Code:
Example

{
    id=123
    time=10:00:00
    date=12/12/09

    {
     ........
    }
}

{
    id=123
    time=10:00:00
    date=12/12/09

    {
     ........
    }
}

If possible i want to extract the id, time and date block....

Also is there a way also to extract text between 2 tags? such as { and } in my example?
# 2  
Old 11-19-2009
You can use grep -Ax if your grep support this option, awk or sed shell script, etc...
The answer and the solution depend on your required output.
# 3  
Old 11-19-2009
Quote:
Originally Posted by danmero
You can use grep -Ax if your grep support this option, awk or sed shell script, etc...
The answer and the solution depend on your required output.
Unfortunately I do not have the grep -A option as I am on older systems.

All i want is to extract the information, at this stage no formatting is required so:

Code:
id=123
time=10:00:00
date=12/12/09
<SPACE>
id=123
time=11:00:00
date=12/12/09

....
# 4  
Old 11-19-2009
Try
Code:
awk '/id/{f=1}/date/{print $0 ORS;f=0}f' FILE

# 5  
Old 11-19-2009
1. Print id, and 2 lines following it.

Code:
sed -n '/id/,+2p' FILE

  • sed -n : suppress default print,
  • /id/ : match pattern 'id'
  • +2 : next 2 lines
  • p: print

2. print from { to }

Code:
sed -n '/{/,/}/p' FILE

  • sed -n : suppress default print,
  • /{/,/}/ : match from { to }
  • p : print

But the 2nd one will print everything, as all your text is enclosed with { and }
# 6  
Old 11-19-2009
Quote:
Originally Posted by thegeek
1. Print id, and 2 lines following it.

Code:
sed -n '/id/,+2p' FILE

This is the similar to
Code:
awk '/id/,/date/' FILE

however your solution don't insert blank line between record blocks Smilie see OP requirement.
# 7  
Old 11-20-2009
Quote:
Originally Posted by danmero
however your solution don't insert blank line between record blocks Smilie see OP requirement.
Code:
sed -n '/id/{p;n;p;n;G;p}' t1

This can print the 2 lines from id, and with blank line following as asked for...

But, giving two regex /id/,/date/ cannot satisfy OPs requirement as printing next X lines, if date comes before/after 2 lines...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Extracting the two lines where the first line is matched

Hi, If I have a file of something like @hg19_gold_AL122127.6-131160 GCTTCATCATGCATGGATAGGCTGGCGCCTTTCCTGAGGCCATATGCCGATGGATATG @hg19_gold_AL122127.6-131159 CTTTAATATTTCCGCCACCATCCTGAGTGAATCCCAGCAAGGACAGTCTTTGGGGATT @hg19_gold_AL122127.6-131158... (4 Replies)
Discussion started by: jyu429
4 Replies

2. Shell Programming and Scripting

Compare file1 for matching line in file2 and print the difference in matching lines

Hello, I have two files file 1 and file 2 each having result of a query on certain database tables and need to compare for Col1 in file1 with Col3 in file2, compare Col2 with Col4 and output the value of Col1 from File1 which is a) not present in Col3 of File2 b) value of Col2 is different from... (2 Replies)
Discussion started by: RasB15
2 Replies

3. Shell Programming and Scripting

How to find a phrase and pull all lines that follow until the phrase occurs again?

I want to burst a report by using the page number value in the report header. Each section starts with *PAGE NO:* 1 Each section might have several pages, but the next section always starts back at 1. So I want to find the "*PAGE NO:* 1" value and pull all lines that follow until "*PAGE NO:* 1"... (4 Replies)
Discussion started by: Scottie1954
4 Replies

4. Shell Programming and Scripting

Insert lines above matching line with content from matching

Hi, I have text file: Name: xyz Gender: M Address: "120_B_C; ksilskdj; lsudlfw" Zip: 20392 Name: KLM Gender: F Address: "65_D_F; wnmlsi;lsuod;,...." Zip:90233I want to insert 2 new lines before the 'Address: ' line deriving value from this Address line value The Address value in quotes... (1 Reply)
Discussion started by: ysrini
1 Replies

5. Shell Programming and Scripting

Deleting a matching string(line) which is also in other lines

Hi, i need help with my shell script I have a file input.txt containing the following contents /. /usr /usr/share /usr/share/doc /usr/share/doc/wine /usr/share/doc/wine/copyright /usr/share/doc/wine/changelog.Debian.gz I need output as /usr/share/doc/wine /usr/share/doc/wine/copyright... (3 Replies)
Discussion started by: Amit0991
3 Replies

6. Shell Programming and Scripting

Extracting lines after nth LINE from an output

Hi all, Here is my problem for which i am breaking my head for past three days.. I have parted command output as follows.. Model: ATA WDC WD5000AAKS-0 (scsi) Disk /dev/sdb: 500GB Sector size (logical/physical): 512B/512B Partition Table: msdos Number Start End Size Type ... (3 Replies)
Discussion started by: selvarajvs
3 Replies

7. Shell Programming and Scripting

print range of lines matching pattern and previous line

Hi all, on Solaris 10, I'd like to print a range of lines starting at pattern but also including the very first line before pattern. the following doesn't print the range starting at pattern and going down to the end of file: cat <my file> | sed -n -e '/<pattern>{x;p;}/' I need to include the... (1 Reply)
Discussion started by: siriche
1 Replies

8. Shell Programming and Scripting

Extracting a string matching a pattern from a line

Hi All, I am pretty new to pattern matching and extraction using shell scripting. Could anyone please help me in extracting the word matching a pattern from a line in bash. Input Sample (can vary between any of the 3 samples below): 1) Adaptec SCSI RAID 5445 2) Adaptec SCSI 5445S RAID 3)... (8 Replies)
Discussion started by: jharish
8 Replies

9. Shell Programming and Scripting

help extracting a matching pattern and next lines of match

Hi there, i'm having some problems just making an awk script (i've tried this way, but other way can be posible for sure), for the next file file.txt <register> <createProfile> <result>0</result> <description><!]></description> <msisdn>34661461174</msisdn> <inputOmvID>1</inputOmvID>... (6 Replies)
Discussion started by: vicious
6 Replies

10. Shell Programming and Scripting

Logfile - extracting certain lines to concatenate into 1 line

I've got a log file from automatic diagnostic runs. The log file is appended to each time an automatic log is run. I'd like to just pull certain lines from each run in the log file, and concatenate them into 1 comma delimited line (for export into excel or an html table). Each diagnostic run... (3 Replies)
Discussion started by: BecTech
3 Replies
Login or Register to Ask a Question