0

I have a text file like inputfolder/abc.txt

Input (Below is just an example - the actual file will have many lines which will vary everytime):

abcdefgh~

asdfghjkliuy~

qwertyuiopasdfgh~

..........

Every line ends with '~' and I would like to merge all the lines into one

Desired output:

abcdefgh~asdfghjkliuy~qwertyuiopasdfgh~...............

How can I merge all the lines into one line using shell script? (I don't want to add any extra character)

Once all the lines are merged, the file should be moved to another folder.
Example: OutputFolder/abc.txt

Sven Eberth
  • 3,057
  • 12
  • 24
  • 29
Alex
  • 11
  • 2
  • Welcome to SO, the question is duplicate, you could find your answer at: "https://stackoverflow.com/questions/15580144/how-to-concatenate-multiple-lines-of-output-to-one-line" or "https://stackoverflow.com/questions/9605232/how-to-merge-every-two-lines-into-one-from-the-command-line". – Victor Lee Jun 23 '21 at 01:35

1 Answers1

1

You can solve this with tr and the delete parameter (delete the new line characters).

$ cat inputfolder/abc.txt 
abcdefgh~

asdfghjkliuy~

qwertyuiopasdfgh~

..........
$ cat inputfolder/abc.txt | tr -d "\r\n" > outputFolder/abc.txt
$ cat outputFolder/abc.txt 
abcdefgh~asdfghjkliuy~qwertyuiopasdfgh~..........

Or with sed:

$ sed -z "s/\n//g" inputfolder/abc.txt > outputFolder/abc.txt
Sven Eberth
  • 3,057
  • 12
  • 24
  • 29
  • Hello Sven and Thanks for your help. I tried executing with tr command but got errored out saying 'tr: command not found'. Is there any other command that I can use instead of 'tr' ? – Alex Jun 23 '21 at 04:36
  • Sven, I tried using sed command and it worked but I have one concern. The command that I am using is --------------- sed ':a; N; $!ba; s/\n//g' input.txt > output.txt ----------- It is merging everything in line 1 but it is also adding one extra blank newline - a space in line 2 (line 2) and I don't want that. Am I missing something here? Any advice is greatly appreciated. – Alex Jun 23 '21 at 06:45
  • Hmm. I don't know why `tr` is not available, but maybe you can check the problem and fix it. Is your PATH correct (See https://stackoverflow.com/questions/51920182/cut-and-tr-command-not-found)? Are you on Ubuntu (See https://askubuntu.com/questions/1113047/tr-command-not-found)? – Sven Eberth Jun 23 '21 at 12:53
  • tbh, I'm not a `sed` expert, but I've added a command to my answer which worked for me without a final newline. – Sven Eberth Jun 23 '21 at 12:55