Kasperian Moving Parts

kinda like Batman, but with a wife and 3 kids

Awksome Stuff, Dude!


I had need to convert some files today from being newline-separated (one line per file, key-value pairs) to ‘###’-separated. In other words, instead of this:


I needed this:


My first inclination was to try using tr solely like this:

cat $file | tr “\n” “###”

This resulted, however, in only one “#” being used to join the lines. Not quite what I wanted. My next thought was sed:

cat $file | sed ‘s,\n,###,g’

But that didn’t work at all. So, my good friend Mr. Google and I were just starting to look into the deeper mysteries of sed when I found this little newsgroup posting which opened my eyes to some of the magic of awk:

cat $file | awk ‘{printf(“%s###”, $0);} END {printf(“\n”)}’

That’s just sweet! Incidentally, I did figure out how to do it with a combination of tr and sed (convoluted because of the missing \n at the end):

F=$(cat $f | tr “\n” “_” ); echo $F | sed ‘s,_,###,g’

But it’s not nearly as pretty. =;)

Author: Jason 'vanRijn' Kasper

My name is Jason 'vanRijn' Kasper. I am the ring leader of the amazing Kasper family. I am unashamedly a Christian Nerd. These are our stories....

Leave a Reply

Required fields are marked *.


This site uses Akismet to reduce spam. Learn how your comment data is processed.