Hi i have a text file sampple.txt as
=====record1
title:javabook
price:$120
author:john
path:d:
=====record2
title:.netbook
author:paul
path:f:
=====record3
author:john
title:phpbook
subject:php
path:f:
price:$150
=====record4
title:phpbook
subject:php
path:f:
price:$150
from this i want to split the data based on author, it should split into 2 files which contains
test1.txt
=====record1
title:javabook
price:$120
author:john
path:d:
=====record3
author:john
title:phpbook
subject:php
path:f:
price:$150
and
test2.txt
=====record2
title:.netbook
author:paul
path:f:
like above i want to classify the main sample.txt file into sub files based on author field dynamically please suggest me a way to do it.
try this line:
awk -F: '{a[++i]=$0;if(i==3)f=$2}i==4{for(x=1;x<=i;x++)print a[x]>f".txt";i=0}' file
this line will name the output file with the author's name. And if the name has space, you have to quote it. If you need a numbered text file, it is also easy, just create an array during reading lines. something like:
f['John']=1
f['Tom']=2
f['Jerry']=3
...
this line is just show how would it work.
with your content in file
as example:
kent$ awk -F: '{a[++i]=$0;if(i==3)f=$2}i==4{for(x=1;x<=i;x++)print a[x]>f".txt";i=0}' file
kent$ head *.txt
==> john.txt <==
=====record1
title:javabook
author:john
path:d:
=====record3
title:javabook
author:john
path:f:
==> paul.txt <==
=====record2
title:.netbook
author:paul
path:f:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.