ilupper
ilupper

Reputation: 235

How to inject a line feed to replace a delimiter

/usr/bin/sed 's/,/\\n/g' comma-delimited.txt > newline-separated.txt

This doesn't work for me. I just get the ',' removed but the tokens are now just not delimited.

Upvotes: 1

Views: 3055

Answers (4)

shellter
shellter

Reputation: 37298

You must have an older version of sed, so you need to put a literal LF char in your substitution, i.e.

/usr/bin/sed 's/,/
/g' comma-delimited.txt > newline-separated.txt

You may even need to escape the LF, so make sure there are no white space chars after the last char '\'

/usr/bin/sed 's/,/\
/g' comma-delimited.txt > newline-separated.txt

Upvotes: 4

Peter
Peter

Reputation: 1

I tried the following, looks clumsy but does the work. Easy to understand. I use tr to do the replacement of the placeholder §. Only caveat is the placeholder, must be something NOT in the string(s).

ps -fu $USER | grep java | grep DML| sed -e "s/ -/§      -/g" | tr "§" "\n"

will give you an indented output of the commandline. DML is just some servername.

Upvotes: 0

happydog
happydog

Reputation: 11

on AIX7 answer #3 worked well: I need to insert a newline at the beginning of a paragraph so I can do grep -p to filter for 'mksysb' in the resulting 'stanza' lsnim -l | /usr/bin/sed 's/^[a-zA-Z/\^J&/' (actually the initial line had an escaped newline: lsnim -l | /usr/bin/sed 's/^[a-zA-Z/\ &/') recalling the command showed the ^J syntax ...

Upvotes: -1

potong
potong

Reputation: 58483

This might work for you:

echo a,b,c,d,e | sed 'G;:a;s/,\(.*\(.\)\)/\2\1/;ta;s/.$//'
a
b
c
d
e

Explanation:

  • Appends a newline to the pattern space. G
  • Substitute ,'s with the last character in the pattern space i.e. the \n :a;s/,\(.*\(.\)\)/\2\1/;ta
  • Remove the newline. s/.$//

Upvotes: 1

Related Questions