awk to insert line previous to a pattern?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to insert line previous to a pattern?
# 1  
Old 04-24-2013
awk to insert line previous to a pattern?

I have a very long line with certain patters embedded in there. I need to be able to read that line, and when it encounters that pattern, create a new line.

I want the pattern to be the beginning of the new line.

I thought sed or awk could do this, but everything I try in sed gives me a "sed garbled" message.

Any thoughts?
# 2  
Old 04-24-2013
Code:
awk '{gsub("pattern", RS "pattern")}1' file

# 3  
Old 04-24-2013
Code:
sed 's/pattern/\
&/g' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to delete the previous line after pattern match?

Team, I am writing a shell script to perform few health checks of the system, where I need to delete the previous line in the text file after pattern match using sed (or) awk. Could you please help me out on this? For example, <td> <td style=color:green align=center> </td> </tr>... (6 Replies)
Discussion started by: Nagaraj R
6 Replies

2. Shell Programming and Scripting

How to print previous line of multiple pattern matched line?

Hello, I have below format log file, Comparing csv_converted_files/2201/9747.1012H67126.5077292103609547345.csv and csv_converted_files/22019/97447.1012H67126.5077292103609547345.csv Comparing csv_converted_files/2559/9447.1012H67126.5077292103609547345.csv and... (6 Replies)
Discussion started by: arvindshukla81
6 Replies

3. Shell Programming and Scripting

Insert certain field of matched pattern line above pattern

Hello every, I am stuck in a problem. I have file like this. I want to add the fifth field of the match pattern line above the lines starting with "# @D". The delimiter is "|" eg > # @D0.00016870300|0.05501020000|12876|12934|3||Qp||Pleistocene||"3 Qp Pleistocene"|Q # @P... (5 Replies)
Discussion started by: jyu3
5 Replies

4. Shell Programming and Scripting

Insert new pattern in newline after the nth occurrence of a line pattern - Bash in Ubuntu 12.04

Hi, I am getting crazy after days on looking at it: Bash in Ubuntu 12.04.1 I want to do this: pattern="system /path1/file1 file1" new_pattern=" data /path2/file2 file2" file to edit: data.db - I need to search in the file data.db for the nth occurrence of pattern - pattern must... (14 Replies)
Discussion started by: Phil3759
14 Replies

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

6. Shell Programming and Scripting

sed: how to move matched pattern to end of previous line

Hello, I'm new to this forum. I've been doing a lot of sed work lately and have found many useful tips on this forum. I've hit a roadblock in a project, though, and could really use some help. I have a text file with many lines like the following, i.e., some lines begin with a single word... (3 Replies)
Discussion started by: paroikoi
3 Replies

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

8. Shell Programming and Scripting

Sed: Working on a line Previous to a pattern.

Hello everyone, I am working with some train time tables, and i have hit a bit of a road block. Using grep/sed i have done a reasonable job of parsing the html into comma delimited format, but NJ transit prints The Track number and status on a new line, and I would much prefer it all on a... (6 Replies)
Discussion started by: mussen
6 Replies

9. Shell Programming and Scripting

Printing previous line based on pattern using sed

Hi, I have a written a shell script to get the previous line based on the pattern. For example if a file has below lines: ---------------------------------------------- #UNBLOCK_As _per #As per 205.162.42.92 #BLOCK_As_per #----------------------- #input checks abc.com... (5 Replies)
Discussion started by: Anjan1
5 Replies

10. UNIX for Dummies Questions & Answers

return previous line for pattern match

Hi, Need some idea on file processing, I have file like below, Processing al sources ... ...No value found : CHECK. Completed comparing all sources. Comparing schedulers... Processing al targets ... ...No value found : From above I need to extract the line where "No value... (4 Replies)
Discussion started by: braindrain
4 Replies
Login or Register to Ask a Question