0

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/;/\n\g

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

How can it be achieved in shell?

Inian
  • 62,560
  • 7
  • 92
  • 110
coder007
  • 157
  • 3
  • 12
  • 2
    What do you mean? Do you want to read the content of a whole file, manipulate that end write out as new file? Better give some example; and include the full script you wrote so far. – GhostCat Oct 21 '16 at 09:17

2 Answers2

1

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 .

Community
  • 1
  • 1
Mounika
  • 161
  • 13
1

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
Mark Setchell
  • 146,975
  • 21
  • 182
  • 306