sed ksh remove newline between 2 containers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed ksh remove newline between 2 containers
# 1  
Old 12-18-2008
Bug sed ksh remove newline between 2 containers

If we assume that each line between the {} container is an XML document then What I want to remove the newline character from all lines within each container to have one XMDL document per line I wrote a bit of sed after trawling the web:

e.g.

#!/bin/sed -nf

H
/}/ {
x
s/\n//g
p
}

This process is not quite behaving how I expect in that some of the lines are mixed up between the containers

File before being passed to sed

{dwfglsdfsdfsdfsdfsdfsdfsdfsdfsdf\n
dgdgdgdgdgdgdgdgdgdgdg\n
dfgsdfgsdfgsdfg}\n
\n
{sdgfsdgggggggggggggggg\n
ggggggggggggggggggggg\n
gggggggggggggggggggggsdf\n
d}\n
\n
{asdfasdfasdfasd\n
asdfasdfasdfasdf\n
sfasdfasdfasdf\n
sdfasdfasdfasdf}\n
\n

File after sed (is what I am hoping for with the only \n after the end }):

{dwfglsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfdgdgdgdfgsdfgsdfgsdfg}\n
{sdgfsdgggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggsdfd}\n
{asdfasdfasdfasdasdfasdfasdfasdfsfasdfasdfasdfsdfasdfasdfasdf}\n

what I am getting is:

{dwfglsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfdgdgdgdgdgdgdgdgdgdgdg
dfgsdfgsdfgsdfg}\n
{sdgfsdgggggggggggggggggggggggggggggggggggggggggggggggggsdfd}{asdfasdfasdfasdasdfasdfasdfasdfsfasdfa sdfasdfsdfasdfasdfasdf}\n

The lines are long so I would expect wrapping per one line but not to see a container }{ being embedded within a line without a newline after the enclosing }
# 2  
Old 12-18-2008
Try the following as your sed script... and just use sed -f (no -n option)

Code:
/[{]/b a
b
:a
/[}]/ {
s/\n//
b
}
s/\n//
N
b a

# 3  
Old 12-18-2008
sed ksh remove newline between 2 containers

nawk '
{
if ("$NF" != "" ) {
a=a" "$0 ;
}
if (!NF && a) {
print a ; a=""
}
} ' inputfiile
# 4  
Old 12-19-2008
Testing sed and nawk solution

Thanks guys,

I tested both solutions and the sed solution produced consistent results whereas the nawk solution was slightly inconsistant with each line in that in some cases it added a space to the start of some of the records.

Thanks for your help guys, much appreciated!

Jim
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed to remove newline chars based on pattern mis-match

Greetings Experts, I am in AIX; I have a file generated through awk after processing the input files. Now I need to replace or remove the new-line characters on all lines that doesn't have a ; which is the last character on the line. I tried to use sed 's/\n/ /g' After checking through the... (6 Replies)
Discussion started by: chill3chee
6 Replies

2. Shell Programming and Scripting

remove ] followed by newline in bash

I need to remove ] followed by newline character to convert lines like: line1] line2 into: line1line2 (4 Replies)
Discussion started by: locoroco
4 Replies

3. Shell Programming and Scripting

any savant ? using AWK/SED to remove newline character between two strings : conditional removal

I'd like to remove (do a pattern or precise replacement - this I can handle in SED using Regex ) ---AFTER THE 1ST Occurrence ( i.e. on the 2nd occurrence - from the 2nd to fourth occurance ) of a specific string : type 1 -- After the 1st occurrence of 1 string1 till the 1st occurrence of... (4 Replies)
Discussion started by: sieger007
4 Replies

4. Shell Programming and Scripting

remove newline between two string with sed command in unix shellscript

I have a file (test.dat) which contains data like this 459|199811047|a |b |shan kar|ooty| 460|199811047|a |bv |gur u|cbe| but I need it like: 459|199811047|a |b |shankar|ooty| 460|199811047|a |b |guru|cbe| While reading the data from this file, I don't want to remove newline from the end of... (4 Replies)
Discussion started by: jcrshankar
4 Replies

5. Shell Programming and Scripting

sed/awk remove newline

Hi, I have input file contains sql queries i need to eliminate newlines from it. when i open it vi text editor and runs :%s/'\n/'/g it provides required result. but when i run sed command from shell prompt it doesn't impact outfile is still same as inputfile. shell] sed -e... (6 Replies)
Discussion started by: mirfan
6 Replies

6. Shell Programming and Scripting

Newline charachter in Ksh-Linux

Hello, I'm trying to create a muliti value shell variable with newlines inside it, So that I can read the values of that variable individually line by line, but KSH seems to be stripping my variable of newlines in LINUX, but UNIX its working fine. Here's Example : String =... (3 Replies)
Discussion started by: Sanju1236
3 Replies

7. Shell Programming and Scripting

SED: how to remove newline after pattern?

Hi, I have the following XML not well-indented code: <hallo >this is a line </hallo> So I need to remove the newline. This syntax finds what I need to correct, but I don't know how to remove the newline after my pattern: sed 's/<.*$/&/' How can I subtract the newline after my... (1 Reply)
Discussion started by: nico.ben
1 Replies

8. Shell Programming and Scripting

how to remove all tabs and newline (\n)

how to remove all tabs and newline (\n) from the exicting file (2 Replies)
Discussion started by: pvr_satya
2 Replies

9. Shell Programming and Scripting

problem escaping newline in ksh

Hi, I did the below. $ print "\\n" $ I am curious, why does \\n give two new lines? I would have thought that the first \ would escape the second \, and so we'd get \n printed. But we didn't. Any ideas? Thanks. (7 Replies)
Discussion started by: JamesByars
7 Replies

10. Shell Programming and Scripting

Newline character not working for ksh

Newline character "\n" not working for ksh in linux AS 3.0 Command : $echo "Hi\nHi" $Hi\nHi $ Expected output : $echo "Hi\nHi" Hi Hi $ Can some help me on this Thanks in advance Sanish. (11 Replies)
Discussion started by: sanikv
11 Replies
Login or Register to Ask a Question