If I have txt file named "test.txt" like this
1|2|3|4|
2|3|4|5|
3|4|5|6|
4|5|6|7|
I need to replace a "|"(pipe) at the end of the line with ";"(semicolon)
it will look like this after replace
1|2|3|4;
2|3|4|5;
3|4|5|6;
4|5|6|7;
Which scripts I have to use?
Many Thanks
using sed:
sed -i 's/\|$/;/' test.txt
using Perl:
perl -pi -e 's/\|$/;/' test.txt
If I need this way, How can I do that?
From
1|2|3|4|
2|3|4|5|
3|4|5|6|
4|5|6|7|
Change to
1|2|3|4|;2|3|4|5|;3|4|5|6|;4|5|6|7|;
Which are
replace "|" at the end of the line to "|;" (pipe and semicolon) then pull up the next line on to the same line.
Thanks again
awk 'sub(/\|$/,";")' ORS= data
Use nawk or /usr/xpg4/bin/awk on Solaris.
your command didn't work with me.
I'm in solaris 10.
And after I used your scripts,it said syntax error.
??
... and you used nawk or /usr/xpg4/bin/awk as I suggested?
yes of course.
Er..
I have a text file named "test.txt"
which store a text file as follow
1|2|3|4|
2|3|4|5|
3|4|5|6|
and i need to replace those text file into these:
1|2|3|4|;2|3|4|5|;3|4|5|6|;
can I still use awk?
But what have you tried ?
this replaces the last "|" in the input
awk '{ sub(/\|$/, ";"); print }END{ printf "\n" }' ORS="" filename
try this for the required output
awk '{ print }END{ printf "\n" }' ORS=";" a2
# tr "\n" ";" < file
1|2|3|4|;2|3|4|5|;3|4|5|6|;4|5|6|7|
if leave to you to put in the last ";" . i suggest you also read up more on shell scripting.
$ cat test.txt
1|2|3|4|
2|3|4|5|
3|4|5|6|
4|5|6|7|
$ nawk 'sub(/\|$/,";")' ORS= test.txt
1|2|3|4;2|3|4|5;3|4|5|6;4|5|6|7;$
If you want the last newline:
$ nawk 'sub(/\|$/,";");END{printf "\n"}' ORS= test.txt
1|2|3|4;2|3|4|5;3|4|5|6;4|5|6|7;
$