Merging previous line using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Merging previous line using sed
# 1  
Old 07-15-2008
Merging previous line using sed

I've been trying to merge a previous line when I find a pattern using sed with no luck. Maybe someone can help me.

I am trying to find </Endtag>. Once I find that, I want to merge the previous line with the current line. I can do it with the next line with using N in sed but not previous. Anyone have a suggestion?

Ex.
Hello how are you.
</Endtag> <New> I am fine.</New>
This is a test.

Results should be:

Hello how are you. </Endtag><New> I am fine.</New>
This is a test.
# 2  
Old 07-15-2008
One way but with awk:

Code:
awk 'NR==1{printf $0;next}
/<\/Endtag>/{printf " " $0;next}
{printf "\n" $0}
END{print ""}
' file

Explanation:

NR==1{printf $0;next}
# print the first line without a newline

/<\/Endtag>/{printf " " $0;next}# If we have a match, print a space before the line and read next line

{printf "\n" $0} # print a newline before the other lines

END{print ""}
# print a newline at the end


If you get errors use nawk, gawk or /usr/xpg4/bin/awk on Solaris.

Regards
# 3  
Old 07-15-2008
Code:
awk '/<\/Endtag>/{print l,$0}{l=$0}' file

# 4  
Old 07-16-2008
Code:
sed -e :a -e '$!N;s/\n\(<\/Endtag>\)/\1/;ta' -e 'P;D' file

# 5  
Old 07-16-2008
I got a SED version, but the output is like this, it outputs the line before the </Endtag> twice, like:

Hello how are you.
Hello how are you.</Endtag> <New> I am fine.</New>
This is a test.

Code:
sed '/<\/Endtag>/ !{
        h
        }
/<\/Endtag>/ {
        H
        x
        s/\n//
        }
' file

# 6  
Old 07-16-2008
Almost work

Quote:
Originally Posted by Franklin52
One way but with awk:

Code:
awk 'NR==1{printf $0;next}
/<\/Endtag>/{printf " " $0;next}
{printf "\n" $0}
END{print ""}
' file

Explanation:

NR==1{printf $0;next}
# print the first line without a newline

/<\/Endtag>/{printf " " $0;next}# If we have a match, print a space before the line and read next line

{printf "\n" $0} # print a newline before the other lines

END{print ""}
# print a newline at the end


If you get errors use nawk, gawk or /usr/xpg4/bin/awk on Solaris.

Regards

Thanks, this way almost work. The problem I found out with this is the awk statement throws an error and quits when it runs into a line with a % sign in it.

Ex.

When it process through lines it stopped on the following line.

Approximately 75% of the Earth is covered with water.
</EndTag>

It quits printing at the above line as give the following error.

Not enough argument for the printf statement: Approximately 75% of the Earth is covered in water. sprintf (Approximately 75% of the Earth is covered in water.

Last edited by quixoticking11; 07-16-2008 at 04:44 PM.. Reason: Grammar
# 7  
Old 07-16-2008
fpmurphy, your ways seems to work.

Thanks

Last edited by quixoticking11; 07-16-2008 at 04:46 PM.. Reason: Grammar
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk or sed to print the character from the previous line after the regexp match

Hi All, I need to print the characters in the previous line just before the regular expression match Please have a look at the input file as attached I need to match the regular expression ^ with the character of the previous like and also the pin numbers and the output file should be like... (6 Replies)
Discussion started by: kshitij
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. Shell Programming and Scripting

Sed Comparing Parenthesized Values In Previous Line To Current Line

I am trying to delete lines in archived Apache httpd logs Each line has the pattern: <ip-address> - - <date-time> <document-request-URL> <http-response> <size-of-req'd-doc> <referring-document-URL> This pattern is shown in the example of 6 lines from the log in the code box below. These 6... (1 Reply)
Discussion started by: Proteomist
1 Replies

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

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

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

7. Shell Programming and Scripting

How to use sed to search for string and Print previous two lines and current line

Hello, Can anybody help me to correct my sed syntax to find the string and print previous two lines and current line and next one line. i am using string as "testing" netstat -v | sed -n -e '/test/{x;2!p;g;$!N;p;D;}' -e h i am able to get the previous line current line next line but... (1 Reply)
Discussion started by: nmadhuhb
1 Replies

8. Shell Programming and Scripting

awk;sed appending line to previous line....

I know this has been asked before but I just can't parse the syntax as explained. I have a set of files that has user information spread out over two lines that I wish to merge into one: User1NameLast User1NameFirst User1Address E-Mail:User1email User2NameLast User2NameFirst User2Address... (11 Replies)
Discussion started by: walkerwheeler
11 Replies

9. Shell Programming and Scripting

sed: appending alternate line after previous line

Hi all, I have to append every alternate line after its previous line. For example if my file has following contents line 1: unix is an OS line 2: it is open source line 3: it supports shell programming line 4: we can write shell scripts Required output should be line1: unix is an OS it is... (4 Replies)
Discussion started by: rish_max
4 Replies

10. Shell Programming and Scripting

Print previous, current and next line using sed

Hi, how can i print the previous, current and next line using sed? current line is the matching line. The following prints all lines containing 'Failure' and also the immediate next line cat $file | sed -n -e '/Failure/{N;p;}' Now, i also want to print the previous line too. Thanks,... (8 Replies)
Discussion started by: ysrinu
8 Replies
Login or Register to Ask a Question