Replacing multiple line patterns with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing multiple line patterns with awk
# 1  
Old 01-14-2015
Replacing multiple line patterns with awk

Hi forum,

Can you please help me understand how to look for and replace the below pattern (containing line breaks) and return a new result?

Rules: Must match the 3 line pattern and return a 1 line result.

I have found solutions with sed, but it seems that sed installed in my system is very limited so its not working. Are there other programs can be used to accomplish this result?

Code:
===============================================================
===============================================================
= Exit

Desired:

Code:
= Exit

Appreciate the help.
# 2  
Old 01-14-2015
sed? What is the pattern on the third line? Otherwise try something like:
Code:
sed -n '/===/{N;/===.*\n===/{n;p;};}' file

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 01-14-2015
try this perl solution instead of sed... will depend on the exact pattern..

Code:
 
 perl -0pe 's/===============================================================\n===============================================================\n= Exit/= Exit/g' file

This User Gave Thanks to senhia83 For This Post:
# 4  
Old 01-15-2015
This should work with all sed:
Code:
sed '
/^====================/ {
N;/\n====================/ {
N;s/.*\n= Exit/= Exit/
}
}
' file

This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 01-15-2015
This one returned an error:

Code:
sed -n '/===/{N;/===.*\n===/{n;p;};}' file
sed: There are too many '{'.

Other 2 options worked fine.

Thank you guys.

Last edited by Scrutinizer; 01-15-2015 at 03:28 PM.. Reason: code tags
# 6  
Old 01-15-2015
Yes, some older sed's (HPUX?) cannot handle semicolons and need newlines instead:
Code:
sed -n '
/===/ {
  N
  /===.*\n===/ {
    n
    p
  }
}
' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep multiple patterns(file) and replace whole line

I am able to grep multiple patterns which stored in a files. However, how could we replace the whole line with either the pattern or new string? For example: pattern_file: *Info in the () is not part of the pattern file. They are the intended name to replace the whole line after the pattern... (5 Replies)
Discussion started by: wxboo
5 Replies

2. Shell Programming and Scripting

Replacing matched patterns in multiple files with awk

Hello all, I have since given up trying to figure this out and used sed instead, but I am trying to understand awk and was wondering how someone might do this in awk. I am trying to match on the first field of a specific file with the first field on multiple files, and append the second field... (2 Replies)
Discussion started by: karlmalowned
2 Replies

3. Shell Programming and Scripting

Multiple patterns for awk script

Hi, I'm getting stuck when supplying multiple patterns for the below code: awk -F, ' .. .. if ($0 ~ pattern) { .. .. } .. .. ' pattern='$ROW' input_file for the same code I'm trying to supply multiple patterns as given below: awk -F, ' .. .. if( ($0 ~ pattern) && ($0 ~... (6 Replies)
Discussion started by: penqueen
6 Replies

4. Shell Programming and Scripting

Matched multiple patterns that could be in a same line

Hi, I need help to match pattern started with "RW" in file 1 and with pattern in $1 in file 2 as follows:- File 1 BH /TOTAL=466(423); /POSITIVE=300(257); /UNKNOWN=25(25); BH /F_P=141(141); /F_N=136; /P=4; CC /TAX=!?; /MAX-R=2; CC /VER=2; RW P9610, AR_BSU , T; PAE25, AE_E57... (10 Replies)
Discussion started by: redse171
10 Replies

5. Shell Programming and Scripting

Join common patterns in multiple lines into one line

Hi I have a file like 1 2 1 2 3 1 5 6 11 12 10 2 7 5 17 12 I would like to have an output as 1 2 3 5 6 10 7 11 12 17 any help would be highly appreciated Thanks (4 Replies)
Discussion started by: Harrisham
4 Replies

6. Shell Programming and Scripting

Searching multiple patterns using awk

Hello, I have the following input file: qh1adm 20130710111201 : tp import all QH1 u6 -Dsourcesystems=BFI,EBJ qh1adm 20130711151154 : tp import all QH1 u6 -Dsourcesystems=BFI,EBJ qx1adm 20130711151154 : tp count QX1 u6 -Dsourcesystems=B17,E17,EE7 qh1adm 20130711151155 : tp import all... (7 Replies)
Discussion started by: kcboy
7 Replies

7. Shell Programming and Scripting

how to form Records[multiple line] between two known patterns

file contents looks like this : #START line1 of record1 line2 of record1 #END #START line1 of record2 line2 of record2 line3 of record2 #END #START line1 of record3 #END my question how should i make it a records between #START and #END . willl i be able to get the contents of the... (5 Replies)
Discussion started by: sathish92
5 Replies

8. UNIX for Dummies Questions & Answers

best method of replacing multiple strings in multiple files - sed or awk? most simple preferred :)

Hi guys, say I have a few files in a directory (58 text files or somthing) each one contains mulitple strings that I wish to replace with other strings so in these 58 files I'm looking for say the following strings: JAM (replace with BUTTER) BREAD (replace with CRACKER) SCOOP (replace... (19 Replies)
Discussion started by: rich@ardz
19 Replies

9. UNIX for Dummies Questions & Answers

AWK: Multiple patterns per line

Hi there, We have been given a bit of coursework using awk on html pages. Without giving too much away and risking the wrath of the plagerism checks, I can say we need to deal with certain html elements. There may be several of these elements on one line. My question is, if there are more... (1 Reply)
Discussion started by: Plavixo
1 Replies

10. Shell Programming and Scripting

Perl: Match a line with multiple search patterns

Hi I'm not very good with the serach patterns and I'd need a sample how to find a line that has multiple patterns. Say I want to find a line that has "abd", "123" and "QWERTY" and there can be any characters or numbers between the serach patterns, I have a file that has thousands of lines and... (10 Replies)
Discussion started by: Juha
10 Replies
Login or Register to Ask a Question