[ale] sed & newline removal

Byron A Jeff byron at cc.gatech.edu
Thu Apr 18 12:00:47 EDT 2002


> 
> Hey all,
> I'm working with sed to create a .csv file from a list of file in a =
> directory.  Each of those files contains configuration values like look =
> like this:
> 
> name=3Dvalue
> name2=3Dvalue2
> 
> now I've gotten to a point where commas are placed at the end of the =
> line for each name/value pair by using the following:
> 
> cat * | sed -e 's/$/,/3'
> 
> I'd now like to take 3 lines and make them one line, such as:
> 
> name1=3Dvalue1, name2=3Dvalue2, name3=3Dvalue3
> 
> name4=3Dvalue4, name5=3Dvalue5, name6=3Dvalue6
> 
> By what I've read, the following should work:
> 
> cat * | sed -e ':!$N;s/\n/,/3'
> 
> However, the results are less than optimistic.  Any thoughts on what I =
> might be doing incorrectly?  I'm thinking that it must have something to =
> do with the N operation takes multiple lines and places them into the =
> namespace for checking by sed. =20

Well the problem with sed is that it's line based, so the substitute won't
find a newline because no line contains one.

Consider using the 'tr' command. Since it is character based and not line
based, it is no such problems with newlines. the following command:

tr '\n' ' '

will change all of the newlines into spaces.

Since you already have a sed script to add the commas, you can add another
command to add a different ending character on every third line ('@' for
the same of argument). You can then do the grouping with the following:

tr '\n' ' ' | tr '@' '\n'

Leaving only the cleanup detail of the deleting the leading space from the 
front of every line except the first.

Hope this helps,

BAJ

---
This message has been sent through the ALE general discussion list.
See http://www.ale.org/mailing-lists.shtml for more info. Problems should be 
sent to listmaster at ale dot org.






More information about the Ale mailing list