If this is your first visit, be sure to check out the FAQ by clicking the link above.
You may have to register before you can post: click the register link above to proceed.
To start viewing messages, select the forum that you want to visit from the selection below.
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.
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
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
#!/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