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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed: how to move matched pattern to end of previous line
# 1  
Old 01-01-2012
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 followed by a "<scripRef>" or "<note>" tag:

Code:
Now there is in Jerusalem by
the<scripRef>Neh 3:1</scripRef> sheep <i>gate</i> a pool, which is called
in<scripRef>John 19:13</scripRef>
Hebrew<note>I.e. Jewish Aramaic</note>
Bethesda<note>Some early mss read ...</note>, having five porticoes.

I want to move the words before the "<scripRef>" or "<note>" tags to the end of the previous line (prepending the word with a space), resulting in the following:

Code:
Now there is in Jerusalem by the
<scripRef>Neh 3:1</scripRef> sheep <i>gate</i> a pool, which is called in
<scripRef>John 19:13</scripRef> Hebrew
<note>I.e. Jewish Aramaic</note> Bethesda
<note>Some early mss read ...</note>, having five porticoes.

The word to be moved will consist of uppercase letters, lowercase letters, and/or single quotes (').

Can you please help? I am most comfortable with sed, but if awk or perl (or anything else) would be better, that's fine.

Thanks in advance for your help!

Josh
# 2  
Old 01-01-2012
Hi paroikoi,

One solution using 'Sed':
Code:
$ cat infile
Now there is in Jerusalem by
the<scripRef>Neh 3:1</scripRef> sheep <i>gate</i> a pool, which is called
in<scripRef>John 19:13</scripRef>
Hebrew<note>I.e. Jewish Aramaic</note>
Bethesda<note>Some early mss read ...</note>, having five porticoes.
$ sed -n '$! { /\n/! { N; s/\n\(.*\)\(<scripRef>\|<note>\)/ \1\n\2/; P; D } }; p' infile
Now there is in Jerusalem by the
<scripRef>Neh 3:1</scripRef> sheep <i>gate</i> a pool, which is called in
<scripRef>John 19:13</scripRef> Hebrew
<note>I.e. Jewish Aramaic</note> Bethesda
<note>Some early mss read ...</note>, having five porticoes.

Regards,
Birei
These 2 Users Gave Thanks to birei For This Post:
# 3  
Old 01-01-2012
awesome!
# 4  
Old 01-01-2012
Hi Birei,
Awesome! THANK YOU!

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

2. Shell Programming and Scripting

Sed: how to merge two lines moving matched pattern to end of previous line

hello everyone, im new here, and also programming with awk, sed and grep commands on linux. In my text i have many lines with this config: 1 1 4 3 1 1 2 5 2 2 1 1 1 3 1 2 1 3 1 1 1 2 2 2 5 2 4 1 3 2 1 1 4 1 2 1 1 1 3 2 1 1 5 4 1 3 1 1... (3 Replies)
Discussion started by: satir
3 Replies

3. Linux

Perl program to print previous set of lines once a pattern is matched

Hi all, I have a text data file. My aim here is to find line called *FIELD* AV for every record and print lines after that till *FIELD* RF. But here I want first 3 to four lines for very record as well. FIELD AV is some where in between for very record. SO I am not sure how to retrieve lines in... (2 Replies)
Discussion started by: kaav06
2 Replies

4. Shell Programming and Scripting

awk get matched line's previous line

hi everyone, a b in c d e f in g output is: a e so awk search for "in", then print out the matched line's previuos line. Please advice. (11 Replies)
Discussion started by: jimmy_y
11 Replies

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

6. Shell Programming and Scripting

awk script to move a line after the matched pattern line

I have the following text format in a file which lists the question first and then 5 choices after that the explanantion and finally the answer. 1.The amount of time it takes for most of a worker’s occupational knowledge and skills to become obsolete has been declining because of the... (2 Replies)
Discussion started by: nanchil_guy
2 Replies

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

8. Shell Programming and Scripting

extracting matched pattern from a line using sed

I am trying to pull certain pieces of data out of a line of a file that matches a certain pattern: The three pieces that I want to pull out of this line are the only occurrences of that pattern within the line, but the rest of the line is not consistent in each file. Basically the line is... (3 Replies)
Discussion started by: ellhef
3 Replies

9. Shell Programming and Scripting

sed: Find start of pattern and extract text to end of line, including the pattern

This is my first post, please be nice. I have tried to google and read different tutorials. The task at hand is: Input file input.txt (example) abc123defhij-E-1234jslo 456ujs-W-abXjklp From this file the task is to grep the -E- and -W- strings that are unique and write a new file... (5 Replies)
Discussion started by: TestTomas
5 Replies

10. Shell Programming and Scripting

Sed : identify a pattern and append a word at the end of a line

Hello to all, On aix, I want to identify a term on a line in a file and then add a word at the end of the line identified. I do not want the word to be added when the line contains the symbol "#". I use the following command, but it deletes the term identified then adds the word. #sed... (4 Replies)
Discussion started by: dantares
4 Replies
Login or Register to Ask a Question