moore1emu
moore1emu

Reputation: 496

splitting file1 using AWK and then name the new files based on lines from file2

File 1 has long string of data between <PhotoField1> multiple times.

Example:

<PhotoField1>alidkfjaeijwoeij<PhotoField1>akdfjalskdfasd<PhotoField1>

File 2 has list of IDs that i want to use to label the Files

Example:

A00565415
A00505050
A54531245

I have an AWK command to parse each string between <PhotoField1> from File1 into its own file, but it only labels the files temp with numbers:

awk -v RS="<PhotoField1>" '{ print $0 > "temp" NR }' File1.xml

I need to replace the temp* part with the a line from the 2nd file

So the new files would be named A00565415, A00505050, A54531245, etc.. - it would be excellent if I could add a .txt to the end of the files: A54531245.txt

The awk command works great for separating it into different files, but i need to be able to name them based on the File2 list.

Upvotes: 1

Views: 70

Answers (2)

anubhava
anubhava

Reputation: 786299

You can use this awk:

awk -v RS="<PhotoField1>|\n" 'FNR==NR{a[NR]=$0; next} 
      NF{ print $0 > a[FNR] ".txt" }' file2 file1

Upvotes: 1

Ed Morton
Ed Morton

Reputation: 204676

awk 'NR==FNR{fname[NR]=$0".txt";next} {print > fname[FNR]}' File2.list RS="<PhotoField1>" File1.xml

Upvotes: 2

Related Questions