Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

shell - removing new line character from incoming stream using sed

I am new to shell scripting and i am trying to remove new line character from each line using SED. this is what i have done so far :

printf "{new
to
linux}" | sed ':a;N;s/
/ /g'

removes only Ist new line character. I somewhere found this command :

printf "{new
to
linux}" | sed ':a;N;$!ba;s/
/ /g'

but it gives :"ba: Event not found."

if i do:

printf "{new
to
linux}" | sed ':a;N;s/
/ /g' | sed ':a;N;s/
/ /g'

then it gives correct output but i am looking for something better as i am not sure how many new character i will get when i run the script. incoming stream is from echo or printf or some variable in script. Thanks in advance

question from:https://stackoverflow.com/questions/10618798/removing-new-line-character-from-incoming-stream-using-sed

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To remove newlines, use tr:

tr -d '
'

If you want to replace each newline with a single space:

tr '
' ' '

The error ba: Event not found is coming from csh, and is due to csh trying to match !ba in your history list. You can escape the ! and write the command:

sed ':a;N;$!ba;s/
/ /g'  # Suitable for csh only!!

but sed is the wrong tool for this, and you would be better off using a shell that handles quoted strings more reasonably. That is, stop using csh and start using bash.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...