Merging two files to form header


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Merging two files to form header
# 1  
Old 01-30-2018
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 valuesENTER 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
Code:
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

Code:
File 2
auabc,1234
auabc,5678
auabc,9711

Code:
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

# 2  
Old 01-30-2018
Hi,
maybe :
Code:
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.
# 3  
Old 01-30-2018
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
# 4  
Old 01-30-2018
Quote:
Originally Posted by disedorgue
Hi,
maybe :
Code:
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.
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.
Code:
awk '{ if (/^ENTER CREDENTIALS/ && getline line <"file2") print ($0 "," line); else print }' file1

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

This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 01-30-2018
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 Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Dynamically merging 2 files on header values

Hi All, I have 2 files which i need to merge together based on the column names provided in the file. The first line in both files are header records. The first file has fixed columns but second file can have subset of the columns from file 1 File 1: ... (6 Replies)
Discussion started by: kushagra
6 Replies

2. Shell Programming and Scripting

Merging two files

Hi All , I have a scenario where we need to combine two files . Below are the sample files and expected output , File 1: 1|ab 1|ac 1|ae 2|ad 2|ac File 2: 1|xy 1|fc 2|gh 2|ku Output file : 1|ab|xy (3 Replies)
Discussion started by: saj
3 Replies

3. UNIX for Dummies Questions & Answers

Merge all csv files in one folder considering only 1 header row and ignoring header of all others

Friends, I need help with the following in UNIX. Merge all csv files in one folder considering only 1 header row and ignoring header of all other files. FYI - All files are in same format and contains same headers. Thank you (4 Replies)
Discussion started by: Shiny_Roy
4 Replies

4. UNIX for Dummies Questions & Answers

Merging two files

Hi, I have two files a.txt and b.txt. a.txt 1 2 3 4 b.txt a b c d e I want to generate a file c.txt by merging these two file and the resultant file would contain c.txt 1 (4 Replies)
Discussion started by: siba.s.nayak
4 Replies

5. Shell Programming and Scripting

mget * (obtein files from current directory but not the files form sub-directories)

Hello, Using the instruction mget (within ftp) and with "Interactive mode off", I want to get all files from directory (DirAA), but not the files in sub-directories. The files names don't follow any defined rule, so they can be just letters without (.) period Directory structure example: ... (0 Replies)
Discussion started by: Peter321
0 Replies

6. Shell Programming and Scripting

merging two files

Friends, os: redhat enterprise linux/SCO UNIX5.0 I have two files and I would like to merge on given key value. Now I have tried with join commd but it does not supporte multiple delimiters. and if records length is not fixed. join -a1 5 -a2 1 -t -o file1 file2 > outname Can any... (7 Replies)
Discussion started by: vakharia Mahesh
7 Replies

7. Shell Programming and Scripting

Want to mv files form one folder to another by date

Hi All Looking for some help here I want to query a file and mv all files that are older than seven days to an archive folder. the archive folder already exists but I am having trouble with the date comparison. list file is as follows eg dd2302071334.txt dd2302071358.txt... (7 Replies)
Discussion started by: SummitElse
7 Replies

8. UNIX for Dummies Questions & Answers

Merging two files

Hi I have a requirement like this. I have two files This is how data1.txt looks: EI3171280 38640658501 NENN2005-12-129999-12-312005-12-12HALL NANCY 344 CHENEY HIGHWAY ... (4 Replies)
Discussion started by: venommaker
4 Replies

9. UNIX for Advanced & Expert Users

Changing Unix form to Microsoft Word form to be able to email it to someone.

Please someone I need information on how to change a Unix form/document into a microsoft word document in order to be emailed to another company. Please help ASAP. Thankyou :confused: (8 Replies)
Discussion started by: Cheraunm
8 Replies
Login or Register to Ask a Question