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

1

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

4 Comments

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' ?
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.
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 stackoverflow.com/questions/51920182/…)? Are you on Ubuntu (See askubuntu.com/questions/1113047/tr-command-not-found)?
tbh, I'm not a sed expert, but I've added a command to my answer which worked for me without a final newline.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.