开发者

SPLIT file by Script (bash, cpp) - numbers in columns

开发者 https://www.devze.com 2023-03-26 03:59 出处:网络
I have files with some columns filled by numbers (float). I would need to split these files according to the value in one of the columns (can set as the first one). 开发者_开发技巧This means, when

I have files with some columns filled by numbers (float). I would need to split these files according to the value in one of the columns (can set as the first one). 开发者_开发技巧This means, when

a b c

in my file the value c fullfils 0.05<=c<=0.1 then create the file named c and copy the whole columns there which fullfils the c-condition... is this possible? I can something small with bash, awk, something also with c++. I have searched for some solutions but - I can the data sort of course and only read the first number of the line.. I don't know. Please, very please. Thank you Jane


As you mentioned awk, the basic rule in awk is 'match a line (either by default or with a regexp, condition or line number)' AND 'do something because you found a match'.

awk uses values like $1, $2, $3 to indicate which column in the current line of data it is looking at. $0 refers to the whole line. So ...

awk '
 BEGIN{
    afile="afile.txt"
    bfile="bfile.txt"
    cfile="cfile.txt"
 }

{
  # test c value between .05 and .1
  if ($3 >= 0.05 && $3 <= 0.1) print $0 > cfile
} inputData

Note that I am testing the value of the third column (c in your example). You can use $2 to test b column, etc.

If you don't know about the sort of condition test I have included >= 0.5 && $3 <= 0.1 you'll have some learning ahead of you.

Questions in the form of 1. I have this input, 2. I want this output. 3. (but) I'm getting this output, 4. with this code .... {code here} .... have a much better chance of getting a reasonable response in a reasonable amount of time ;-)

I hope this helps.

P.S. as you appear to be a new user, if you get an answer that helps you please remember to mark it as accepted, and/or give it a + (or -) as a useful answer.


If I understand your requirements correctly:

awk '{print > $3}' file ...
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号