- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: how to add colums of files
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
10-05-2004 12:17 PM
10-05-2004 12:17 PM
how to add colums of files
I have no. of files each file has no. of columns with Tab separate. i want to add these colums in a new file or in existing one with tab separation . say i want colume1,2 from file a.txt , colume 3 from file a.txt, colume 4,5 from file c.txt and so .
How can i do this ,Pl. provide some solution.
Thanks
sanjeev
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-05-2004 12:32 PM
10-05-2004 12:32 PM
Re: how to add colums of files
A quick way I can think is to get all the columns in seperate files. For ex.,
awk '{print $1,$2}' a.txt > a.col
awk '{print $3}' b.txt > b.col
awk '{print $4,$5}' c.txt > c.col
Then 'paste them together'
paste a.col b.col c.col > result.col
Look at man page of 'paste' for more information.
-Sri
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-05-2004 04:39 PM
10-05-2004 04:39 PM
Re: how to add colums of files
Also check out the 'cut' command as and alternative to awk to generate the column file.
And check out the 'join' command if there are 'keys' on the lines, not just order.
Finally here is a 'one-liner' with awk to solve this:
awk '{getline bl<"b.txt"; split(bl,b); getline cl<"c.txt"; split(cl,c); print $1,$2,b[3],c[4],c[5]}' a.txt
This mainly processes file A.
FOr every line in there is read a line from B in to a B-Line variable, and splits it into columns/words into array B.
Same for C.
Then print the desired fields as $?, B[?] and C[?].
The exmaple generates
a1 aa1 bbb1 cccc1 ccccc1
a2 aa2 bbb2 cccc2 ccccc2
a3 aa3 bbb3 cccc3 ccccc3
from:
a.txt
a1 aa1 aaa1 aaaa1 aaaaa1 aaaaaa1
a2 aa2 aaa2 aaaa2 aaaaa2 aaaaaa2
a3 aa3 aaa3 aaaa3 aaaaa3 aaaaaa3
b.txt
b1 bb1 bbb1 bbbb1 bbbbb1 bbbbbb1
b2 bb2 bbb2 bbbb2 bbbbb2 bbbbbb2
b3 bb3 bbb3 bbbb3 bbbbb3 bbbbbb3
c.txt
c1 cc1 ccc1 cccc1 ccccc1 cccccc1
c2 cc2 ccc2 cccc2 ccccc2 cccccc2
c3 cc3 ccc3 cccc3 ccccc3 cccccc3
Cheers,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-05-2004 05:15 PM
10-05-2004 05:15 PM
Re: how to add colums of files
IT is great one. Really great.
And we can try with awk / paste as,
combine all files and print based on your needed fields.
From hein's example,
# paste a.txt b.txt c.txt
a1 aa1 aaa1 aaaa1 aaaaa1 aaaaaa1 b1 bb1 bbb1 bbbb1 bbbbb1 bbbbbb1 c1 cc1 ccc1 cccc1 ccccc1 cccccc1
a2 aa2 aaa2 aaaa2 aaaaa2 aaaaaa2 b2 bb2 bbb2 bbbb2 bbbbb2 bbbbbb2 c2 cc2 ccc2 cccc2 ccccc2 cccccc2
a3 aa3 aaa3 aaaa3 aaaaa3 aaaaaa3 b3 bb3 bbb3 bbbb3 bbbbb3 bbbbbb3 c3 cc3 ccc3 cccc3 ccccc3 cccccc3
# paste a.txt b.txt c.txt | awk '{ print $1" "$2" "$7" "$13" "$14 }'
a1 aa1 b1 c1 cc1
a2 aa2 b2 c2 cc2
a3 aa3 b3 c3 cc3
I will try on script next to reach this.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-05-2004 06:06 PM
10-05-2004 06:06 PM
Re: how to add colums of files
#!/usr/bin/sh
readline()
{
file=$1
num=$2
line=`sed -n ${num}p $file`
}
index=1
while [[ $index -le $(wc -l a.txt| awk '{ print $1 }') ]]
do
readline a.txt $index
ALINE=$line
readline b.txt $index
BLINE=$line
readline c.txt $index
CLINE=$line
echo $ALINE| awk '{ printf $1"\t"$2"\t" }'
echo $BLINE| awk '{ printf $1"\t" }'
echo $CLINE| awk '{ printf $1"\t"$2"\n" }'
let index=index+1
done
It will give you what you need there.