Splitting file - custom req.

Hello everybody,

I have a file to split in a bit different way. My file looks like this:

My_file.dat

123333333|01|38|440X|P
384747382|32|31|440X|P
320384743|32|54|420Y|I
843739349|12|43|420Y|I

I need to split it into:

MYFILE_440X_P.dat

123333333|01|38
384747382|32|31

MYFILE_420Y_I.dat

320384743|32|54
843739349|12|43

I mean, the most important are two last columns. They determine file name.
Condition is: take all records that match two last colums, split them into one file and name it like examples above.
I use RHEL 6.1. I try to split it from shell script. Any ideas?

Thanks.
bijou

Well I would use awk...
What have you tried? search the forums, because there are plenty of similar posts here...

Try:

awk -F"|" -v OFS="|" '{x=$4;y=$5;NF=3;print > "MYFILE_"x"_"y".dat"}' My_file.dat
1 Like

I tried to use split command, some loops but I couldn't get the point.
I was also searching on this forum (and some others) but couldn't find anything like this. (fact, there is plenty of similar cases..but not this one).

---------- Post updated at 09:19 AM ---------- Previous update was at 09:15 AM ----------

That works amazing :slight_smile:
Big thanks to You!:):slight_smile:

---------- Post updated at 09:58 AM ---------- Previous update was at 09:19 AM ----------

Let me just ask last question.

I need to add a current date YYYYMMDD to filename. So, finally it should be:

MYFILE_YYYYMMD_x_y.dat