absolute
absolute

Reputation: 37

How to use grep command in a loop in gnuscript

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.

Upvotes: 2

Views: 191

Answers (1)

Ethan
Ethan

Reputation: 15118

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.

Upvotes: 3

Related Questions