Remove improperly placed newlines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove improperly placed newlines
# 1  
Old 03-03-2010
Remove improperly placed newlines

Hello, there. I have a file that's a horrible, horrible mess. (Basically, it's an export from a firewall config.) The people who generated the file didn't think that putting a newline in the middle of a hostname would ever be a problem. It is.

Here's an example of the stuff in the file:
Code:
Entity name: Random_Subnet
02F7B97A-9DA1-4393-A7E3-7F367356190C
Network address
192.168.151.0
Random_Subnet
Netmask
255.255.255.0
Entity name: AccessApplications_Group
E486A584-E708-4DB3-809E-F5685F2215E5
Access internet applications
Network Entity
Random_Subnet,Random_Subnet2,Random_
Subnet3,Random_Subnet4,Random_Subnet
5,Random_Subnet6

The part with the network address I have parsed, all well and good. But the second "entity name" entry, where there's a group, you can see that it puts line breaks in random places. There's no rhyme or reason - there doesn't have to be a special character or anything, it just puts that there.

This is the string I used to get it to that point:
Code:
tr -d '\t' < VPhosts.htm | sed 's/<.*>//g' | sed '/^$/d' | egrep -v "false|Symantec Gateway Security|Description|Locked|Read only|Caption|Sequence Revision|MAC address|UUID"

There's further string manipulation I do afterwards to get it into a proper comma-separated value file. But right here is the step where I need help.

I want to take every line which has commas in it, remove the line break from the end, and concatenate it with the line after. I want this only to happen on lines with commas, everything else should remain intact. I've tried everything in my limited skill set with sed, awk, perl and tr of which I could think, and I can't get it right. The closest I came was:

Code:
for i in `cat test.out`; do if [[ $i == , ]]; then echo -n $i; else echo $i; fi; done > test2.out

But that also put in a newline wherever there was a space, so it didn't help, either.

Can someone give me a little one-liner that will do this? It sounds so simple, but it has gone way beyond my meager ability at scripting.
# 2  
Old 03-03-2010
Add empty line between Entiry session.
Code:
$ awk '{print (/^Entity name:/)?"\n"$0:$0} ' urfile

Entity name: Random_Subnet
02F7B97A-9DA1-4393-A7E3-7F367356190C
Network address
192.168.151.0
Random_Subnet
Netmask
255.255.255.0

Entity name: AccessApplications_Group
E486A584-E708-4DB3-809E-F5685F2215E5
Access internet applications
Network Entity
Random_Subnet,Random_Subnet2,Random_
Subnet3,Random_S

For your problem, can you provide more examples?
# 3  
Old 03-03-2010
This will find the lines with commas and create a single line as long as there are not more than three lines with commas in a row
Code:
sed '/,/N;s/\n//;/,/N;s/\n//' 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

Remove newlines and carriage return from a csv file using UNIX

I need to remove new lines and carriage returns from csv file. Is there anything other than sed and gwak by which we could achieve this ? Any suggestions ? (3 Replies)
Discussion started by: A_Gaddale
3 Replies

2. UNIX for Dummies Questions & Answers

Using find with awk to remove newlines

I want to list all html files present in a directory tree, the remove the newline and get one string with a space between files find /home/chrisd/Desktop/seg/geohtml/ -name '*.html' | awk BEGIN{FS=\r} '{print}' ---------- Post updated at 06:47 PM ---------- Previous update was at 06:25 PM... (5 Replies)
Discussion started by: kristinu
5 Replies

3. UNIX for Dummies Questions & Answers

Remove newlines

Hi buddy's my file are like this: s.no,name,band,sal 1,"suneel",,10 2,"bargav sand",,20 30," ebdug gil",,4 but i want s.no,name,band,sal 1,"suneel",,10 2,"bargav sand",,20 30,"ebdug gil",,4 any command or Shell script for this. please help me it's urgent to implement (33 Replies)
Discussion started by: Suneelbabu.etl
33 Replies

4. Shell Programming and Scripting

removing newlines after a certain word.

Hello! This is my first post here. I have a file with text like: A</title> B C</title> D I need to format it to: AB CD I am trying to use sed: sed 's/<//title>\n/ /g' file > newfile to delete </title> and the newline character, but the file is unchanged because there are... (3 Replies)
Discussion started by: DaytonCPS
3 Replies

5. Shell Programming and Scripting

sed remove newlines and spaces

Hi all, i am getting count from oracle 11g by spooling it to a file. Now there are some newline characters and blank spaces i need to remove these. pl provide me a awk/sed solution. the spooled file is attached. i tried this.. but not getting req o/p (6 Replies)
Discussion started by: rishav
6 Replies

6. Shell Programming and Scripting

Transpose with two newlines as delimiter

Hi Guys, I have data in a file as follows: a 1 2 3 b 4 5 6 a 6 7 8 a 4 7 9 b 6 8 5 c 0 8 7 So the number of rows which have data is variable (2 for the first group, one for the second group and three for the third group), but the delimiters between the... (10 Replies)
Discussion started by: npatwardhan
10 Replies

7. Shell Programming and Scripting

awk - need to remove unwanted newlines on match

Context: I need to remove unwanted newlines from a data file listing books and associated data. Here is a sample listing ( line numbers included ): 1 360762| Skip-beat! 14 /| 9781421517544| nb | 2008.| Nakamura, Yoshiki.| NAKAMUR | Kyoko Mogami followed 2 her true love Sho to Tokyo to... (6 Replies)
Discussion started by: Bubnoff
6 Replies

8. Shell Programming and Scripting

Newlines in shell variables

Hello, I'm trying to create a shell variable with newlines inside it, so that when I echo the variable and pipe it to, say, awk, it output with the newlines. Why is this so problematic? I frankly don't know, but BASH seems to be stripping my variable of newlines. Here's an example $ cat... (5 Replies)
Discussion started by: narcvs
5 Replies

9. Shell Programming and Scripting

Need to remove improperly formatted fortran output line from files, tried sed

I have been trying to remove some improperly formatted lines of output from fortran code I have been using. The problem is that I have some singularities in the math for some points that causes an incorrectly large value to be reported that exceeds the normal formating set in the code resulting in... (2 Replies)
Discussion started by: gillesc_mac
2 Replies

10. Solaris

Improperly formatted value for 'tftp-server' error while booting the target from LAN

Hi, I wish to install solaris 10 on a target machine (t1000) by using Jumpstart. I have configured by jumpstart environment for the same. When i boot the target with the option "boot net -v install" i get the following error... {0} ok boot net -v install Boot device: /pci@7c0/pci@0/network@4... (3 Replies)
Discussion started by: hemalsid
3 Replies
Login or Register to Ask a Question