Grep Data Base on Column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep Data Base on Column
# 1  
Old 12-16-2015
Grep Data Base on Column

HI

Want to grep data from column header and match with second file.

Code:
File	A.txt					
						
	1	2	3	4	5	6
X1	A	L	D	J	Q	R
X2	B	M	K	P	w	T
X3	C	S	L	P	e	Y
X4	R	Z	M	A	r	U
						
						
FileB.txt						
1						
2						
3						
4						
6						
7						
						
						
FileC.txt						
						
	1	2	3	4	6	7
X1	A	L	D	J	R	n/a
X2	B	M	K	P	T	n/a
X3	C	S	L	P	Y	n/a
X4	R	Z	M	A	U	n/a

# 2  
Old 12-16-2015
Any attempts/ideas/thoughts from your side?
# 3  
Old 12-18-2015
Here is one.
Code:
awk '
(NR==FNR) {
# File1, read into array
  col[++colmax]=$1
  next
}
{
# File2
  if (FNR==1) {
    offset=0
  } else {
# print the first column; the addressed columns are +1
    printf "%s", $1
    offset=1
  }
# print the columns in the order of col[]
  for (c=1; c<=colmax; c++) {
    pos=offset+col[c]
    printf "%s%s", "\t", (pos>NF) ? "-" : $pos
  }
  printf "\n"
}
' FileB.txt FileA.txt

# 4  
Old 12-18-2015
Well, I had this one, but waited for OP's attempts/ideas/thoughts:
Code:
awk '
FNR == NR       {COL[++CNT]=$1
                 next
                }
FNR == 1        {printf "%-8s", ""
                 for (i=1; i<=CNT; i++) printf "%-8s", COL[i]
                 printf "\n"
                 next
                }
                {printf "%-8s", $1
                 for (i=1; i<=CNT; i++) printf "%-8s", $(COL[i]+1)?$(COL[i]+1):"n/a"
                 printf "\n"
                }
' file2 file1
        1       2       3       4       6       7       
X1      A       L       D       J       R       n/a     
X2      B       M       K       P       T       n/a     
X3      C       S       L       P       Y       n/a     
X4      R       Z       M       A       U       n/a

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Column to Row base on first and second Column

Input :- Column to Row Time CT 1 2 3 17:45 X 10 15 20 18:00 X 15 20 30 18:15 X 10 10 10 18:30 X 5 5 5 17:45 Y 10 15 20 18:00 Y 15 20 30 18:15 Y 10 10 10 18:30 Y 5 5 5 output:- Time P X Y 17:45 1 10 10 18:00 1 15 15 18:15 1 10 10 (2 Replies)
Discussion started by: pareshkp
2 Replies

2. Shell Programming and Scripting

Grep Data Base on Header

HI Guys, File A.txt UID,HD1,HD2,HD3,HD4 1,2,33,44,55 2,10,14,15,16 File B.txt UID HD1 HD4 A.txt B.txt >>>Output.txt UID,HD1,HD4 1,2,55 2,10,16 (11 Replies)
Discussion started by: pareshkp
11 Replies

3. Shell Programming and Scripting

Add Column base on other Column Data

HI Guys, I want add one extra Column base on 3rd Column . Input :- M204 MS204_154 :vsDataUeMe M204 MS204_154 es:sMeasure 0 M204 MS204_154 es:90ilterCoe 9 M204 MS204_154 es:searchE9090ortTime 40 M204 MS204_154 es:servOrPrioI90HoTimer 4000 M204 MS204_154 es:ueMeajllls154545 TRUE... (5 Replies)
Discussion started by: pareshkp
5 Replies

4. Shell Programming and Scripting

Combine data from two files base on uniq data

File 1 ID Name Po1 Po2 DD134 DD134_4A_1 NN-1 L_0_1 DD134 DD134_4B_1 NN-2 L_1_1 DD134 DD134_4C_1 NN-3 L_2_1 DD142 DD142_4A_1 NN-1 L_0_1 DD142 DD142_4B_1 NN-2 L_1_1 DD142 DD142_4C_1 NN-3 L_2_1 DD142 DD142_3A_1 NN-41 L_3_1 DD142 DD142_3A_1 NN-42 L_3_2 File 2 ( Combination of... (1 Reply)
Discussion started by: pareshkp
1 Replies

5. Shell Programming and Scripting

Change Data base on Column

Base of last two column i want to change may data if Last two Column have A and C then Copy Column $4 to Column $3. Input :- DD142 0_1 DD142_A DD142_B A B DD142 1_1 DD142_B DD142_C B C DD142 2_1 DD142_A DD142_C A C DD142 3_1 DD142_A A DD142 3_2 DD142_A A DD142 4_1 DD142_B B ... (4 Replies)
Discussion started by: pareshkp
4 Replies

6. Shell Programming and Scripting

Add column base on same name of second column

HI Guys, I have input file A.txt 1 AAA 1 BBB 1 1SW 1 2SW 1 3SW 1 BBB 1 PPP 1 1SW 1 PPP 1 1WS 1 1AS 1 P1P 1 AAA (1 Reply)
Discussion started by: pareshkp
1 Replies

7. Shell Programming and Scripting

Copy same name data from other file base on column

HI I have input file A.txt X Y Z File B.txt 1 X 10 AAA 11123 2 Y 22 PlD 4563 3 Z 55 PlD 54645 4 Z 66 PlD 15698 5 F 44 PlD 154798 6 C 55 PlD 12554 7 Z 88 PlD 23265 8 C 99 PlD 151654 9 C 11 PlD 21546546 I need New File C.txt (1 Reply)
Discussion started by: pareshkp
1 Replies

8. Shell Programming and Scripting

Change column to row base on column 2

Hi Guys, Input.txt L194 A -118.2 L194 B -115.1 L194 C -118.7 L196 A 0 L196 C 0 L197 A -111.2 L197 B -118.9 L197 C -119.9 L199 A -120.4 L199 B -119.9 ... (2 Replies)
Discussion started by: asavaliya
2 Replies

9. Shell Programming and Scripting

grep data on 2nd line and 3rd column

How do I grep/check the on-hand value on the second line of show_prod script below? In this case it's a "3". So if it's > 0, then run_this, otherwise, quit. > ./show_prod Product Status Onhand Price shoe OK 3 1.1 (6 Replies)
Discussion started by: joker_789us
6 Replies

10. Shell Programming and Scripting

Using loop reading a file,retrieving data from data base.

Hi All, I am having trouble through, I am reading the input from tab delimited file containing several records, e.g. line1 field1 field2 field3 so on.. line2 field1 field2 field3 so on.. .. .. on the basis of certain fields for each record in input file, I have to retrieve... (1 Reply)
Discussion started by: Sonu4lov
1 Replies
Login or Register to Ask a Question