Replacing LineFeed \x0a with SED

9,356

The sed utility is line orientated. A line is read and put into the pattern space (the pattern space doesn't contain a \n). The sed actions are applied to the pattern space and the line is then written out, with a \n appended. This is why it's not doing what you expect.

If you want to remove all of the new lines in a file then you can do this

sed ':a;N;$!ba;s/\n//g' file

This effectively loops over the file reading lines in and appending them to the pattern space until the last line is reached when the whole pattern space is acted upon to remove the \n's.

  • :a creates a label.
  • N reads the next line into the patern space
  • $! if not the last line ba branch to a
  • s//n//g substitute all /g occurrences of \n with nothing.

Depending on your file size this may not work as there may be limits to the size of the pattern space. It's generally more portable to use tr to do this

tr -d '\n' <file  
Share:
9,356

Related videos on Youtube

IRousso
Author by

IRousso

I do things with linux!

Updated on September 18, 2022

Comments

  • IRousso
    IRousso over 1 year

    I have to delete lots of Linefeeds (Hex \x0a) in a logfile.

    I just have sed to solve the problem. It is a bit complicated I know..

    Do you have idea how to solve the problem?

    Here is an example textfile: http://s000.tinyupload.com/index.php?file_id=05715208147126674999

    hexdump -C hexprob.txt
    00000000 45 0a 69 0a 6e 0a 66 0a 61 0a |E.i.n.f.a.|

    I use the following code to remove the 'E':

    sed -r 's/\x45//g' hexprob.txt | hexdump -C

    00000000 0a 69 0a 6e 0a 66 0a 61 0a |.i.n.f.a.|

    But if I want to remove the '\x0a' it doesnt work:

    sed -r 's/\x0a//g' hexprob.txt | hexdump -C

    00000000 45 0a 69 0a 6e 0a 66 0a 61 0a |E.i.n.f.a.|

    Do you know what to do? I just dont know why i cant replace or delete it the same way like any other hex value?

    Thank you so much! Fake4d

  • Dennis Williamson
    Dennis Williamson almost 10 years
    Another way with sed: sed -n 'H;${g;s/\n//g;p}'. In this version which saves the lines in hold space, sed does the looping for free without an explicit loop. One thing to note about using tr in this way: the final newline is also removed.