print columns of a file in a for loop


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers print columns of a file in a for loop
# 1  
Old 12-13-2011
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

Code:
for a in {1..32..1};do awk '{print$a}' file1.txt|while read line;do grep $line file2.txt;done > file3.txt;done

# 2  
Old 12-13-2011
Does your grep support -f?

If so you could just do something like:
Code:
[user@host2: /tmp] cat 1.txt
aaa bbb ccc eee
[user@host2: /tmp] cat 2.txt
aaa bbbb
cccc
dddd
eeee
ffff
gggg
hhhh cccc
[user@host2: /tmp] grep -f <(tr ' ' '\n' <1.txt) 2.txt
aaa bbbb
cccc
eeee
hhhh cccc

# 3  
Old 12-13-2011
Sorry, I am not sure how to use your command in my case
# 4  
Old 12-13-2011
Post sample input and output...

CarloM's code, use it like this in your case...
Code:
grep -f <(tr ' ' '\n' <file1.txt) file2.txt > file3.txt

--ahamed
# 5  
Old 12-13-2011
Please post some sample data, making it clear what separates a "column" in each record.

You can use awk to search a particular column without using grep.
# 6  
Old 12-14-2011
Thank you for your replies, ahamed and methyl here are some sample data

file1.txt
Code:
A    G    K    N    Q    T    W    
B   H    L    O    R    U    X    
C    I   M    P    S    V    Y   
D    J                        
E                            
F

and file2.txt
Code:
A p1 p2 p3
B s1 s2 s3 s4 
E a1 a2 
P f1 f2
J d1 d2 d3 d4 d5

I want to read each column sequentially
e.g. the first column
Code:
A
B
C
D
E
F

and output
Code:
A p1 p2 p3
B s1 s2 s3 s4 
E a1 a2

and then column 2,
Code:
G
H
I
J

and output
Code:
J d1 d2 d3 d4 d5

The files are tab-delimited
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare 2 columns from the same file and print a value depending on the result

Hello Unix gurus, I have a file with this format (example values): label1 1 0 label2 1 0 label3 0.4 0.6 label4 0.5 0.5 label5 0.1 0.9 label6 0.9 0.1 in which: column 1 is a row label column 2 and 3 are values I would like to do a simple operation on this table and get the... (8 Replies)
Discussion started by: ksennin
8 Replies

2. UNIX for Beginners Questions & Answers

Count multiple columns and print original file

Hello, I have two tab files with headers File1: with 4 columns header1 header2 header3 header4 44 a bb 1 57 c ab 4 64 d d 5 File2: with 26 columns header1.. header5 header6 header7 ... header 22...header26 id1 44 a bb id2 57 ... (6 Replies)
Discussion started by: nans
6 Replies

3. 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

4. Shell Programming and Scripting

Awk print all columns in delimited file

text file example 1,222222222222,333,444444444444444 111111111,22,33333333,444 desired output 1 222222222222 333 444444444444444 111111111 22 33333333 444I have a delimeted file which I want to print in a table like format. The... (10 Replies)
Discussion started by: Calypso
10 Replies

5. 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

6. 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

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

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: 2 6 7 . . . n . . . And I need to extract (2n-1, 2n) columns from matrix.txt: 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... (3 Replies)
Discussion started by: Zoho
3 Replies

10. Shell Programming and Scripting

Compare selected columns from a file and print difference

I have learned file comparison from my previous post here. Then, it is comparing the whole line. Now, i have a new problem. I have two files with 3 columns separated with a "|". What i want to do is to compare the second and third column of file 1, and the second and third column of file 2. And... (4 Replies)
Discussion started by: kingpeejay
4 Replies
Login or Register to Ask a Question