Solution which is works perfect if all headers match from file b


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Solution which is works perfect if all headers match from file b
# 1  
Old 07-05-2019
Solution which is works perfect if all headers match from file b

HI Guys,

I have file A.txt

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

File B.txt

Code:
Code:
ID
L1
L4
L5

Output:-

Code:
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:
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 07-05-2019
And the question is?
# 3  
Old 07-05-2019
In nawk you can get a $() error, so you better cast a[i] to a number: $(a[i]+0)

Now it is portable, but you do not want $(0) (the whole line), you want "n/a".
The following expression does it: (i in a)?$a[i]:"n/a"

Last edited by MadeInGermany; 07-05-2019 at 01:46 PM.. Reason: Expression that gives "n/a"
# 4  
Old 07-07-2019
Wildly guessing what your request might be, I came up with
Code:
awk '
BEGIN           {OFS = FS = ","}
FNR == NR       {h[$0] = NR
                 T[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", T[i], (i==MX)?RS:OFS
                 next
                }

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

How far would that get you?
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Keep only the closet match of timestamped row (include headers) from file1 to precede file2 row/s

This is a question that is related to one I had last August when I was trying to sort/merge two files by millsecond time column (in this case column 6). The script (below) that helped me last august by RudiC solved the puzzle of sorting/merging two files by time, except it gets lost when the... (0 Replies)
Discussion started by: aachave1
0 Replies

2. UNIX for Beginners Questions & Answers

Keep only the closet match of timestamped row (include headers) from file1 to precede file2 row/s

My original files are like this below and I distinguish them from the AP_ID (file1 has 572 and file2 has 544). Also, the header on file1 has “G_” pre-pended. NOTE: these are only snippets of very large files and much of the data is not present here. Original File 1: ... (36 Replies)
Discussion started by: aachave1
36 Replies

3. Solaris

Samba idmap ldap: works perfect on Linux,bad on Solaris and hpux

I have configured samba for working with and external ldap(ad windows2003+openldap backend to obtain the same uid and gid on all linux machines) On linux works perfect,and i get the same uid for a X user on all machines. On solaris11 and hpux 11.31 not wbinfo -u works fine wbinfo -g works... (0 Replies)
Discussion started by: Linusolaradm1
0 Replies

4. Shell Programming and Scripting

Remove headers thar dont match

Good evening I need your help please, im new at Unix and i wanted to remove the first 5 headers for 100000 records files and then create a control file .ctl that contains the number of records and all seem to work out but when i tested at production it didnt wotk. Here is the code: #!... (6 Replies)
Discussion started by: alexcol
6 Replies

5. Shell Programming and Scripting

Merging of files with different headers to make combined headers file

Hi , I have a typical situation. I have 4 files and with different headers (number of headers is varible ). I need to make such a merged file which will have headers combined from all files (comman coluns should appear once only). For example - File 1 H1|H2|H3|H4 11|12|13|14 21|22|23|23... (1 Reply)
Discussion started by: marut_ashu
1 Replies

6. Shell Programming and Scripting

Remove text between headers while leaving headers intact

Hi, I'm trying to strip all lines between two headers in a file: ### BEGIN ### Text to remove, contains all kinds of characters ... Antispyware-Downloadserver.com (Germany)=http://www.antispyware-downloadserver.c om/updates/ Antispyware-Downloadserver.com #2... (3 Replies)
Discussion started by: Trones
3 Replies

7. Shell Programming and Scripting

extraction of perfect text from file.

Hi All, I have a file of the following format. <?xml version='1.0' encoding='utf-8'?> <tomcat-users> <role rolename="tomcat"/> <role rolename="role1"/> <role rolename="manager"/> <role rolename="admin"/> <user username="tomcat" password="tomcat" roles="tomcat"/> <user... (5 Replies)
Discussion started by: nua7
5 Replies
Login or Register to Ask a Question