Computing.Net > Forums > Unix > Vi search across carriage return

Vi search across carriage return

Reply to Message Icon

Original Message
Name: sharief
Date: June 2, 2004 at 11:55:10 Pacific
Subject: Vi search across carriage return
OS: Sun OS 5.7
CPU/Ram: 4 cpus
Comment:

How can I search for a string that wraps on to the next line and then replace the carriage return with nothing so that the line no longer wraps on to the next line.

Example:

blah blah blah...bl
ah blah blah...bla
h blah blah...blah
blah blah blah bl
ah blah blah blah

In the above text, each time the line wraps it adds a space at the beginning of the next line. I want to search for <CR><space> and replace it with nothing so the line no longer wraps and looks like this:

blah blah blah...blah blah blah...blah blah blah...blah blah blah blah blah blah blah blah

you get the picture.

-sharief


Report Offensive Message For Removal


Response Number 1
Name: Jim Boothe
Date: June 2, 2004 at 13:28:47 Pacific
Subject: Vi search across carriage return
Reply: (edit)

I don't know about vi, but you could use awk or sed:

sed -e :a -e '$!N;s/\n //;ta' -e P -e D infile


Report Offensive Follow Up For Removal

Response Number 2
Name: Devtakh
Date: June 2, 2004 at 13:52:48 Pacific
Subject: Vi search across carriage return
Reply: (edit)

Hi Jim,

I am curious to know-what does the command line in sed " $!N and ta " do.

Thanks a lot.


Regards,
Dev


Report Offensive Follow Up For Removal

Response Number 3
Name: Jim Boothe
Date: June 2, 2004 at 16:05:16 Pacific
Subject: Vi search across carriage return
Reply: (edit)

$!N says to fetch the Next line into the current buffer for the specified range of lines, which in this case is each line that is NOT the last line ($ means last line and ! means NOT).

Possibly a clearer example of a command preceded by a qualifying line number or range of lines would be:
sed 2,4d  (delete lines 2-4)
sed 2,4!d (delete all but lines 2-4)

ta is a conditional (Test) branch to label a. From the man page:
Branch to the : command bearing the label if any substitutions have been made since the most recent reading of an input line or execution of a t.

ba would be an unconditional Branch to label a.

Calling the next line into the buffer allows looking at multiple lines at once. With multiple lines in the buffer, this allows me to include the delimiting newline character(s) in my edits. In this case, I am deleting any newline-followed-by-space strings.

The logic depends upon always keeping the last line in the buffer (in case the next line needs to be joined to it). When I delete the newline character and thus join two lines into one, I now have just one line in the buffer, and I cannot output that line yet because the next line might also need to be joined. Thus, the loop is required. As long as changes are made (newline characters being deleted), it will keep looping and pulling more lines into the buffer until finally no changes are made, meaning I have two lines in the buffer, so I can fall out of the loop which will output the first of the two lines.


Report Offensive Follow Up For Removal

Response Number 4
Name: Devtakh
Date: June 3, 2004 at 07:59:48 Pacific
Subject: Vi search across carriage return
Reply: (edit)

Thanks a bunch !! Jim. That makes me understand better.

Really appreciate it.

Regards,
Dev


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Vi search across carriage return

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software