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.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > replacing a carriage return and space with a space

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-05-10, 19:49
hally hally is offline
Registered User
 
Join Date: Feb 2003
Location: Toronto, Ontario, Canada
Posts: 34
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
Reply With Quote
  #2 (permalink)  
Old 05-05-10, 20:19
kitaman kitaman is offline
Papabi's friend
 
Join Date: Sep 2009
Location: Ontario
Posts: 629
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
Reply With Quote
  #3 (permalink)  
Old 05-05-10, 20:34
hally hally is offline
Registered User
 
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
Reply With Quote
  #4 (permalink)  
Old 05-05-10, 22:25
kitaman kitaman is offline
Papabi's friend
 
Join Date: Sep 2009
Location: Ontario
Posts: 629
Is the following input possible? And what should the output look like?

123abc
_456def
789ghi
000abc
_nopattern
_yesthereis
Reply With Quote
  #5 (permalink)  
Old 05-05-10, 22:32
hally hally is offline
Registered User
 
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
Reply With Quote
  #6 (permalink)  
Old 05-05-10, 23:46
kitaman kitaman is offline
Papabi's friend
 
Join Date: Sep 2009
Location: Ontario
Posts: 629
#!/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
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On