【问题标题】:Remove the newline character in awk删除 awk 中的换行符
【发布时间】:2015-03-23 16:40:10
【问题描述】:

我正在尝试删除日期函数的换行符并让它包含空格。我正在使用以下方法保存变量:

current_date=$(date "+%m/%d/ AT %y%H:%M:%S" )

我可以通过 echo $current_date 看到这是我需要的正确格式。 但是,当我需要使用这个变量时,它不会按照我想要的方式运行。

awk '(++n==47) {print "1\nstring \nblah '$current_date' blah 2;     n=0} (/blah/) {n=0} {print}' input file > output file 

除非指定,否则我需要将日期保留在当前文本行中并且不使用换行符继续。

提前致谢。

【问题讨论】:

  • 使用echo "$current_date",而不是echo $current_date来查看$current_date的实际值。您尝试在 awk 脚本中使用 shell 变量的方式也是错误的。发布一些示例输入和预期输出,以便我们为您提供帮助。

标签: bash awk formatting


【解决方案1】:

与其尝试将变量插入到命令字符串中,不如将其传递给 awk,如下所示:

awk -v date="$(date "+%m/%d/ AT %y%H:%M:%S")" '# your awk one-liner here' input_file

然后您可以在脚本中使用变量date 作为 awk 变量:

print "1\nstring \nblah " date " blah 2";

顺便说一句,您原来的 print 语句似乎被破坏了,因为它的末尾缺少双引号。

【讨论】:

    猜你喜欢
    • 2019-03-29
    • 2019-12-01
    • 2019-08-31
    • 2012-07-25
    • 1970-01-01
    • 1970-01-01
    • 2019-01-17
    • 2012-05-08
    • 1970-01-01
    相关资源
    最近更新 更多