Convert tick to new lines

i can convert ticks to new lines using something like this:

tr '`' '\n' < filename
or
tr "\`" "\n" < filename

or vice versa

tr '\n' '`' < filename
or
tr "\n" "\`" < filename

however, this command seems to not work the same on every system. it works on ubuntu, and it works on redhat when run from the command line. but if run from a script, it gets rid of the '\n' and turns it to 'n', which then ends up replacing all letter "n's with '`'.

is there a different command i can use for this that will behave the same on every unix system? i've spent a couple of hours on this and nothing seems to work.

i just want to be able to translate all new lines to ticks and also be able to translate back to new lines in a reliable way.

Hello SkySmart,

If you want to use an awk solution then following may help you in same. Let's say our Input_file is as follows for an example.

cat Input_file
singh_is`_king1233
``is the input_file here.
test test test
test ` newline test `` again new line test

Now following is the command for your requirement.

awk -vs1="\`" '{gsub(s1,"\n",$0)} 1'  Input_file

Output will be as follows.

singh_is
_king1233


is the input_file here.
test test test
test 
 newline test 

 again new line test

Thanks,
R. Singh

1 Like

You could try perl as an alternative:

perl -pe 'y/`/\n/' file 
perl -pe 'y/\n/`/' file

But tr should work. How do you run it from a script and on what OS. Can you give an example?

1 Like

Hello SkySmart,

I am not sure about all O.S(or on which you are currently), I had tested this following in Ubuntu and BASH and it worked fine for me. Could you please try following and let me know if this helps you. So let's say we have following Input_file.

cat  Input_file
singh_is`_king1233
``is the input_file here.
test test test
test ` newline test `` again new line test
tr "$(printf "\x60\n")" "\n" <  Input_file

Output will be as follows.

singh_is
_king1233


is the input_file here.
test test test
test 
 newline test 

 again new line test

Thanks,
R. Singh

1 Like

Scrutinizer is right: tr works in both directions, on command line or in script, in Ubuntu or in FreeBSD. RedHat, being another linux flavour, shouldn't make any problems.
So - give us more info about the case when is doesn't work. How is the script run? What environment does it have? Any error messages?