Operating System - HP-UX
1833780 Members
2488 Online
110063 Solutions
New Discussion

Adding a new column in a text file

 
Chern Jian Leaw
Regular Advisor

Adding a new column in a text file

HI,
I would like to append a new column in a text file which already has a column at the extreme left justification of the file:
#cat myfile
/file1
/file2
/file3

I would like to add new column as below:
/file1 server1
/file2 server2
/file3 server3

Could anyone show me how it should be done?

Thanks
9 REPLIES 9
malay boy
Trusted Contributor

Re: Adding a new column in a text file

Hi,

You can use something like this :

sed 's/$/ server/g' file_name

this will append a string "server" at the end of each line in the file_name .

hope this help.

regards
mB
There are three person in my team-Me ,myself and I.
Balaji N
Honored Contributor

Re: Adding a new column in a text file


$cat a1
file1
file2
file3
file4
[balajin@tispenguin tmp]$ awk '{print $1 " server"NR}' a1
file1 server1
file2 server2
file3 server3
file4 server4


does this help.
-balaji
Its Always Important To Know, What People Think Of You. Then, Of Course, You Surprise Them By Giving More.
V. V. Ravi Kumar_1
Respected Contributor

Re: Adding a new column in a text file

hi,

use paste command.
Ex:
file a
a1
a2
a3
file b
b1
b2
b3
paste a b > ab
a1 b1
a2 b2
a3 b3


Regards
Never Say No
Balaji N
Honored Contributor

Re: Adding a new column in a text file

amazing ravi.

u learn something new every day. thanks for that tip.
-balaji
Its Always Important To Know, What People Think Of You. Then, Of Course, You Surprise Them By Giving More.
Robin Wakefield
Honored Contributor

Re: Adding a new column in a text file

Hi,

If the numbers have to match:

sed 's+/file\(.*\)+& server\1+' filename

or

perl -pe 's!/file(.*)!$& server\1!' filename

rgds, Robin
Chern Jian Leaw
Regular Advisor

Re: Adding a new column in a text file

Ravi,
Thanks for introducing paste command.
However, I would like to concatenated lines i.e.:
/file1 server1
(list continues ...)
to be separated by 2 tab spaces instead of the default single tab space.

#cat myfile
/file1
/file2
/file3

#cat serverlist
server1
server2
server3

I tried doing the method below, but it still produced the file newfile which separates the entries with a single tab space:
#paste -d"\t\t" myfile serverlist > newfile

Any ideas how I could solve this problem?

Thanks


Robin Wakefield
Honored Contributor

Re: Adding a new column in a text file

Hi,

You could cheat, and use a dummy 3rd file:

echo | paste myfile - serverlist > newfile

rgds, Robin
Chern Jian Leaw
Regular Advisor

Re: Adding a new column in a text file

Robin,

echo | paste myfile - serverlist > newfile

produces the file newfile as follows:
#cat newfile
file1 file1 server1
file2 file2 server2
file3 file3 server3

where myfile contains:
/file1
/file2
/file3

and serverlist contains:
server1
server2
server3

May I know why is there an additional column of file1, file2 and file3?

Also, is there a way which I can specify the append any constant string(not from a file) to the first column? i.e:
paste myfile "myserver" > newfile
#cat newfile
file1 myserver
file2 myserver
(list continues ...)

Any ideas how this can be done?

Thanks

Robin Wakefield
Honored Contributor

Re: Adding a new column in a text file

Hi,

This is weird, as it works OK on my system:

echo | paste myfile - serverlist | od -c
0000000 / f i l e 1 \t \t s e r v e r 1 \n
0000020 / f i l e 2 \t \t s e r v e r 2 \n
0000040 / f i l e 3 \t \t s e r v e r 3 \n
0000060

anyone else get this?

If you want to add a constant, I'd just use sed

sed 's/.*/& myserver/' myfile > newfile

where I've pressed tab twice after the "&".

rgds, Robin