Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2003

    Unanswered: how to get rid of end of line char by using scripts

    How to use end of line character(\n) as delimiter in sed or awk?
    or anyway can solve my problem by using boron shell scripts.
    e.g. myfile contains the following message
    hello world1
    hello world2
    I want to append the second line to the first line.
    I tried the following ways:
    nawk 'BEGIN{FS="\n"} END{print $0}' myfile
    sed -n 's/\\\n//g' myfile

    They all didn't work! sign.... Can anyone help me to figure out the problems?

  2. #2
    Join Date
    Jan 2003
    awk 'BEGIN{RS="/"} {gsub("\n","");print}' filename

    This sets the record separtor to a character that doesn't exist in the source file (in my case a slash). The for each line read, strip off the newlines and print. This will concatenate all the lines in the file to one massive output.
    If you just wanted to join adjacent lines, then this does not work, and
    something along the lines of:
    awk '{gsub("\n","");getline x;printf "%-s%-s\n",$0,x}' filename

    should work.
    This reads a line (automatically), uses getline to 'read' the next line into x, then prints the original row and x followed by a newline.

    I'm not an expert so there may be easier ways of doing the same...

  3. #3
    Join Date
    Aug 2001

    Re: how to get rid of end of line char by using scripts

    The first script appends pairs of lines ...

    cat <filename> | sed '$!N;s/\n/ /'

    The following appends all lines in a file to one ...

    cat <filename>| sed -e :a -e '$!N;s/\n/ /; ta'



Posting Permissions

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