Splitting file - custom req.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Splitting file - custom req.
# 1  
Old 03-25-2014
Lightbulb [Solved] 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
Code:
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
Code:
123333333|01|38
384747382|32|31

MYFILE_420Y_I.dat
Code:
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


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data. Thanks
# 2  
Old 03-25-2014
Well I would use awk...
What have you tried? search the forums, because there are plenty of similar posts here...
# 3  
Old 03-25-2014
Try:
Code:
awk -F"|" -v OFS="|" '{x=$4;y=$5;NF=3;print > "MYFILE_"x"_"y".dat"}' My_file.dat

This User Gave Thanks to bartus11 For This Post:
# 4  
Old 03-25-2014
Quote:
Originally Posted by vbe
Well I would use awk...
What have you tried? search the forums, because there are plenty of similar posts here...
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 ----------

Quote:
Originally Posted by bartus11
Try:
Code:
awk -F"|" -v OFS="|" '{x=$4;y=$5;NF=3;print > "MYFILE_"x"_"y".dat"}' My_file.dat

That works amazing Smilie
Big thanks to You!SmilieSmilie

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

Quote:
Originally Posted by bartus11
Try:
Code:
awk -F"|" -v OFS="|" '{x=$4;y=$5;NF=3;print > "MYFILE_"x"_"y".dat"}' My_file.dat

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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Splitting week start date and end date based on custom period start dates

Below are my custom period start and end dates based on a calender, these dates are placed in a file, for each period i need to split into three weeks for each period row, example is given below. Could you please help out to achieve solution through shell script.. File content: ... (2 Replies)
Discussion started by: nani2019
2 Replies

2. Shell Programming and Scripting

Sort file data according to a custom list of string

I have a string of pre defined ip address list which will always remain constant their order will never change like in below sample: iplist=8.8.5.19,9.7.5.14,12.9.9.23,8.8.8.14,144.1.113 In the above example i m considering only 5 ips but there could be many more. Now i have a file which... (15 Replies)
Discussion started by: mohtashims
15 Replies

3. Shell Programming and Scripting

Curl and write custom result to file

I have the following script, for i in $(cat MyWebApps); do curl -u manager:tH1s1s4f3k3p4ssw0Rd http://10.10.10.10:7529/manager/jmxproxy/"?get=Catalina:type=Manager,context=/$i,host=localhost&att=activeSessions"; done Which gives me an output like this OK - Attribute get... (12 Replies)
Discussion started by: charli1
12 Replies

4. UNIX for Beginners Questions & Answers

How do I custom sort the files in a directory using the filenames in a text file.?

Hi all, (5 Replies)
Discussion started by: KMusunuru
5 Replies

5. Shell Programming and Scripting

Sort and Split file with header and custom name

Hi, I am using SUN SOLARIS (SunOS sun4v sparc SUNW, T5240). I have a huge data file with header and trailer. This file gets used into an ETL process. ETL skips the header record (which is the first record of the file) and loads the rest of the record. The file can be delimited (comma,... (5 Replies)
Discussion started by: Saanvi1
5 Replies

6. UNIX for Dummies Questions & Answers

How to create a TAR File in a Custom Destination Directory?

I want to create the tarzip file into a destination directory, i am in /var/sftp/home/archive/rquadri directory and i am using below command. However it is creating the file in the /var/sftp/home/archive/rquadri directory itself instead of /tmp, may i please know how do i resolve this. tar -cvzf... (5 Replies)
Discussion started by: Ariean
5 Replies

7. UNIX for Dummies Questions & Answers

Split a file and give custom names

Hello, I want to split a file based on an input list file that contains the lines each split should have + a corresponding file name. #!/bin/sh # sed -n 'start_line_#,end_line_#p' my_input_file > lines_extracted_output_file while read a b c do sed -n '$a,$bp' myLarge.file > $c.split... (2 Replies)
Discussion started by: Gussifinknottle
2 Replies

8. Shell Programming and Scripting

Populating File data with custom manipulation on file names

Hi, I am confused how to proceed firther please find the problem below: Input Files: DCIA_GEOG_DATA_OCEAN.TXT DCIA_GEOG_DATA_MCRO.TXT DCIA_GEOG_DATA_CVAS.TXT DCIA_GEOG_DATA_MCR.TXT Output File Name: MMA_RFC_GEOG_NAM_DIM_LOD.txt Sample Record(DCIA_GEOG_DATA_OCEAN.TXT):(Layout same for... (4 Replies)
Discussion started by: Arun Mishra
4 Replies

9. Shell Programming and Scripting

(Req)Shell script req

HI All Im new to shell scripting...kindly plz help me... I need a shell script for: We have to take export of all db's on daily basis from all svr's and keep these export backups on diffrent server. Plz help. Regards Gaurav (5 Replies)
Discussion started by: ergauravtaneja
5 Replies

10. Filesystems, Disks and Memory

veritas vcs \ file system docs req.

hi if anyone has docs (corporate internals on how to setup vcs mfor example, especially with Veritas VM, pleas let me know..i need exposure experience in this area of Enterprise Administration thanks jasmine (SCSA & MCSE) (2 Replies)
Discussion started by: jasmine_uk
2 Replies
Login or Register to Ask a Question