Grep Data Base on Header


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep Data Base on Header
# 1  
Old 02-26-2016
Grep Data Base on Header

HI Guys,


File A.txt
Code:
UID,HD1,HD2,HD3,HD4
1,2,33,44,55
2,10,14,15,16

File B.txt
Code:
UID
HD1
HD4

A.txt B.txt >>>Output.txt

Code:
UID,HD1,HD4
1,2,55
2,10,16


Last edited by pareshkp; 02-26-2016 at 05:03 PM..
# 2  
Old 02-26-2016
What specifically are you looking to do and what have you tried to solve this...
# 3  
Old 02-26-2016
Just Copy Data From One file to another base on Header Name.
# 4  
Old 02-26-2016
And what have you tried to solve it on your own...
# 5  
Old 02-26-2016
I have tried Below

Code:
$ cols=($(sed '1!d;s/, /\n/g' $A | grep -nf $B | sed 's/:.*$//'))

$ cut -d ',' -f 1$(printf ",%s" "${cols[@]}") $A

# 6  
Old 02-26-2016
You weren't far off. Your field separator is a comma, not a comma followed by a space. So, if you change your first sed from:
Code:
sed '1!d;s/, /\n/g' $A

to:
Code:
sed '1!d;s/,/\n/g' $A

as in:
Code:
#!/bin/ksh
A=A.txt
B=B.txt
cols=($(sed '1!d;s/,/\n/g' "$A" | grep -nf "$B" | sed 's/:.*$//'))
cut -d ',' -f 1$(printf ",%s" "${cols[@]}") "$A"

it seems to do what you want. You might also want to consider:
Code:
#!/bin/ksh
A='A.txt'
B='B.txt'
awk '
BEGIN {	FS = OFS = ","
}
FNR == NR {
	h[++hc] = $0
	next
}
FNR == 1 {
	for(i = 1; i <= hc; i++) {
		for(j = 1; j <= NF; j++) {
			if($j == h[i]) {
				o[i] = j
				break
			}
		}
		if(j > NF) {
			printf("Header \"%s\" not found in file \"%s\".\n",
			    h[i], FILENAME)
			exit 1
		}
	}
}
{	for(i = 1; i <= hc; i++)
		printf("%s%s", $o[i], (i == hc) ? ORS : OFS)
}' "$B" "$A"

which invokes awk once instead of invoking sed twice, grep once, and cut once; so it should run a bit faster.

As always, if you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
# 7  
Old 02-27-2016
Smilie
Code:
# cat A.txt
UID,HD1,HD2,HD3,HD4
1,2,33,44,55
2,10,14,15,16
# cat B.txt
UID
HD1
HD4

Solution
Code:
# awk 'FNR==NR{h[$0]=$0;next}FNR==1{for(;i++<NF;){if($i==h[$i]){o[l++]=i}}}{for(_ in o)printf "%s%s",$o[_],(_==(l-1))?RS:FS}' FS=\, B.txt A.txt
UID,HD1,HD4
1,2,55
2,10,16


Last edited by danmero; 02-27-2016 at 11:26 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep Data Base on Column

HI Want to grep data from column header and match with second file. 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 (3 Replies)
Discussion started by: pareshkp
3 Replies

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

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

4. Shell Programming and Scripting

Filtering data base on range

Hi All, I have the file in following format. I need to change column 16 and column 17 for those lines which has atleast one numerical digit in column 2 , with the exception on SUPP1 Input file : S00005615|1044|0|0.00|0|0.00| |112|-30|28.1|0|0| |20130331|220.2|2|3|... (2 Replies)
Discussion started by: nua7
2 Replies

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

6. Shell Programming and Scripting

data base update

hello everyone I need to update data base in file 1-ID 2-Name 3-ID group 4-ID teacher 5-mark list unique ID is (ID+ID group+ID teacher) we can append 5 th columns (marks list) main base file: Code: 1:John:3:1:4 3 2 2:Mark:1:2:1 3 3:Susan:3:4: input file: (1 Reply)
Discussion started by: mleplawy
1 Replies

7. UNIX for Dummies Questions & Answers

grep data from header of files

Hello I want to grep data from header of the files . The header conatins data in this format . # $maint$ test1 # $product$ TTT I want to store maint name and product name with path of file . (12 Replies)
Discussion started by: getdpg
12 Replies

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

9. Programming

data base

Dear sirs, I am new to DataBase in C++,Also here iam using mysql, How can do it nad get the result back from the database and do operation onit. Thanks in advance, arunkumar (2 Replies)
Discussion started by: arunkumar_mca
2 Replies
Login or Register to Ask a Question