file to string and string to file in bash

I want to convert file to string and do some processing and get back same to file again in bash shell in Linux.

Mainly, aim is to remove new line 'n' by ';' which is not exist in file and get file gain by replacing ';' by 'n' .(; is non exists in file)

when I checked file with :set list to check all character in the file. I can see $ at end of line and when I run :

:%s/n;g 

to replace it works but next

:%s/;/ng

don't work, I can see character ^@ instead of $ at the new line.

How can it be achieved in shell?


Considering you are talking about vim command line. I highly doubt when you said :%s/n;g is working.

To replace newline with ';' try: :%s/n/;/g

Similarly ';' to newline try :%s/;/r/g

Clear explanation of why to use r can be found here.

The usage of %s is :%s/<string_to_be_replaced>/<string_to_be_replaced_with>/g .


Are you really talking about the shell or about vi ? If you do mean the shell, the easiest way is to use tr to transliterate individual characters:

Replace all newlines with semicolons:

tr 'n' ';' < inputFile > outputFile

Replace all semicolons with newlines:

tr ';' 'n' < inputFile > outputFile
链接地址: http://www.djcxy.com/p/49340.html

上一篇: ^ v vs \ n在vim字符串替换中

下一篇: 文件以字符串形式存储在bash中