LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   problem in tr command (https://www.linuxquestions.org/questions/linux-general-1/problem-in-tr-command-4175490188/)

massy 01-05-2014 12:33 AM

problem in tr command
 
my input file is in this format:
RSU-1-1
15:40
2

I used tr command to replace '\n' for '\t'= tr '\n' '\t'
But, when I run it, the output is:
RSU-1-1 15:40 2
I want to have more spaces between RSU-1-1 and 15:40

I tried it by:
RSU-1-10
15:40
2

and it worked correctly:
RSU-1-10 15:40 2

druuna 01-05-2014 05:29 AM

A tab uses a fixed length (8 is very common).

The first entry you use (RSU-1-1) is 7 characters and using a tab after that will only add 1.
The second example (RSU-1-10) has 8 characters and using a tab will add 8:
Code:

$ tr '\n' '\t' < infile
RSU-1-1 15:40  2     
$ tr '\n' '\t' < infile2
RSU-1-10        15:40  2

One of the reason I personally do not like tabs.

You can use awk to do this:
Code:

$ awk 'BEGIN { ORS="    " } { print }' infile
RSU-1-1    15:40    2

The above will put 5 spaces between each entry.

You could also set the tab width before executing the tr command:
Code:

$ tabs -11
$ tr '\n' '\t' < infile
RSU-1-1  15:40      2         
$ tr '\n' '\t' < infile2
RSU-1-10  15:40      2

In the above example the tabs are set to 11

massy 01-06-2014 05:52 AM

Quote:

Originally Posted by druuna (Post 5092227)
A tab uses a fixed length (8 is very common).

The first entry you use (RSU-1-1) is 7 characters and using a tab after that will only add 1.
The second example (RSU-1-10) has 8 characters and using a tab will add 8:
Code:

$ tr '\n' '\t' < infile
RSU-1-1 15:40  2     
$ tr '\n' '\t' < infile2
RSU-1-10        15:40  2

One of the reason I personally do not like tabs.

You can use awk to do this:
Code:

$ awk 'BEGIN { ORS="    " } { print }' infile
RSU-1-1    15:40    2

The above will put 5 spaces between each entry.

You could also set the tab width before executing the tr command:
Code:

$ tabs -11
$ tr '\n' '\t' < infile
RSU-1-1  15:40      2         
$ tr '\n' '\t' < infile2
RSU-1-10  15:40      2

In the above example the tabs are set to 11

Thanks alot


All times are GMT -5. The time now is 11:50 AM.