Print columns in a file by number list in another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print columns in a file by number list in another file
# 1  
Old 01-12-2010
Print columns in a file by number list in another file

This should follow with my last post but I think it's better to start a new one. Now I have a list of numbers stored in pos.txt:

Code:
2
6
7
.
.
.
n
.
.
.

And I need to extract (2n-1, 2n) columns from matrix.txt:

Code:
ind1 A B C D E F G H I J K L M N O P Q R ...
ind2 B C D E F G H I J K L M N O P Q R S ...
ind3 C D E F G H I J K L M N O P Q R S T ...

The returned result should be like:

Code:
ind1 C D K L M N ...
ind2 D E L M N O ...
ind3 E F M N O P ...

With each number (n) read from list pos.txt, I was thinking of using something like

Code:
awk '{FS=" "; print $1" "$2*$n-1" "$2*$n}' matrix.txt

Sorry that I don't know much about awk but really appreciate your help!
# 2  
Old 01-12-2010
nawk -f zoho.awk pos.txt matrix.txt

zoho.awk:
Code:
FNR==NR {f1[$1];next}
{
  printf("%s", $1)
  for(i=2;i <= NF; i++)
    if(((i-1)/2) in f1) {
      printf("%c%s%c%s", OFS, $(i-1), OFS, $i)
    }
  printf("%c", ORS)
}

# 3  
Old 01-12-2010
Code:
#!/bin/bash
while read -a LINE
do
    echo -n "${LINE[0]} "
    while read J
    do
        let J*=2
        echo -n " ${LINE[$((J-1))]} ${LINE[$J]}"
    done < pos.txt
    echo
done < matrix.txt

If you want the output to a file :
Code:
{
...here the code
} > output.txt

# 4  
Old 01-12-2010
Appreciate a lot! You guys rock! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find columns in a file based on header and print to new file

Hello, I have to fish out some specific columns from a file based on the header value. I have the list of columns I need in a different file. I thought I could read in the list of headers I need, # file with header names of required columns in required order headers_file=$2 # read contents... (11 Replies)
Discussion started by: LMHmedchem
11 Replies

2. Shell Programming and Scripting

Number of columns in xml file

How to find the number of columns in xml file. i tried following command. #!bin/ksh cat $1 | grep -c "</mdm:attribute>" exit 0 but i am not getting accurate result which is not matching with the manual count. data format : <mdm:attribute> value </mdm:attribute> (6 Replies)
Discussion started by: victory
6 Replies

3. Shell Programming and Scripting

Match and print columns in second file

Hi All, I have to match each row in file 1 with 1st row in file 2 and print the corresponding column from file2. I am trying to use an awk script to do this. For example cat File1 X1 X3 X4 cat File2 ID X1 X2 X3 X4 A 1 6 2 1 B 2 7 3 3 C 3 8 4 1 D 4 9 1 1 (3 Replies)
Discussion started by: newpro
3 Replies

4. UNIX for Dummies Questions & Answers

print columns of a file in a for loop

Hello, I have a file with 32 columns and I want to print one column at a time, and then read each line of this column in order to use grep command. I tried with awk but i cannot put variable in awk for a in {1..32..1};do awk '{print$a}' file1.txt|while read line;do grep $line file2.txt;done >... (5 Replies)
Discussion started by: FelipeAd
5 Replies

5. Shell Programming and Scripting

Need to find a column from one file and print certain columns in second file

Hi, I need helping in finding some of the text in one file and some columns which have same column in file 1 EG cat file_1 aaaa bbbb cccc dddd eeee fffff gggg hhhh cat file_2 aaaa,abcd,effgh,ereref,name,age,sex,........... bbbb,efdfh,erere,afdafds,name,age,sex.............. (1 Reply)
Discussion started by: jpkumar10
1 Replies

6. Shell Programming and Scripting

awk: print columns depending on their number

hi guys, i have the following problem: i have a matrix with 3 columns and over 450 rows like this: 0.0165 0.0151 0.0230 0.0143 0.0153 0.0134 0.0135 0.0123 0.0195 0.0173 0.0153 0.0182 i now want to calculate the average of every line and divide every element of this... (1 Reply)
Discussion started by: waddle
1 Replies

7. UNIX for Dummies Questions & Answers

Print entire columns into new file

Dear All, I am having trouble obtaining the 3 outfiles (as shown below) from a single input file. Could you please help?? INPUT: filename a b c 1 4 2 3 3 2 4 2 7 OUTPUT: outfile1 (a.dat) 1 (2 Replies)
Discussion started by: chen.xiao.po
2 Replies

8. Shell Programming and Scripting

using awk to print some columns of a file

Hi, i have a file with content 00:01:20.613 'integer32' 459254 00:01:34.158 459556 00:01:36.626 'integer32' 459255 and i want to print only output as below 00:01:20.613 459254 00:01:34.158 459556 00:01:36.626 459255 i dont want the word 'integer32' which is the second column. i... (2 Replies)
Discussion started by: dealerso
2 Replies

9. Shell Programming and Scripting

concatenate 'n' number of columns in a file

i have a file which may have 'n' number of columns 1 222 fafda 32 afdaf 4343 4343 234 43fdaf 4343 fdd fdfd fdfd fdd fdfd fdfd fdfd fdfd fdfd fdd fdfd fdfd need to concatenate the columns with... (3 Replies)
Discussion started by: mlpathir
3 Replies

10. UNIX for Dummies Questions & Answers

Find number of columns in a file

Hi all, may seem a very stupid question.but me stuck up in it for long.... How to find the number of columns in a ASCII file. EX:-Demo.lst songs 1 34 45 67 Number of columns should be 5. Regards, Anindya ;) (13 Replies)
Discussion started by: rahul26
13 Replies
Login or Register to Ask a Question