Pages

2017年4月14日 星期五

awk string parsing uses shell variables

I need to parse user input parameters in shell file, so I do some research for it..

1.shell parameter format:
VAR=$(expr.)


2. awk function to replace string:  gsub("target string","replace string")
   sed way:                                      sed 's/target string/replace string/g'

3. awk use shell parameters : -v
   str1="shell value"
   echo awk -v S_RN=${str1} '{ print S_RN}'

Through 1 to 3 steps, we can use parameters from user input in awk function. 
See below example:
command line:
./test.sh "-i $RN -c -a"

test.sh:
RN=$RANDOM

FLAG=$(echo $1| awk -v S_RN=${RN} '{gsub("\$RN",S_RN);print}')

沒有留言:

張貼留言

 
 
Blogger Templates