help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] split


From: Pierre Gaston
Subject: Re: [Help-bash] split
Date: Tue, 29 May 2018 16:22:56 +0300

On Sat, May 26, 2018 at 8:28 PM, Val Krem <address@hidden> wrote:

> Hi All,
> I wanted to split a big file based on the value of the first column of
> thsi file
> file 1 (atest.dat).1 ah1 251 ah2 26
> 4 ah5 354 ah6 362 ah2 54
> I want to split this file into three files based on the first column
>  file 1 will be
>      1 ah1 25     1 ah2 26
> file 2 would be    4 ah5 35    4 ah6 36
> file three would be     2 ah2 54
> The range of the first column could vary from 1 up to 100.
> I trad  the following script
> ################################################
> #! bin/bash
> numb=($(seq 1 1 10))
> for i in "address@hidden"
>    do
>      awk '{if($1=='"${i}"') print $0}' atest.dat   > numb${i}.txt
>    done
> #################################################
>
> The above script gave me 10  files while I was expecting only 3  files.
> How do I limit to get only the three files that do have data in atest.dat
> file?
>
>
awk can do the redirection, you may hit a problem if the number of open
files is too high, but with gnu awk you should be fine.

awk  '{print > ("numb" $1 ".txt")}' atest.dat

i'd suggest you use padding for the number so that lexical sorting works
e.g.:

awk  '{print > (sprintf("numb%03d.txt", $1))}' atest.dat


reply via email to

[Prev in Thread] Current Thread [Next in Thread]