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

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Vi search across carriage return

Reply to Message Icon

Name: sharief
Date: June 2, 2004 at 11:55:10 Pacific
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



Sponsored Link
Ads by Google

Response Number 1
Name: Jim Boothe
Date: June 2, 2004 at 13:28:47 Pacific
Reply:

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


0

Response Number 2
Name: Devtakh
Date: June 2, 2004 at 13:52:48 Pacific
Reply:

Hi Jim,

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

Thanks a lot.


Regards,
Dev


0

Response Number 3
Name: Jim Boothe
Date: June 2, 2004 at 16:05:16 Pacific
Reply:

$!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.


0

Response Number 4
Name: Devtakh
Date: June 3, 2004 at 07:59:48 Pacific
Reply:

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

Really appreciate it.

Regards,
Dev


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Vi search across carriage return

vi \ ex : Substitute with newline? www.computing.net/answers/unix/vi-ex-substitute-with-newline/6485.html

Pushing carriage return in a file www.computing.net/answers/unix/pushing-carriage-return-in-a-file/7352.html

Removing carriage return www.computing.net/answers/unix/removing-carriage-return/4719.html