Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2003
    Posts
    5

    Unanswered: awk-ward carriage returns

    What I thought would be a simple search and replace is turning out not to be so....

    I would like to remove all carriage returns from a file - I am finding, though, that awk does not recognize carriage returns in its substitute function. Here is what I have tried:

    awk 'gsub("\n", "--")' testFile > newFile
    awk 'gsub("\r", "--")' testFile > newFile
    awk 'gsub("\f", "--")' testFile > newFile
    awk 'gsub("\x0a", "--")' testFile > newFile

    where testFile is:
    move
    this
    text
    to one line...

    I guess UNIX treats files as a group of lines, rather than a stream of bytes?
    Any ideas? Are there any other UNIX tools I should look at?

    thanks for your help UNIX gurus!

  2. #2
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    • The following command removes all new line character from your file :

      cat testFile | tr 'd "\n" > newFile

      With your file the result is (if lines doen't end with a space):
      movethistextto one line...
    • To replace newline char by a space :

      cat textFile | tr "\n" " " > newFile

      and the result is :
      move this text to one line...
    • To replace newline by a string (eg '--') :

      sed 's/$/--/' testFile | tr -d "\n" > newFile

      and the result is
      move--this--text--to one line...


    • Awk read the file line by line (a ligne end with NL in the file) and store them in the variable $0 without the ending NL.
      So gsub can't remove the NL because there is no NL in $0.

      To specify action for all lines, use the syntax :
      awk '{ action }';

      In gsub, the \\ must be double.
      gsub("\\r", "--") # replace all carriage return (M) by "--"

      Another point, you must add a 'print $0' statement after 'gsub'?
      awk '{gsub("\\r", "--"); print $0}' testFile > newFile
    Jean-Pierre.

  3. #3
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    Hi Jean Pierre. A few ponts/questions...
    cat testFile | tr -d "\n" > newFile
    No need for 'cat': tr -d "\n" file > newFile
    Awk read the file line by line (a ligne end with NL in the file) and store them in the variable $0 without the ending NL.
    So gsub can't remove the NL because there is no NL in $0.
    Awk reads its input record by record. Set the record separator to something other than the default, newline character and each record may then contain multiple newline characters.

    e.g. awk 'BEGIN {RS=""}{gsub(/\n/," ",$0); print $0}' file > newFile
    In gsub, the \\ must be double.
    gsub("\\r", "--") # replace all carriage return (M) by "--"
    Which version of awk is this?

  4. #4
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    Hi Damian
    No need for 'cat': tr -d "\n" file > newFile
    Exact, 'cat' is not need but the right syntax is : tr -d "\n" < file > new_file

    Awk reads its input record by record. Set the record separator to something other than the default, newline character and each record may then contain multiple newline characters.

    e.g. awk 'BEGIN {RS=""}{gsub(/\n/," ",$0); print $0}' file > newFile
    good shoot, I know the variable and its function but I did not think of using it

    quote:
    --------------------------------------------------------------------------------

    In gsub, the \\ must be double.
    gsub("\\r", "--") # replace all carriage return (M) by "--"

    --------------------------------------------------------------------------------


    Which version of awk is this?
    "\r" works fine. I confused with rules relating to \ and & in dynamic regular expression.
    However, my awk (AIX) is rather permissive, the three following staements give the same result.
    gsub("\n","--")
    gsub("\\n","--")
    gsub("\\\n","--")
    Simplest is to use dyname RE only when it is not possible to make differently :
    gsub(/\n/,"--")
    Jean-Pierre.

  5. #5
    Join Date
    Dec 2003
    Posts
    5
    What a nice surprise to come into work this morning to find that my problem has been solved!

    Thank you Damian and Jean Pierre -

    I am using the tr command - that's the tool I was looking for.

Posting Permissions

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