Shell script to match row number from one file and put it as comments in another file when match

Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
-1
down vote
favorite
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
linux shell-script shell scripting file-descriptors
add a comment |Â
up vote
-1
down vote
favorite
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
linux shell-script shell scripting file-descriptors
add a comment |Â
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
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
linux shell-script shell scripting file-descriptors
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
linux shell-script shell scripting file-descriptors
edited Jul 30 at 11:07
asked Jul 30 at 5:37
Prince Garg
11
11
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
0
down vote
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 afterNR==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 at 7:44
Thanks Kamaraj. After implementing suggestion from Kusalananda, the command is working fine
â Prince Garg
Jul 30 at 8:54
add a comment |Â
up vote
0
down vote
awk 'FNR==NR for (i=1; i<=NF; ++i)
split($i, a, "=")
s[a[1]+1] = $i ; next
FNR in s printf("%-20s//%sn", 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
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
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 afterNR==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 at 7:44
Thanks Kamaraj. After implementing suggestion from Kusalananda, the command is working fine
â Prince Garg
Jul 30 at 8:54
add a comment |Â
up vote
0
down vote
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 afterNR==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 at 7:44
Thanks Kamaraj. After implementing suggestion from Kusalananda, the command is working fine
â Prince Garg
Jul 30 at 8:54
add a comment |Â
up vote
0
down vote
up vote
0
down vote
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.
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.
answered Jul 30 at 7:06
Kamaraj
2,5341312
2,5341312
1
Due to the linebreak afterNR==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 at 7:44
Thanks Kamaraj. After implementing suggestion from Kusalananda, the command is working fine
â Prince Garg
Jul 30 at 8:54
add a comment |Â
1
Due to the linebreak afterNR==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 at 7:44
Thanks Kamaraj. After implementing suggestion from Kusalananda, the command is working fine
â Prince Garg
Jul 30 at 8:54
1
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 at 7:44
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 at 7:44
Thanks Kamaraj. After implementing suggestion from Kusalananda, the command is working fine
â Prince Garg
Jul 30 at 8:54
Thanks Kamaraj. After implementing suggestion from Kusalananda, the command is working fine
â Prince Garg
Jul 30 at 8:54
add a comment |Â
up vote
0
down vote
awk 'FNR==NR for (i=1; i<=NF; ++i)
split($i, a, "=")
s[a[1]+1] = $i ; next
FNR in s printf("%-20s//%sn", 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
add a comment |Â
up vote
0
down vote
awk 'FNR==NR for (i=1; i<=NF; ++i)
split($i, a, "=")
s[a[1]+1] = $i ; next
FNR in s printf("%-20s//%sn", 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
add a comment |Â
up vote
0
down vote
up vote
0
down vote
awk 'FNR==NR for (i=1; i<=NF; ++i)
split($i, a, "=")
s[a[1]+1] = $i ; next
FNR in s printf("%-20s//%sn", 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
awk 'FNR==NR for (i=1; i<=NF; ++i)
split($i, a, "=")
s[a[1]+1] = $i ; next
FNR in s printf("%-20s//%sn", 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
answered Jul 30 at 7:28
Kusalananda
101k13199311
101k13199311
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f459255%2fshell-script-to-match-row-number-from-one-file-and-put-it-as-comments-in-another%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password