split file depending on content


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting split file depending on content
# 1  
Old 11-14-2007
split file depending on content

Hi,
I have a file which contains records of data.
I need to split the file into multiple files depending upon the value of last field.
How do i read the last field of each record in the file???

Regards,
Chaitrali
# 2  
Old 11-14-2007
what is the record separator in your file?

give some sample data in order to receive good responses
# 3  
Old 11-14-2007
Hi Yogesh,

The record is tab separated......


Rgds,
Chairali.
# 4  
Old 11-14-2007
In this case the last field would be matched by a regexp (replace "<tab>" by a literal tab character):

sed 's/.*<tab>//' file

The reason why this works is because Unix regexps are "greedy": always the longest possible match is used. If you have several tabs in one input line ".*<tab>" will match all possible characters (including tabs!) followed by a tab char, which will be the rightmost one. Be sure to have no trailing tabs at the end of the line, though, as in this case the regexp would match the whole line.

To extract the rightmpost field from the line and split the file linewise into different files use something like:

Code:
cat file | while read line ; do
     lastfield="$(print $line | sed 's/.*<tab>//')"
     case $lastfield in
          abc)
               print - "$line" >> file1
               ;;

           def)
               print - "$line" >> file2
               ;;

           ghi)
               print - "$line" >> file3
               ;;

           *)
               print -u2 "i do not know where to put $line"
               ;;

     esac
done

bakunin

Last edited by bakunin; 11-14-2007 at 09:19 AM..
# 5  
Old 11-14-2007
Quote:
How do i read the last field of each record in the file???
Code:
awk -F"\t" '{ print $NF }' filename

to split files based on the last field value using the last field value as the split_filename

Code:
awk -F"\t" '{ print > $NF }' filename

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Split a non delimited file into columns depending on user input

I would like some advice on some code. I want to write a small script that will take an input file of this format 111222233334444555666661112222AAAA 2222333445556612323244455445454545 2334556345643534505435345353453453 (and so on) It will be called as : script inputfile X (where X is... (5 Replies)
Discussion started by: onlyforbopi
5 Replies

2. Shell Programming and Scripting

Split a file in more files based on score content

Dear All, I have the following file tabulated: ID distanceTSS score 8434 571269 10 10122 393912 9 7652 6 10 4863 1451 9 8419 39 2 9363 564 21 9333 7714 22 9638 8334 9 1638 1231 11 10701 918 1000 6587 32056 111 What I would like to do is the following, create 100 new files based... (5 Replies)
Discussion started by: paolo.kunder
5 Replies

3. Shell Programming and Scripting

Split file depending on Column Value

Hi , my file look likes below , cat file.csv 12/09/2014,50,5,0,300 12/09/2014, ,5,0,300 12/09/2014,50,,,300 i need to split file , the first one contains values (2nd column is 50 , 3rd and fourth column is null ) the second file contains all others firstfile ... (2 Replies)
Discussion started by: ubaisalih
2 Replies

4. Shell Programming and Scripting

How to Split File to 2 depending on condition?

Hi , cat myfile.txt ! 3100.2.0.5 ! 3100.2.22.4 ! 3100.2.30.33 ! 3100.2.4.1 ! ! 3100.2.0.5 ! 3100.2.22.4 ! 3100.2.22.11 ! 3100.2.4.1 ! ! 3100.2.0.5 ! 3100.2.2.50 ! 3100.2.22.11 ! 3100.2.4.1 ! ! 3100.2.0.5 ! 3100.2.22.4 ! 3100.2.30.33 ! 3100.2.4.1 ! ! 3100.2.0.5 ! 3100.2.22.4 !... (6 Replies)
Discussion started by: OTNA
6 Replies

5. Shell Programming and Scripting

Parsing files depending on the content

I am trying to parse files kkapjil kkpcjil kkexjil ...which have autosys job names. The objective is to parse each file...do an autorep -j <job name > -q and write it as output with a line for condition at the end of the each job. The problem I am facing is with the box jobs...as autorep -j <box... (0 Replies)
Discussion started by: w020637
0 Replies

6. Shell Programming and Scripting

split file content into specific folders

Hi I have a large text file and I want to split its content into multiple flies. this large file contains several blocks of codes separated by a comment line for each block. this comment line represents a directory path So, when separate these blocks each into a separate file, This output... (7 Replies)
Discussion started by: turki_00
7 Replies

7. Shell Programming and Scripting

Split the file based on the content

Arun kumar something somehting Enterting in to the line . . . . Some text text Finshing the sentence Some other text . . . . Again something somehting Enterting in to the line . . . . . . Again text text Finshing the sentence (6 Replies)
Discussion started by: arukuku
6 Replies

8. Shell Programming and Scripting

How to split a data file into separate files with the file names depending upon a column's value?

Hi, I have a data file xyz.dat similar to the one given below, 2345|98|809||x|969|0 2345|98|809||y|0|537 2345|97|809||x|544|0 2345|97|809||y|0|651 9685|98|809||x|321|0 9685|98|809||y|0|357 9685|98|709||x|687|0 9685|98|709||y|0|234 2315|98|809||x|564|0 2315|98|809||y|0|537... (2 Replies)
Discussion started by: nithins007
2 Replies

9. Shell Programming and Scripting

split file content

Hi All; I have input file like below name char(3) number number(3) inputfile namenumber xyz123abc509kai330 aca203 ald390afa000als303 I wanted to split like below:- output like this:- xyz123 abc509 kai330 aca203 ald390 (6 Replies)
Discussion started by: Jairaj
6 Replies

10. Shell Programming and Scripting

Split file into multiple files depending upon first 4 digits

Hi All, I have a file like below: 1016D"ddd","343","1299" 1016D"ddd","3564","1299" 1016D"ddd","3297","1393" 1016D"ddd","32989","1527" 1016D"ddd","346498","1652" 2312D"ddd","3269","1652" 2312D"ddd","328","1652" 2312D"ddd","2224","2100" 3444D"ddd","252","2100" 3444D"ddd","2619","2100"... (4 Replies)
Discussion started by: deepakgang
4 Replies
Login or Register to Ask a Question