Reputation: 879
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.
Upvotes: 0
Views: 79
Reputation: 67211
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.
Upvotes: 1
Reputation: 195029
(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
Upvotes: 1
Reputation: 289495
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
Upvotes: 4