shellunixless-unix

Split file in unix based on similar column values using split command


I have seen many examples using awk command but I wanted to try if this can be done using split command.

For example my file looks like this
Input file

col1    col2    col3                
1   Rahul   IT              
2   Askhay  CS              
3   Teju    IT              
8   Riyansh CO              
1   Rahul   IT              
2   Askhay  CS              

expected file

Output--File1

Col1    col2    col3
1   Rahul   IT
1   Rahul   IT

Output File2

Col1    col2    col3
2   Askhay  CS
2   Askhay  CS

Output file3

Col1    col2    col3                
3   Teju    IT              

Earlier I have used split -d splitrange filename.txt but this command randomly splits files based on splitrange given for eg if it is set to 3 so 3 files will be created with different values but I wanted as per above


Solution

  • No. Read the split manual page. split splits a file based on size or number of lines, not based on a pattern.