Merging two files to form header

Dear experts required you support to achieve below
i need the file 2 values show on top of file 1 for mentioned values ENTER CREDENTIALS or beside this
please support to achieve the expected result . if I get the result 2 that will be better

I have two files

File 1
ENTER CREDENTIALS
BILLING - ONLINE BILL DETAILS
INTERACTION - SUB ORDERS - VIEW - MANAGED SERVICE
ENTER CREDENTIALS
INTERACTION - SUB ORDERS - VIEW - MANAGED SERVICE
ENTER CREDENTIALS
INTERACTION - SUB ORDERS - VIEW - MANAGED SERVICE
BILLING - ONLINE BILL DETAILS
ENTER CREDENTIALS
File 2
auabc,1234
auabc,5678
auabc,9711
Expected result
auabc,1234
ENTER CREDENTIALS
BILLING - ONLINE BILL DETAILS
INTERACTION - SUB ORDERS - VIEW - MANAGED SERVICE
auabc,5678
ENTER CREDENTIALS
INTERACTION - SUB ORDERS - VIEW - MANAGED SERVICE
auabc,9711
ENTER CREDENTIALS
INTERACTION - SUB ORDERS - VIEW - MANAGED SERVICE
BILLING - ONLINE BILL DETAILS

or (appreciate we can achieve this one )

ENTER CREDENTIALS ,auabc,1234
BILLING - ONLINE BILL DETAILS ,auabc,1234
INTERACTION - SUB ORDERS - VIEW - MANAGED SERVICE ,auabc,1234
ENTER CREDENTIALS  ,auabc,5678
INTERACTION - SUB ORDERS - VIEW - MANAGED SERVICE ,auabc,5678
ENTER CREDENTIALS  ,auabc,9711
INTERACTION - SUB ORDERS - VIEW - MANAGED SERVICE ,auabc,9711
BILLING - ONLINE BILL DETAILS ,auabc,9711

Hi,
maybe :

awk '/^ENTER CREDENTIALS/ { getline line <"file2"} $0=$0" ,"line' file1

But, if you have most block "ENTER CREDENTIALS" that input in file2, the last Blocks will setting with last input of file2.

Regards.

Hello mirwasim,

I have a few to questions pose in response first:-

  • Is this homework/assignment? There are specific forums for these.
  • What have you tried so far?
  • What output/errors do you get?
  • What OS and version are you using?
  • What are your preferred tools? (C, shell, perl, awk, etc.)
  • What logical process have you considered? (to help steer us to follow what you are trying to achieve)
  • What are the rules for moving to the next record in each file?

Most importantly, What have you tried so far?

There are probably many ways to achieve most tasks, so giving us an idea of your style and thoughts will help us guide you to an answer most suitable to you so you can adjust it to suit your needs in future.

We're all here to learn and getting the relevant information will help us all.

Kind regards,
Robin

Also I get different behavior with different awk versions. An unsuccessful getline gives different results. Better check its return status.
Some awk versions have a different opinion when a ($0) is true or false. Better do not rely on it.

awk '{ if (/^ENTER CREDENTIALS/ && getline line <"file2") print ($0 "," line); else print }' file1

or

awk '/^ENTER CREDENTIALS/ && getline line <"file2" { $0=($0 "," line) } 1' file1
1 Like

Ok, but I don't sure that many awk versions fill line variable when getline is unsuccessful...
For ($0) , in worst case, it will setting with string " ," and I think it's always true.

But, I recognize that your first solution is very better :wink: