Link to home
Start Free TrialLog in
Avatar of mruff
mruff

asked on

unix replace line feeds

Hi
with a unix command I want to replace all occurrences in a text file
1. LF by CRLF
2. LF by NL
3. CRLF by NL
5. NL by LF

Where
Abbreviation      hexvalue      decvalue      Escape sequence
LF                      0A                10               \n
CR LF                0D 0A      13 10      \r\n
CR                        0D              13              \r
RS                       1E                30      
LF+CR                 0A 0D      10 13      \n\r
NL                        15                  21      \025


Many thanks
Avatar of David Favor
David Favor
Flag of United States of America image

Unsure if this is exactly what you're after. If you do the above, then you may end up with problems.

LF -> CRLF (#1) -> NL -> (#3) -> LF (#5)

So the above setup of transforms, converts LF back to LF.

Maybe clarify a bit more what you're trying to do... or maybe you're saying to only apply one transform per line + if the transform occurs, then abandon any further transforms.
Avatar of skullnobrains
skullnobrains

here is how to achieve the first : change occurrences of LF into CRLF
sed 's/\x0A/\r\n/g'

Open in new window


basically it's sed s/TRANSFORMWHAT/TRANSFORMINTO/

you can easily extrapolate the others given you have the hex codes ( which you can also use in the transform into part )

as a side note, dos2unix unix2dos tofrodos can also be helpful
Avatar of mruff

ASKER

Hi thx
How do I replace  LF by NL, I've tried
sed -i ':a;N;$!ba;s/\n/x15/g' myfile.txt
sed -i ':a;N;$!ba;s/\n/\025/g' myfile.txt

none of them worked
thx
Try this:
  cat myfile.txt | sed -e 's/WHATTOREPLACE/REPLACEINTO/g' > mynewfile.txt
e.g.
  cat myfile.txt | sed -e 's/\x0A/\r\n/g' > newfile.txt
Avatar of mruff

ASKER

Hi Hanno
Thx yes replacing LF by CRLF that works

BUT replacing LF by NL does not work, I tried to state LN by it's hex and escape sequence, x15 in this case x15 was written to the result file, and by \025 and in this cas 025 was written to the output file, instead of the NL char.
In Unix or in C language:
CR                 \r                    short for "r"eturn
LF                  \n                   short for "n"ew line
CRLF             \r\n
Avatar of mruff

ASKER

Hi Hanno
I know, I was just want to replace LF by NL *hex x15, escape sequence  \025
NOT by LF or CRLF this works
ASKER CERTIFIED SOLUTION
Avatar of Hanno P.S.
Hanno P.S.
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial