Print the 2nd line everytime after defined pattern is found.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print the 2nd line everytime after defined pattern is found.
# 1  
Old 01-18-2011
Print the 2nd line everytime after defined pattern is found.

Hi,

I have a text file similar to the example below and I want to print the second line every time after the "--------------------------" pattern is found. The pattern is a fixed length of - characters.

Example of input;
Code:
1    --------------------------
2
3    39184018234
4    1093841028934
5    9128348
9    8429834
10
11  --------------------------
23
24  4984598
25  jma8re8
26  98rn8r
27  9n8re8
28
29  --------------------------
30
31  lldskfgkl
32  iriro04049
33  490494
34  lkaskiooir

Desired output;
Code:
3    39184018234
24  4984598
31  lldskfgkl


Thank you!

Last edited by radoulov; 02-22-2011 at 07:42 AM.. Reason: Code tags, please!
# 2  
Old 01-18-2011
Code:
awk '/------------/{nr=NR;next} NR==nr+2' inputFile

# 3  
Old 01-18-2011
Another one:
Code:
awk '/---/{c=2}c--==0' file

# 4  
Old 01-18-2011
Thanks guys!
# 5  
Old 01-18-2011
thru sed..
Code:
sed -n '/----/{N;x;N;s/.*\n//p}' inputfile > outfile

# 6  
Old 01-18-2011
Code:
sed -n '/---/{n;n;p;}' infile

# 7  
Old 02-22-2011
I would like to see whether it is possible to expand my output further.

I have a text file similar to the example below and I want to print the second line every time after the "--------------------------" pattern is found. The pattern is a fixed length of - characters. In addition I would also like to add a word with a sequential number e.g. Item 1, Item 2, Item 3


Input;
Code:
1 --------------------------
2
3 39184018234
4 1093841028934
5 9128348
9 8429834
10
11 --------------------------
23
24 4984598
25 jma8re8
26 98rn8r
27 9n8re8
28
29 --------------------------
30
31 lldskfgkl
32 iriro04049
33 490494
34 lkaskiooir


Desired output;
Code:
Item 1
3 39184018234
Item 2
24 4984598
Item 3
31 lldskfgkl


Last edited by Franklin52; 02-22-2011 at 07:31 AM.. Reason: Please use code tags, thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

2. Shell Programming and Scripting

Print character after pattern found

Hi Gurus, i need your help to create a script the will print a characters after the pattern was found. Sample lines are below: My birthday:"1977-16-07", My birthday:"1975-16-07" My birthday:"1970-16-07". My patter should be "birthday:", then i want to print the following characters which... (18 Replies)
Discussion started by: scripter123
18 Replies

3. Shell Programming and Scripting

If first pattern is found, look for second pattern. If second pattern not found, delete line

I had a spot of trouble coming up with a title, hopefully you'll understand once you read my problem... :) I have the output of an ldapsearch that looks like this: dn: cn=sam,ou=company,o=com uidNumber: 7174 gidNumber: 49563 homeDirectory: /home/sam loginshell: /bin/bash uid: sam... (2 Replies)
Discussion started by: samgoober
2 Replies

4. Shell Programming and Scripting

Script to compare pattern and print a different pattern in each line

Hi, I am writing a shell script to parse some files, and gather data. The data in the files is displayed as below. .......xyz: abz: ......qrt: .... .......xyz: abz: ......qrt: ... I have tried using awk and cut, but the position of these values keep changing, so I wasn't able to get... (2 Replies)
Discussion started by: Serena
2 Replies

5. Shell Programming and Scripting

Copy/print all lines between pattern is found in .log files

Hi, I have a folder with multiple (< 33) .log files. And I have to copy the lines between two patterns from all the .log files to a new file. (script file with a loop?) Thanks in advance. 1.log ... .. xx1> begin ... .. .. >>> Total: 2 Alarms .. .. (17 Replies)
Discussion started by: AK47
17 Replies

6. Shell Programming and Scripting

awk to print all lines after a pattern is found

Is there a way with aw to print all lines after a string is found There is a file like this ....... ........ 2012/19/11 :11.58 PM some data lne no date 2012/19/11 :11.59 PM some other data 2012/20/11 :12.00 AM some other data some line without dates some more lines without dates... (8 Replies)
Discussion started by: swayam123
8 Replies

7. Shell Programming and Scripting

Print characters till the next space when the pattern is found

i have a file which contains alphanumeric data in every line. what i need is the data after certain pattern. the data after the pattern is not of fixed length so i need the data till the space after the pattern. Input file: bfdkasfbdfg khffkf lkdhfhdf pattern (datarequired data not required)... (2 Replies)
Discussion started by: gpk_newbie
2 Replies

8. Shell Programming and Scripting

Append next line to previous line when one pattern not found

Hi, I need help for below scenario.I have a flat file which is having records seperated by delimiters which will represent each record for oracle table.My Control file will consider each line as one record for that table. Some of the lines are aligned in two/three lines so that records are... (4 Replies)
Discussion started by: kannansr621
4 Replies

9. Shell Programming and Scripting

print next word after found pattern

Hi all, I'd like to print the next word after a found pattern. example text: word1 word2 word3 word4 pattern word5 pattern word1 word2 word3 word4 word1 word2 pattern word4 basiclly the word after pattern. Thanks (9 Replies)
Discussion started by: stinkefisch
9 Replies

10. Shell Programming and Scripting

How to replace a line below where the pattern found

Hi All, I have a file say abc.xml. In this file, I need to search for a pattern “SAP_GATEWAY_HOST”; if this pattern found and the next line also contain the pattern “nwprc03.cos” then I need to replace this pattern “nwprc03.cos” with some other pattern “nwdrc03.apjp”. $ cat abc.xml... (3 Replies)
Discussion started by: Ritesh.patni84
3 Replies
Login or Register to Ask a Question