separate a file using a tag 1

separate a file using a tag

 cat file.txt | awk -v RS="your_tag" '{ print $0 > "structure" NR }'
 this tend to create empty files / useless files after the final tag, so remove them as below 
 $5==0 $5 is file size column, 0 is to empty sometime it may be 2,3 kb, add file size accrodingly 
 ls -ltr structure* | awk '{if($5==0) system("rm "$9)}' 

Here is what the above code is Doing:
1. The -v RS=”your_tag” tells awk to use your_tag as the record separator.
2. The ‘{ print $0 > “structure” NR }’ tells awk to print the current record to a file named structure followed by the current record number.
3. The ls -ltr structure* | awk ‘{if($5==0) system(“rm “$9)}’ removes any empty files.

Similar Posts