0

i have this script in bash:

#!/bin/bash
dir="/home/dortiz/Prueba"
for i in $dir/*
do
cat $i | awk '{print $1"  " $2"    " $3"    " $4"\n " $5}' | \
         awk '/gi/{print ">" $0; getline; print}' | \
         awk '$3>20.00 {print $0; getline; print;}' \
         >  "${i}.outsel"
done         
cd /home/dortiz/Prueba
mv *.outsel /home/dortiz/Prueba2

and i would like to set an argument to change the value after ""awk '$3>"" in an easy way from my main program that will call this script.

i have read something about getopts but i dont uderstand it at all

Thanks a lot in advance

Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
Dani
  • 91
  • 1
  • 1
  • 6
  • There is usually no need to pipe awk to awk: `awk | awk | awk`. And you can set variables with `-v var=xxx`: `awk -v my_var=1234 'BEGIN {print my_var}'` – Andreas Louv Jun 13 '16 at 12:20

1 Answers1

0

The simplest way is to just pass an argument to your script:

yourscript.sh 20.0

Then in your script

#!/bin/bash
value=$1    # store the value passed in as the first parameter.
dir="/home/dortiz/Prueba"
for i in $dir/*; do
    awk '{print $1"  " $2"    " $3"    " $4"\n " $5}' "$i" |
    awk '/gi/{print ">" $0; getline; print}' | 
    awk -v val="$value" '$3>val {print $0; getline; print;}' >  "${i}.outsel"
    #   ^^^^^^^^^^^^^^^
done
...

and the cat|awk|awk|awk pipeline can probably be written like this:

awk -v val="$value" '
    $3 > val {
        prefix = /gi/ ? ">" : ""
        print prefix $1 "  " $2"    " $3"    " $4"\n " $5
    }
' "$i" > "$i.outsel"
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • Thank you very much! at the end i understood how to use getopts by my own and i think is a better option because i will have to add more arguments in the future and maybe is more practical. – Dani Jun 15 '16 at 09:30
  • I didnt know the thing about the pipeline in awk, thanks! – Dani Jun 15 '16 at 09:31