Operating System - HP-UX
1832274 Members
1909 Online
110041 Solutions
New Discussion

Re: how to add colums of files

 
Sanjeev gupta_2
Frequent Advisor

how to add colums of files

Hi All
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
4 REPLIES 4
Sridhar Bhaskarla
Honored Contributor

Re: how to add colums of files

Hi Sanjeev,

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
You may be disappointed if you fail, but you are doomed if you don't try
Hein van den Heuvel
Honored Contributor

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.
Muthukumar_5
Honored Contributor

Re: how to add colums of files

Hein.

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.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: how to add colums of files

We can do with scripting by taking hein's examples as,

#!/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.
Easy to suggest when don't know about the problem!