How to use grep command in a loop in gnuscript

二次信任 提交于 2019-12-24 07:02:07

问题


sorry, I am asking multiple questions. I have a case.dat file which is having multiple columns and I want to extract array of the data according to colum 2 in the gnuscript. I tried with below script but it is giving me error

array=""
do for [i=300:800:100] {  # I mean start:end:increment. so it should read 300, 400, 500, 600, 700, 800 here
val ="grep i case.dat"        # Want to store the command in a valuel/variable
print val > i.dat              #Here I need to store the data in i.dat
}

error

line 45: undefined variable: dat

my bash script is like below

##!/bin/bash
case="data"
for i in `seq 100 100 800`
do
       awk '$2=='$i'{print $0}' $case.dat > $i.dat
done

that I want to use at the start of the gnu-script so that the further operation can be done in the rest part of the gnu-script.

回答1:


gnuplot script:

do for [i=300:800:100] {
   outfile = sprintf("%d.dat", i)
   command = sprintf("grep %d case.dat",i)
   set print outfile
   print system(command)
   unset print
}

This will create separate files 300.dat 400.dat 500.dat and so on.

If you want to keep these data subsets entirely internal to gnuplot, i.e. not create any new files, you could instead create named datablocks $data_300 $data_400 etc:

do for [i=300:800:100] {
    eval( sprintf("set print $data_%3d", i) )
    print( sprintf("grep %d case.dat") )
    unset print
}

Named datablocks can in general be used anywhere you could use a file name, e.g. plot $data_500 with lines.



来源:https://stackoverflow.com/questions/58049022/how-to-use-grep-command-in-a-loop-in-gnuscript

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!