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


Sample output






  • 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 – Siva 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

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.

| improve this answer | |
  • 1
    can you please elaborate, how it works – upkar Apr 26 '19 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 '19 at 20:35
  • Thanks, got it now. – upkar Apr 27 '19 at 22:44
  • Is it possible to create the folder in another directory while re-directing – upkar May 3 '19 at 2:09
  • Certainly. Use awk's system call to run mkdir. – RudiC May 4 '19 at 6:36

Try this:

for i in `cat file.txt`
        File=`echo $i | awk -F '~' '{print "Src_"$5".txt"}'`
        echo "$i" >> $File
  • 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.
| improve this answer | |

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.