Anand Abhay
Anand Abhay

Reputation: 359

Split data into separate files unix

I have data in below format

 _id :  ANC,Name : TEST,actn : Testing,date : 2018-0208   |    _id :  ANC,Name : TEST,actn : Testing,date : 2018-0208
                                                          >    _id :  ANC,Name : TEST,actn : Testing,date : 2018-0209
 _id :  ANC,Name : TEST,actn : Testing,date : 2018-0210   <   

I want to split data into separate files based following condition :--

anything before  | should go in file 1 and after | should go in file 2.
Anything after > should in file 2 
Anything before < should go in file 1

So at the end files would look like :-- File1:-- _id : ANC,Name : TEST,actn : Testing,date : 2018-0208 _id : ANC,Name : TEST,actn : Testing,date : 2018-0210 File2 _id : ANC,Name : TEST,actn : Testing,date : 2018-0208 _id : ANC,Name : TEST,actn : Testing,date : 2018-0209

I tried doing it using sed sed 's/|.*//' test.txt but unfortunately I am not able to add all conditions so data is getting messed.

Regards.

Upvotes: 0

Views: 51

Answers (3)

RomanPerekhrest
RomanPerekhrest

Reputation: 92904

Awk solution:

awk '{ for (i=1; i<=2; i++) if ($i) print $i > "file"i }' \
       FS='[[:space:]][[:space:]]+[|<>][[:space:]][[:space:]]+' file

Viewing results:

$ head file[12]
==> file1 <==
_id :  ANC,Name : TEST,actn : Testing,date : 2018-0208
 _id :  ANC,Name : TEST,actn : Testing,date : 2018-0210

==> file2 <==
_id :  ANC,Name : TEST,actn : Testing,date : 2018-0208
_id :  ANC,Name : TEST,actn : Testing,date : 2018-0209

Upvotes: 0

RavinderSingh13
RavinderSingh13

Reputation: 133770

Following simple awk may help you on same too.

awk -F'[|><]' '{gsub(/^ +| +$/,"")}$1{print $1 > "file1"} $2{print $2 > "file2"}'    Input_file

Upvotes: 1

kabanus
kabanus

Reputation: 26005

One way using, awk, since you essentially have two columns (assuming no other|,<,>):

awk -F' *[<>|] *' '{if ( $1 != "" ) { print $1 > "file1"; }; if ( $2 != "") { print $2 > "file2" } }' inputfile
  1. -F is setting the delimiter to one of the 3 special symbols, with any amount of apace after and before.
  2. If the first column is not empty, print it to file 1.
  3. If the second column is not empty, print it to file 2.

If you don't mind a few steps you can replace the delimiters with a single delimiter:

sed -i 's/[<>]/|/' input

and then just using cut -d'|' -f1 > file1 would work. Same for file 2 - though you would have empty lines. Also you can use a bash loop and easily iterate line by line splitting the line n one of the delimiters, but I think awk here is well suited.

Upvotes: 1

Related Questions