Find the answer to your Linux question:
Results 1 to 6 of 6
I know virtually no unix BUT I want to remove line feed \n from a file but only when NOT preceded by carriage return \r - this is a file ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2011
    Posts
    3

    Question Command to remove \n from Windows file but preserve \r \n


    I know virtually no unix BUT
    I want to remove line feed \n from a file but only when NOT preceded by carriage return \r - this is a file copied from windows. Our Oracle data load script needs to read the next record after \r \n but ignore \n embedded in text

    Steve Edmeades
    Last edited by oz; 09-16-2011 at 07:20 PM. Reason: only your name is permitted in manual user signatures

  2. #2
    Linux Newbie lugoteehalt's Avatar
    Join Date
    Jan 2004
    Posts
    232
    Quote Originally Posted by sedmeades View Post
    I know virtually no unix BUT
    I want to remove line feed \n from a file but only when NOT preceded by carriage return \r - this is a file copied from windows. Our Oracle data load script needs to read the next record after \r \n but ignore \n embedded in text

    Steve Edmeades
    This is probably to ridiculously misunderstand the problem but:
    Code:
    $ cat|sed 's/\\n//g'|sed 's/\\r/\\r\\n/g'
    The wholesome bread\r\n
    Of Mrs. Brag\n
    Fell to the ground\r\n
    As if twere dead\n
    The wholesome bread\r\n
    Of Mrs. Brag
    Fell to the ground\r\n
    As if twere dead
    cat just sends the four lines below through the pipe. The first sed removes the \n's and the second looks for \r and adds an \n to it.

    Discovered that grep \\\\ looks for \ - it's true.
    Last edited by lugoteehalt; 09-18-2011 at 06:00 AM.
    All power is violence; all power is evil.
    Money As Debt

  3. #3
    Just Joined!
    Join Date
    Sep 2011
    Posts
    3
    Thanks lugoteehalt, appreciate your help - I think you have grasped the issue. How do I do that operation on files - ie Take MYfile1 process it in the way you say and end up with a cleaned up file MYfile2. This would be a line I could include in a shell script.
    Thanks again
    Steve Edmeades - Cancer Research UK

  4. #4
    Linux Newbie lugoteehalt's Avatar
    Join Date
    Jan 2004
    Posts
    232
    sed 's/\\n//g' < MYfile1 | sed 's/\\r/\\r\\n/g' > MYfile2

    It has to actually have the characters \r and \n, you know - as written to work.

    It won't do anything to:

    The chief defect
    Of Henry King

    It has to be:

    The chief defect\n and stuff like that.
    Code:
    #! /bin/bash
    # Remove \n's but not \r\n's.
    
    MYfile1=$1
    sed 's/\\n//g' < "$MYfile1" | sed 's/\\r/\\r\\n/g' > MYfile2
    Seems to work. You'd run that with:

    $ bash progName.sh /path/to/MYfile1 or something.
    Last edited by lugoteehalt; 09-19-2011 at 12:24 PM.
    All power is violence; all power is evil.
    Money As Debt

  5. #5
    Just Joined!
    Join Date
    Sep 2011
    Posts
    3
    Thanks for giving me some more of your time on this,
    that did not quite work in my scenario, but after playing around a bit this did seem to do the trick:-
    cat file1 | tr -d '\n' | sed 's/\r/\r\n/g' > file2

  6. #6
    Linux Newbie lugoteehalt's Avatar
    Join Date
    Jan 2004
    Posts
    232
    You sure?
    Code:
    lugo@debian:~/Videos/Medicine$ echo "\r"|sed 's/\r/\r\n/g'
    \r
    lugo@debian:~/Videos/Medicine$ echo "\r"|sed 's/\\r/\\r\\n/g'
    \r\n
    lugo@debian:~/Videos/Medicine$
    tr's nice, had never heard of it.
    All power is violence; all power is evil.
    Money As Debt

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •