简体   繁体   English

使用echo / printf在awk / sed脚本中打印出shell变量和“”

[英]Print out shell variables and “” in awk/sed script using echo/printf

A printout question: We need to print out the following awk into a .sh file. 一个打印输出问题:我们需要将以下awk打印到.sh文件中。 (Remove all the row that has "0" in the 12th column) (删除第12列中所有具有“ 0”的行)

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

Here is our script (The case loop is necessary for another purpose. This is a small step of a bigger script. Assume $TargetIDs=123): 这是我们的脚本(case循环对于另一个目的是必要的。这是较大脚本的一小步。假设$ TargetIDs = 123):

case $TargetIDs in ($P1) echo "awk -F, '$12 != "0"' ${TargetIDs}_output.csv >> ${TargetIDs}_output2.csv" >> output.sh;

(*)  ;;

esac

This will print out the following in the "output.sh". 这将在“ output.sh”中打印以下内容。 The "$1" in "$12" disappears : ( “ $ 12”中的“ $ 1”消失:(

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

We try an 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 will print out the following in the "output.sh". 这将在“ output.sh”中打印以下内容。 $12 disappear: $ 12消失:

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

The second question is similar: we use a sed way: 第二个问题是相似的:我们使用sed方法:

We want to print out this sed: 我们要打印此sed:

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

Here is the 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 will print out the following in the "output.sh": 这将在“ output.sh”中打印出以下内容:

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

The sed command becomes this in the "output.sh" sed命令在“ output.sh”中变为

sed -i.temp '/0/d'

instead of 代替

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

"" is gone in the "output.sh" and hence all the rows that have zero will be removed by “”在“ output.sh”中消失了,因此所有具有零的行将被删除

sed -i.temp '/0/d'

Wonder if gurus might have some solutions for this? 想知道大师是否可能对此有一些解决方案? Thanks! 谢谢!

You need to watch your quoting. 您需要注意报价。 Inside double quotes, single quotes aren't special, but dollar signs are. 在双引号中,单引号并不特殊,但美元符号则不然。 The nice bit is that you can concatenate multiple quoted segments, so you can change between different quoting styles in the same parameter. 令人高兴的是,您可以连接多个带引号的句段,因此可以在同一参数的不同引用样式之间进行更改。

Try this: 尝试这个:

echo "awk -F, '"'$12 != "0"'"' ${TargetIDs}_output.csv >> ${TargetIDs}_output2.csv"

If TargetIDs might include whitespace or shell metacharacters, you'd also want to insert some more quotes into the generated text: 如果TargetIDs可能包含空格或shell元字符,则您还想在生成的文本中插入更多引号:

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

As per the discussion in the comments, there is a difference between: 根据评论中的讨论,两者之间存在差异:

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

and

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

in the case that field 12 is 00 . 在字段12为00的情况下。 However, neither are correct if you want to remove lines where the 12th field is "0" , which seems to be the point of your sed line in your second question. 但是,如果要删除第12个字段为"0" ,这都不对,这似乎是第二个问题中sed行的要点。 In order to catch that, you'd need: 为了赶上这一点,您需要:

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

Make sure that you are using the correct version. 确保您使用的是正确的版本。

Try this: 尝试这个:

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

This of course assumes that you are treating column 12 as a numeric value. 当然,这假定您将第12列视为数字值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM