-1

I have two files. The content of both files has dynamic and generated by the system when required.

The first file contains the meaning for specific row number as below :

head simdb.txt 
MSISDN
Account_ID
COSP_ID
Currency
Language
Home_Zone
SIM_PIN
Screening_PIN
Third_ParAnothercess_PIN
Cumulative_Incorrect_PIN

Other file contains the dynamic data as below

head subscriber.txt
0='917598936722' 4='ENG' 6='1234'

Output should be like :

0='917598936722'  //MSISDN
4='ENG'           //Language
6='1234'          //SIM_PIN

Question updated========

Adding to the above query, if subscriber.txt would have multiple lines then how can we have a script to print line first then it required output? For example, if we subscriber.txt file like below

head subscriber.txt
0='917598936722' 4='ENG' 6='1234'
0='919654680634' 4='ENG' 6='1234'

Then desired output be like :

0='917598936722' 4='ENG' 6='1234'

0='917598936722'  //MSISDN
4='ENG'           //Language
6='1234'          //SIM_PIN

=========================================

0='919654680634' 4='ENG' 6='1234'

0='919654680634'  //MSISDN
4='ENG'           //Language
6='1234'          //SIM_PIN
0
awk 'NR==FNR
    {Arr[NR-1]=$0;
    next}
    {for(i=1;i<=NF;i++)
    {split($i,a,"="); 
    print $i "    //"Arr[a[1]]}
    }'simdb.txt subscriber.txt
  • read all the lines from simdb.txt and keep in array. Index starts from 0
  • read the second file and check what is the value before equal symbol
  • read the value from Arr and print it.
  • 1
    Due to the linebreak after NR==FNR, this would print all lines from the first file (and nothing else). You also miss a space before the first filename. – Kusalananda Jul 30 '18 at 7:44
  • Thanks Kamaraj. After implementing suggestion from Kusalananda, the command is working fine – Prince Garg Jul 30 '18 at 8:54
0
awk 'FNR==NR { for (i=1; i<=NF; ++i) {
                   split($i, a, "=")
                   s[a[1]+1] = $i }; next }
     FNR in s { printf("%-20s//%s\n", s[FNR], $0) }' subscriber.txt simdb.txt

This first reads the fields in the shorter subscriber.txt file and assigns their values to an array s. The s array is keyed on the number in front of = in each field (plus one).

When the simdb.txt file is read, a test is made on the line number in that file. If that line number is a key in our array s, the desired output is created. The output is done with printf and a formatting string that allocates 20 characters for a left-justified string (the data from simdb.txt) followed by // and the data saved from subscriber.txt for that particular row.

Output given the data in the question:

0='917598936722'    //MSISDN
4='ENG'             //Language
6='1234'            //SIM_PIN

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.