Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2003
    Location
    Toronto, Ontario, Canada
    Posts
    34

    Unanswered: replacing a carriage return and space with a space

    Hi

    I have a file that I would like edit by searching for any carriage return and space and replacing that with a space. So if the file looked like the following:

    123abc
    456def
    789ghi
    012jkl

    after running my shell script the edited file would like:
    123abc 456def
    789ghi 012jkl

    I tried using the tr command but I can't figure out how to search for carriage returns and spaces as a pattern. If anyone can help it would be much appreciated.

    Thanks

    hally

  2. #2
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    No sure which of the following two you want.

    tr -d "\n" <input_file >output_file #removes all line feeds, output is a one line file
    or
    #!/bin.ksh #creates one line out of every two lines of input
    while read a
    do
    read b
    echo $a $b >>output_file
    done <input_file

  3. #3
    Join Date
    Feb 2003
    Location
    Toronto, Ontario, Canada
    Posts
    34
    I tried using the tr -d option earlier today but because i'm looking for carriage returns with a space beginning on the next line the -d option doesn't work for me. I was thinking about using a loop but because I can't figure out how to search for carriage returns with a space beginning on the next line so the loop won't do much for me. I noticed the example didn't show up how I wanted it to be viewed so i'm going to sustitute the whitespaces with <s>

    123abc
    <s>456def
    789ghi
    <s>012jkl

    after running my shell script the edited file would like
    123abc<s>456def
    789ghi<s> 012jkl

  4. #4
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    Is the following input possible? And what should the output look like?

    123abc
    _456def
    789ghi
    000abc
    _nopattern
    _yesthereis

  5. #5
    Join Date
    Feb 2003
    Location
    Toronto, Ontario, Canada
    Posts
    34
    Quote Originally Posted by kitaman View Post
    Is the following input possible? And what should the output look like?

    123abc
    _456def
    789ghi
    000abc
    _nopattern
    _yesthereis
    i'll assume the underscore is the space and yes that input is possible. The output should look like this:

    123abc 456def
    789ghi
    000abc nopattern yesthereis

  6. #6
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    #!/bin/ksh
    ed inputfile <ed.inp #change all leading space to underscore
    prev=""
    while read a
    do
    if [ `echo $a|cut -c1` = "_" ]
    then
    a=`echo $a|cut -c2-`
    prev="$prev $a"
    else
    echo "$prev"
    prev=$a
    fi
    done <inputfile
    echo $prev


    ed.inp
    1,$s/^ /_/ #one,$s/^space/_/
    w
    q

Posting Permissions

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