Computing.Net > Forums > Unix > replacing newline char in a text in

replacing newline char in a text in

Reply to Message Icon

Original Message
Name: kamalpreet
Date: January 18, 2007 at 03:46:21 Pacific
Subject: replacing newline char in a text in
OS: Solaris
CPU/Ram: Ultra 60
Model/Manufacturer: Sun
Comment:


How can I join lines in vi editor..
s/^M// is not working?

(though I can change "space" with a new line
s/ /^M/ this works..)

Warm Regards
Kamalpreet Singh


Report Offensive Message For Removal


Response Number 1
Name: James Boothe
Date: January 18, 2007 at 08:04:55 Pacific
Subject: replacing newline char in a text in
Reply: (edit)

On a line-by-line basis, of course you would position on a desired line and type J to join the following line to it.

For a global operation, such as to locate each line containing xyz and join the following line to those, then do:

:g/xyz/j

In both cases, by default joined lines will be separated by a single space.


Report Offensive Follow Up For Removal

Response Number 2
Name: kamalpreet
Date: January 18, 2007 at 20:54:51 Pacific
Subject: replacing newline char in a text in
Reply: (edit)

Yes this is working ...but in a restricted way...
I want to cat all lines into one.
I tried ":g/$/j" and this had to be done multiple times to achieve what I wanted.

Solution is Good ..can u suggest a better one liner
Thanks

Warm Regards
Kamalpreet Singh


Report Offensive Follow Up For Removal

Response Number 3
Name: nails
Date: January 22, 2007 at 11:26:41 Pacific
Subject: replacing newline char in a text in
Reply: (edit)

It is not a vi solution, but this command places all lines of a file on 1:

xargs < myfile

which is the short had way of saying:

cat myfile|xargs


Report Offensive Follow Up For Removal

Response Number 4
Name: James Boothe
Date: January 22, 2007 at 12:56:05 Pacific
Subject: replacing newline char in a text in
Reply: (edit)

kamalpreet,

nails gave you a good non-vi solution because the task you want of joining all lines of a file would typically be done outside of vi.

Other non-vi solutions ...

Join all lines with no space between:
cat myfile | tr -d "\n"

Join all lines with a space between:
cat myfile | tr "\n" " "

And in vi, for smallish files, you could do a single J command for the number of lines in the file. If your file has 50 lines, then in command mode (not at the colon prompt), do: 50J


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: replacing newline char in a text in

Comments:

 


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




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge