bash - Print out shell variables and "" in awk/sed script using echo/printf -


a printout question: need print out following awk .sh file. (remove row has "0" in 12th column)

awk -f, '$12 != "0"' output.csv >> output2.csv 

here our script (the case loop necessary purpose. small step of bigger script. assume $targetids=123):

case $targetids in ($p1) echo "awk -f, '$12 != "0"' ${targetids}_output.csv >> ${targetids}_output2.csv" >> output.sh;  (*)  ;;  esac 

this print out following in "output.sh". "$1" in "$12" disappears : (

awk -f, '2 != "0"' 123_output.csv >> 123_output2.csv 

we try array:

 v="12"   b=($v)  case $targetids in ($p1) echo "awk -f, '${b[1]} != "0"' ${targetids}_output.csv >> ${targetids}_output2.csv" >> output.sh;  (*)  ;;  esac 

this print out following in "output.sh". $12 disappear:

    awk -f, ' != 0' output.csv >> output2.csv 

the second question similar: use sed way:

we want print out sed:

    sed -i.temp '/"0"/d' 123_output.csv.temp     mv 123_output.csv.temp 123_output.csv 

here script

    case $targetids in ($p1) printf "sed -i.temp '/"0"/d' ${targetids}_output.csv.temp\n mv ${targetids}_output.csv.temp ${targetids}_output.csv\n" >> output.sh ;;      (*)  ;;      esac 

this print out following in "output.sh":

sed -i.temp '/0/d' 123_output.csv.temp mv 123_output.csv.temp 123_output.csv 

the sed command becomes in "output.sh"

sed -i.temp '/0/d' 

instead of

sed -i.temp '/"0"/d' 

"" gone in "output.sh" , hence rows have 0 removed

sed -i.temp '/0/d' 

wonder if gurus might have solutions this? thanks!

try this:

echo "awk -f, '\$12 != 0' ${targetids}_output.csv >> ${targetids}_output2.csv" 

this of course assumes treating column 12 numeric value.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -