删除awk中的换行符 [英] Remove the newline character in awk

查看:4785
本文介绍了删除awk中的换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除一个日期函数换行字符,并将它包含空格。我节省了使用这个变量:

I am trying to remove the new line character for a date function and have it include spaces. I am saving the variables using this:

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

我可以看到,这是正确的格式,我需要做的回声$ CURRENT_DATE。
然而,当我需要使用这个变量它不采取行动,我想它的方式。

I can see that this is the right format I need by doing a echo $current_date. However, when I need to use this variable it does not act the way I would like it.

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

我需要的日期停留在文本的当前行,没有换行继续,除非指定。

I need the date to stay in the current line of text and continue with no newline unless specified.

在此先感谢。

推荐答案

,而不是试图将变量插入到你正在做的命令字符串,你可以通过它来awk的是这样的:

Rather than attempting to insert the variable into the command string as you are doing, you can pass it to awk like this:

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

然后,您可以使用变量日期作为脚本中一个awk变量:

You can then use the variable date as an awk variable within the script:

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

顺便说一句,它看起来像你原来的打印语句被打破了,因为有双引号,从它的结束丢失。

As an aside, it looks like your original print statement was broken, as there were double quotes missing from the end of it.

这篇关于删除awk中的换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆