0

I've been doing research for Unix Script these past 2 days. Since I never touch Unix script before and suddenly I need to create Unix script to support my program.

Basically I would like to seek an advise for my case below.

I have a file containing list of Source Filename that I need to load using one of ETL tool.

For example I have VALID_FILENAME.csv and the content like

test1_file.csv 
test2_file.csv 
test3_file.csv

I want to duplicate them into 3 parts, since I’ll have Header, Details and Trailer for each.

My expectation is I will duplicate VALID_FILENAME.csv into 3 files and name them VALID_FILENAME_H, VALID_FILENAME_D and VALID_FILENAME_T.

The content also I’ll replace so it will be like this below.

VALID FILENAME_H:

test1_file_H
test2_file_H
test3_file_H

VALID FILENAME_D:

test1_file_D
test2_file_D
test3_file_D

VALID FILENAME_T:

test1_file_T
test2_file_T
test3_file_T

My question is whether it's possible for me only use cp and rename the files to duplicate VALID_FILENAME.csv to 3 another files? And whether it's also possible to use sed to replace .csv to either H/D/T?

Why I need to separate them into 3 parts is because my ETL tool program will process Header, Detail and Trailer in seperate flow.

Appreciate any suggestions or opinions on what is the best approach to duplicate file, rename it to different name and replace the string inside the file in Unix.

1
  • The best approach imho will depend on how exactly you want to separate the files inot parts (which you haven't shared). For example, if you want to split specific ranges of lines into the _H, _D, _T files, then you can likely do that with a single awk command. May 26, 2020 at 16:40

1 Answer 1

0

Just cp cannot create multiple files with one execution.

Although not very elegant, you can use the following loop to accomplish your goal:

for file in *.csv;do for end in _H _D _T ;do pc $file ${file%.csv}$end;done;rm $file;done

Which basicly just loops through all your csv files, creates the three copies and removes the original.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .