How to insert line with between two consecutive lines that match special pattern?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to insert line with between two consecutive lines that match special pattern?
# 1  
Old 05-17-2012
How to insert line with between two consecutive lines that match special pattern?

I have following pattern in a file:

Code:
00:01:38 UTC  
abcd
00:01:48 UTC
00:01:58 UTC
efgh
00:02:08 UTC
00:02:18 UTC

and I need to change something like the following

Code:
00:01:38 UTC  
abcd
00:01:48 UTC
XXXX
00:01:58 UTC
efgh
00:02:08 UTC
XXXX
00:02:18 UTC

What would be the best way of doing it?

Thanks
Moderator's Comments:
Mod Comment Code tags for code, please.

Last edited by Corona688; 05-17-2012 at 12:43 PM..
# 2  
Old 05-17-2012
What the 'best' way is to do something is always open to debate, but you can do this using awk:

Code:
awk -v ADD="XXXX" -v PATTERN="[0-9][0-9]:[0-9][0-9]:[0-9][0-9] UTC" 'L { print L; if((L ~ PATTERN) && ($0 ~ PATTERN)) print ADD }; { L=$0 } END { print L }' inputfile > outputfile

Hopefully simple to modify to your exact needs by altering ADD and PATTERN.
# 3  
Old 05-17-2012
Code:
awk 'BEGIN{pat="^[0-9][0-9]:[0-9][0-9]:[0-9][0-9]( UTC)?$"} {if($0 ~ pat) {getline l; if(l ~ pat) print $0 RS "XXXX" RS l; else print $0 RS l} else print}' infile > outfile

# 4  
Old 05-17-2012
Corona688,

Thanks a lot for the quick response. Works like a charm!
# 5  
Old 05-17-2012
Code:
awk '{if ($1~/^[0-2][0-9]:[0-5][0-9]:[0-6][0-9]$/){if(p)print "XXXX"; p=1}else p=0}1' infile


Last edited by Scrutinizer; 05-17-2012 at 02:20 PM..
# 6  
Old 05-18-2012
Code:
sed -n '
/^[[:digit:]]/ {
p
n
/^[[:digit:]]/ {
i \
XXXXXX
p}
}
/^[^[:digit:]]/ p' filename

# 7  
Old 05-21-2012
Try as..
Code:
[mkt@michael]$ sed '/UTC$/{
> $!{
> N;/UTC$/s/\(.*\)\n\(.*\)/\1\nXXX\n\2/
> }
> }
> $s/.*/XXX\n&/' filename

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

find pattern matches in consecutive lines in certain fields-awk

I have a text file with many thousands of lines, a small sample of which looks like this: InputFile:PS002,003 D -1 5 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 6 6 -1 -1 -1 -1 0 509 0 PS002,003 PSQ 0 1 7 18 1 0 -1 1 1 3 -1 -1 ... (5 Replies)
Discussion started by: jvoot
5 Replies

2. Shell Programming and Scripting

awk to combine lines from line with pattern match to a line that ends in a pattern

I am trying to combine lines with these conditions: 1. First line starts with text of "libname VALUE db2 datasrc" where VALUE can be any text. 2. If condition1 is met then continue to combine lines through a line that ends with a semicolon. 3. Ignore case when matching patterns and remove any... (5 Replies)
Discussion started by: Wes Kem
5 Replies

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

4. Shell Programming and Scripting

Rearrange or replace only the second line after pattern match or pattern match

Im using the command below , but thats not the output that i want. it only prints the odd and even numbers. awk '{if(NR%2){print $0 > "1"}else{print $0 > "2"}}' Im hoping for something like this file1: Text hi this is just a test text1 text2 text3 text4 text5 text6 Text hi... (2 Replies)
Discussion started by: invinzin21
2 Replies

5. UNIX for Dummies Questions & Answers

Eliminate consecutive lines with the same pattern

Hi, I would like to know how to remove lines which has the same pattern as the next line through sed/awk. Stream 39 (wan stream 7) Stream 40 (wan stream 8) WINQ Counter 115955 1 1613 (BYTE) 11204787 163 ... (2 Replies)
Discussion started by: sarn_nat
2 Replies

6. UNIX for Dummies Questions & Answers

Finding the same pattern in three consecutive lines in several files in a directory

I know how to search for a pattern/regular expression in many files that I have in a directory. For example, by doing this: grep -Ril "News/U.S." . I can find which files contain the pattern "News/U.S." in a directory. I am unable to accomplish about how to extend this code so that it can... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

7. Shell Programming and Scripting

sed : match one pattern then the next consecutive second pattern not working

Ive used this snippet of code on a solaris box thousands of times. But it isnt working on the new linux box sed -n '/interface LoopBack0/{N;/ ip address /p;}' *.conf its driving me nuts !! Is there something Im missing ? (7 Replies)
Discussion started by: popeye
7 Replies

8. Shell Programming and Scripting

awk print pattern match line and following lines

Data: Pattern Data Data Data Data Data Data Data Data Data ... With awk, how do I print the pattern matching line, then the subsequent lines following the pattern matching line. Varying number of lines following the pattern matching line. (9 Replies)
Discussion started by: dmesserly
9 Replies

9. Shell Programming and Scripting

merging of 2 consecutive lines in a file for a specific pattern

Hi , I'm looking for a way to merge two lines only for a given pattern / condition. Input : abcd/dad + -49.201 2.09 -49.5 34 ewrew rewtre * fdsgfds/dsgf/sdfdsfasdd + -4.30 0.62 -49.5 45 sdfdsf cvbbv * sdfds/retret/asdsaddsa + ... (1 Reply)
Discussion started by: novice_man
1 Replies

10. Shell Programming and Scripting

Concatenating multiple lines to one line if match pattern

Hi all, I've been working on a script which I have hit a road block now. I have written a script using sed to extract the below data and pumped into another file: Severity............: MAJORWARNING Summary: System temperature is out of normal range. Severity............: MAJORWARNING... (13 Replies)
Discussion started by: phixsius
13 Replies
Login or Register to Ask a Question