Big-BoB
Big-BoB

Reputation: 91

Substitute CarriageReturn with new line and pattern in VIM

I have a file.txt having three lines

line1
line2
line3

While i open file.txt in vim i want to substitute every CR(carriage Return) with a new line having the string "foo" , some thing like this.

$ cat file.txt
line1
foo
line2
foo
line3
foo

Also looking for the possibility with awk, sed and any thing else in bash script.

Upvotes: 2

Views: 679

Answers (4)

Ed Morton
Ed Morton

Reputation: 203522

$ awk '{print $0 "\nfoo"}' file
line1
foo
line2
foo
line3
foo

Upvotes: 1

SibiCoder
SibiCoder

Reputation: 1496

This pattern will do what you wanted in vim.

   :%s/\n/^Mfoo^M/g

To insert ^M in the pattern, press Ctrl+V and then Enter. ^M inserts a newline whereas in the pattern will literally insert <,C,R and >

Instead of ^M, you can use \r for Linux machines.

    :%s/\n/\rfoo\r/g

Upvotes: 0

romainl
romainl

Reputation: 196556

You can use this substitution in Vim:

:%s/\n/&foo&

Or this macro:

:%g/^/norm ofoo

Upvotes: 2

SLePort
SLePort

Reputation: 15461

With sed, if you just want to replace carriage return with foo :

sed 's/\x0D$/\nfoo/' file.txt

To add a line with string foo after every line :

sed 's/$/&\nfoo/' file.txt

Upvotes: 2

Related Questions