0

Back again with another question.

The file I have right now is of the following format:

1234,
1234,
1-23-4

I would like to do two things with this file.

First, remove the - characters in the third line. Therefore, 1-23-4 ==> 1234.

Second, I would like to make it all print in one line.

The final result should look like:

1234,1234,1234

Is this possible using line commands in a script? Kindly advise.

Thank you in advance for your time and help.

3 Answers 3

4

With tr:

$ tr -d '\n-' < a | sed 's/$/\n/'
1234,1234,1234

To remove the hyphens:

$ tr -d '-' < file
1234,
1234,
1234

And the same applies for the new lines with \n.

As we are removing all new lines, it will miss the finishing one. To recover it, we use sed.

$prompt tr -d '\n-' < a
1234,1234,1234$prompt

$ tr -d '\n-' < a | sed 's/$/\n/'
1234,1234,1234

Thanks fedorqui.I tried this. I replaced file with my file name but it is not creating a new file with the final format for me. Sorry i think I should have mentioned this in the question. My bad.

No problem. You just need to redirect it:

$ tr -d '\n-' < a | sed 's/$/\n/' > new_file
$ cat new_file 
1234,1234,1234
Sign up to request clarification or add additional context in comments.

8 Comments

+1, though you may need to put a finishing newline back on.
Thanks fedorqui.I tried this. I replaced file with my file name but it is not creating a new file with the final format for me. Sorry i think I should have mentioned this in the question. My bad.
Yes, that is true @paxdiablo I updated with sed writing it back. By the way, congrats for being 300K, that's so big :)
@Manus no problem, check my update in which I redirect the output. Hope it is clear
Hi fedorqui. Really appreciate your help. But I am stuck again. In my new file, only the last 1234 is appearing, with the '-' removed. Am I missing something here?
|
1

(gnu) awk:

awk -v RS="\0" 'gsub(/[\n-]/,"")' file

test

kent$  echo "1234,
1234,
1-23-4"|awk -v RS="\0" 'gsub(/[\n-]/,"")'
1234,1234,1234

1 Comment

Thank you kent. I will try out this option and update the thread. @fedorqui's solution worked for me and Im sure this one will too.
1

tr was a good one.

Anotherway in perl:

perl -pne 's/[-\n]//g' your_file
  • -n-> this will act as a while loop for each line in the file.

  • -e-> the thing after this is nothing but the expression which will act on each an every line.

  • -p-> print each line after the expression is executed on each line.

    s/search/replace/g

  • s/ search for either a newline or "-"/ replace with empty character/g-for all occurences in the line.

3 Comments

Thank you Vijay. But I do not know how to use PERL. Will keep in mind when I learn the language.
I will expalin you a bit to be more comfortable for you.
@Vijay I just formatted your answer a little bit to list all the options. I hope you don't mind.

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.