Print Missing Header Name from File.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print Missing Header Name from File.
# 1  
Old 03-14-2016
Print Missing Header Name from File.

HI Guys,

I have file A.txt

Code:
ID,L1,L2,L3,L4
1A,2a,33a,44b,55c
2A,10a,14a,15b,16c

File B.txt

Code:
ID
L1
L4
L5

Output:-

Code:
ID,L1,L4,L5
1A,55c,n/a
2A,16c,n/a

I have below solution which is works perfect if all header match from file b.

Code:
nawk -v OFS="," '$1=$1' $Lic1 > $Lic2

	nawk '
	BEGIN           {OFS = FS = ","}
	FNR == NR       {h[$0] = NR
                 MX = NR
                 next
                }
	FNR == 1        {for (i=1; i<=NF; i++) if ($i in h) a[h[$i]] = i
                }

                {for (i=1; i<=MX; i++) printf "%s%s", $a[i], (i==MX)?RS:OFS
                }
	' fileB fileA

Thanks
# 2  
Old 03-14-2016
Do not understand your request

Are you looking to determine the entries not specified?
return L2 and L3 ??
# 3  
Old 03-14-2016
Your output sample seems to miss the L1 column values. However, try
Code:
awk '
BEGIN           {OFS    = FS = ","}
FNR == NR       {H[$0]  = NR
                 HI[NR] = $0
                 MX     = NR
                 next
                }
FNR == 1        {for (i=1; i<=NF; i++) if ($i in H) A[H[$i]] = i
                }

                {for (i=1; i<=MX; i++) printf "%s%s",  (A[i]?$A[i]:(FNR==1)?HI[i]:"n/a"), (i==MX)?RS:OFS
                }
' file2 file1

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to print multiple required columns dynamically in a file using the header name?

Hi All, i am trying to print required multiple columns dynamically from a fie. But i am able to print only one column at a time. i am new to shell script, please help me on this issue. i am using below script awk -v COLT=$1 ' NR==1 { for (i=1; i<=NF; i++) { ... (2 Replies)
Discussion started by: balu1234
2 Replies

2. Shell Programming and Scripting

Find header in a text file and prepend it to all lines until another header is found

I've been struggling with this one for quite a while and cannot seem to find a solution for this find/replace scenario. Perhaps I'm getting rusty. I have a file that contains a number of metrics (exactly 3 fields per line) from a few appliances that are collected in parallel. To identify the... (3 Replies)
Discussion started by: verdepollo
3 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

Compare two files and find match and print the header of the second file

Hi, I have two input files; file1 and file2. I compare them based on matched values in 1 column and print selected columns of the second file (file2). I got the result but the header was not printed. i want the header of file2 to be printed together with the result. Then i did below codes:- ... (3 Replies)
Discussion started by: redse171
3 Replies

5. UNIX for Dummies Questions & Answers

Find the missing file and print

Hi , i will be getting 24 files for one day with a formate like 20131028_01 - 20131028_24 kind of ,i am trying to write a shell script to count the number of files and if the count is not equal to 24 print the missing files names for A in `seq 0 23`; do ls *20131024_`printf "%02d" $A`*;... (3 Replies)
Discussion started by: vikatakavi
3 Replies

6. Shell Programming and Scripting

awk print header as text from separate file with getline

I would like to print the output beginning with a header from a seperate file like this: awk 'BEGIN{FS="_";print ((getline < "header.txt")>0)} { if (! ($0 ~ /EL/ ) print }" input.txtWhat am i doing wrong? (4 Replies)
Discussion started by: sdf
4 Replies

7. Shell Programming and Scripting

need to print the missing lines in 1 file from 1 file via script

Hi, I am writting a script which will take all the files present in 2 directories. (the 2 directories have the same files with same name but some different content) i need 2 compare 2 files and then copy the contents of file1 to file2. the contents which are missing in file1 and nothing from... (8 Replies)
Discussion started by: dazdseg
8 Replies

8. OS X (Apple)

[GCC] Missing header files

Good morning! I am creating a program that uses raw sockets in C and need to call the "netpacket/packet.h" header file. However when I compile with gcc i get I'm running os 10.5.8 with version gcc 4.0.1 and just update xcode last night which didn't seem to help. Any ideas will be greatly... (2 Replies)
Discussion started by: taiL
2 Replies

9. Shell Programming and Scripting

print lines except the header

awk -F ";" '{if($10>80 && NR>1) print $0 }' txt_file_* I am using this command to print the lines which has 10th field more then 80 and leaving the first line of the file which is the header. But this is not working , the first line is is coming as output , please correct me . thanks (2 Replies)
Discussion started by: madfox
2 Replies

10. Linux

Reading the header of a tar file(posix header)

say i have these many file in a directory named exam. 1)/exam/newfolder/link.txt. 2)/exam/newfolder1/ and i create a tar say exam.tar well the problem is, when i read the tar file i dont find any metadata about the directories,as you cannot create a tar containig empty directories. on the... (2 Replies)
Discussion started by: Tanvirk
2 Replies
Login or Register to Ask a Question