- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Adding a new column in a text file
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2003 07:25 PM
04-28-2003 07:25 PM
Adding a new column in a text file
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2003 07:46 PM
04-28-2003 07:46 PM
Re: Adding a new column in a text file
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2003 07:59 PM
04-28-2003 07:59 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2003 08:16 PM
04-28-2003 08:16 PM
Re: Adding a new column in a text file
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2003 08:28 PM
04-28-2003 08:28 PM
Re: Adding a new column in a text file
u learn something new every day. thanks for that tip.
-balaji
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2003 11:10 PM
04-28-2003 11:10 PM
Re: Adding a new column in a text file
If the numbers have to match:
sed 's+/file\(.*\)+& server\1+' filename
or
perl -pe 's!/file(.*)!$& server\1!' filename
rgds, Robin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2003 11:37 PM
04-28-2003 11:37 PM
Re: Adding a new column in a text file
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2003 12:02 AM
04-29-2003 12:02 AM
Re: Adding a new column in a text file
You could cheat, and use a dummy 3rd file:
echo | paste myfile - serverlist > newfile
rgds, Robin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2003 12:15 AM
04-29-2003 12:15 AM
Re: Adding a new column in a text file
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2003 12:28 AM
04-29-2003 12:28 AM
Re: Adding a new column in a text file
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