0

I have a text file that has 6 fields delimited by ~. I need to write the lines to multiple new files whose names are based on the 5th field in the line, such that all lines with the same 5th field end up in the same file. For example, all lines with foo in the 5th field should end up in Src_foo.txt.

Sample input

WatchListEntry_20180820_DLY_0.dat~3~NA~USD~AMLCOMP~2018-08-20~
WatchListEntry_20180820_DLY_2.dat~3~NA~USD~CBNABI~2018-08-20~
WatchListEntry_20180820_DLY_99.dat~3~NA~USD~AMLDW~2018-08-20~
BackOfficeTransaction_20180820_DLY_1.dat~5~0~USD~CBNABI~2018-08-

Sample output

Src_AMLCOMP.txt

WatchListEntry_20180820_DLY_0.dat~3~NA~USD~AMLCOMP~2018-08-20 

Src_AMLDW.txt

WatchListEntry_20180820_DLY_99.dat~3~NA~USD~AMLDW~2018-08-20

Src_CBNABI.txt

WatchListEntry_20180820_DLY_2.dat~3~NA~USD~CBNABI~2018-08-20
BackOfficeTransaction_20180820_DLY_1.dat~5~0~USD~CBNABI~2018-08-20
  • 4
    I'm voting to close this question as off-topic because question on SE Unix & Linux should show some proof of work. This question show none. – Kiwy Sep 6 '18 at 12:10
  • @Chandrakala can you cross check your expected output. Src_CBNABI.txt: must have one line – msp9011 Sep 6 '18 at 12:20
  • Welcome to Unix Stackexchange! You can take the tour first and then learn How to Ask a good question. That makes it easier for us to help you. – andcoz Sep 6 '18 at 12:36
  • Please, edit your question and show us what you tried so far. – andcoz Sep 6 '18 at 12:36
4

How far would

awk -F"~" '{print >> ("Src_" $5 ".txt")}' file

get you? You may need to close the output files if you get near the maximum number of open files allowed.

  • can you please elaborate, how it works – upkar Apr 26 at 17:52
  • As the fifth field holds the essential part of the target file name, the latter is contructed from it and then used for the redirection of the print statement, which by default prints $0, the entire input line. – RudiC Apr 27 at 20:35
  • Thanks, got it now. – upkar Apr 27 at 22:44
  • Is it possible to create the folder in another directory while re-directing – upkar May 3 at 2:09
  • Certainly. Use awk's system call to run mkdir. – RudiC May 4 at 6:36
1

Try this:

for i in `cat file.txt`
do
        File=`echo $i | awk -F '~' '{print "Src_"$5".txt"}'`
        echo "$i" >> $File
done
  • pass the content of file.txt line by line to for loop.
  • extract the 5th field and Src_ as prefix and .txt as the suffix to declare it as filename
  • then pass the content to the file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.