Splitting records in a text file based on delimiter

A text file has 2 fields (Data, Filename) delimited by # as below,

        Data,Filename

Row1 -> abc#Test1.xml
Row2 -> xyz#Test2.xml
Row3 -> ghi#Test3.xml

The content in first field has to be written into a file where filename should be considered from second field.

So from the above example

  1. Data 'abc' should be written into a file named Test1.xml
  2. Data 'xyz' should be written into a file named Test2.xml
  3. Data 'ghi' should be written into a file named Test3.xml

Is it possible to acheive this using Shellscript, please help me with the logic.

 awk -F'#' ' { print $1 > $2 } ' file
1 Like

Hi, this is the simplest way

cat list.txt
abc#Test1.xml
xyz#Test2.xml
ghi#Test3.xml
cat list.txt|tr -s '#' ' '|while read FIELD FILE; do echo $FIELD > $FILE; done

deleted ... obsolete

Thanks everyone.

i have used the command awk -F'#' ' { print $1 > $2 } ' file and it worked.