Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
awk-won';不打印新行字符_Awk_Newline - Fatal编程技术网

awk-won';不打印新行字符

awk-won';不打印新行字符,awk,newline,Awk,Newline,我正在使用下面的代码来更改现有的awk脚本,这样我就可以用一个简单的命令添加越来越多的案例 echo `awk '{if(/#append1/){print "pref'"$1"'=0\n" $0 "\n"} else{print $0 "\n"}}' tf.a 请注意,第一次打印是“pref''$1''=0\n”,因此它在其环境中引用变量$1,而不是在awk本身中 命令/tfb.a“c”应将代码更改为: BEGIN{ #append1 } ... 致: 然而,它在一条线上给了我一切 有人知

我正在使用下面的代码来更改现有的awk脚本,这样我就可以用一个简单的命令添加越来越多的案例

echo `awk '{if(/#append1/){print "pref'"$1"'=0\n" $0 "\n"} else{print $0 "\n"}}' tf.a
请注意,第一次打印是
“pref''$1''=0\n”
,因此它在其环境中引用变量
$1
,而不是在
awk
本身中

命令
/tfb.a“c”
应将代码更改为:

BEGIN{
#append1
}
...
致:

然而,它在一条线上给了我一切


有人知道这是为什么吗?

像这样做。使用-v将变量从shell正确地传递给awk

#!/bin/bash
toinsert="$1"
awk -v toinsert=$toinsert '
/#append1/{
    $0="pref"toinsert"=0\n"$0
}
{print}
' file > temp
mv temp file
输出

$ cat file
BEGIN{
#append1
}

$ ./shell.sh c
BEGIN{
prefc=0
#append1
}

如果从等式中直接取下
awk
,您可以看到发生了什么:

# Use a small test file instead of an awk script
$ cat xxx
hello
there
$ echo `cat xxx`
hello there
$ echo "`cat xxx`"
hello
there
$ echo "$(cat xxx)"
hello
there
$
backtick操作符过早地将输出扩展为shell“words”。您可以在shell(yikes)中使用
$IFS
变量,也可以使用双引号


如果您正在运行一个现代的
sh
(例如
ksh
bash
,而不是“经典的”Bourne
sh
),您可能还需要使用
$()
语法(更容易找到匹配的开始/结束定界符)。

如果
awk
被包装在
echo
中是否重要?请丢失echo并重试。说真的,我不知道回声是干什么的。查看我的输出。否则,请显示代码的更多详细信息,并清楚地描述所需内容。@Mechko:回声是导致输出打印在一行上的原因。按照ghostdog74的建议删除它,或者像这样做:
echo“$(awk stuff)”
-用双引号将其括起来将保留换行符。啊,好的。我确实需要回音,因为还有很多其他东西需要打印出来。顺便说一句,我现在有了一个功能齐全的标记解析器,如果有人需要,可以让用户定义自己的标记宏。我明天要做一个安装脚本。
# Use a small test file instead of an awk script
$ cat xxx
hello
there
$ echo `cat xxx`
hello there
$ echo "`cat xxx`"
hello
there
$ echo "$(cat xxx)"
hello
there
$