AWK参数打印不需要的换行符 [英] AWK argument prints unwanted newline

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

问题描述


免责声明:我使用了一个非常简单的例子,认为每个参数都有一些我不知道的隐藏编码。结果我的
格式是完全错误的。如@ miken32所说,我应该使用
逗号。我改变了我的格式,它的工作完美。宝贵的课程
学习。


我已经导出了一个 csv 使用Excel 2013(在Windows上)从 xlsl 中的文件。我向我发送了新的 csv 文件,并在Unix(MacOS Sierra)上运行这些测试。



考虑以下CSV文件:

  John 
Adam
Cameron
Jordan

我正在尝试格式化每行,像这样:

  {'operator':'EQ','property':'first_name','value' '},
{'operator':'EQ','property':'first_name','value':'Adam'},
{'operator':'EQ','property' 'first_name','value':'Cameron'},
{'operator':'EQ','property':'first_name','value':'Jordan'}

所以是每一行之间变化的唯一参数。



这是我写的 awk 文件:

 code> BEGIN {} 
{
print{'operator':'EQ','property':'first_name','value':'$ 0'}
}
END {}

但执行完毕后,我得到的输出:

  {'operator':'EQ','property':'first_name','value' $ b $} 
{'operator':'EQ','property':'first_name','value':'Adam
'},
注意在打开输入参数($ 0)后,打印换行符的方式是否正确?这是我的JSON格式混乱。我有一种感觉,这与excel导出有关(这是由另存为.csv )。



任何建议?

解决方案

在awk中, $ 0 表示整行,而 $ 1 $ 2 $ n class ='doc-link'href =https://stackoverflow.com/documentation/awk/3947/row-manipulation/28060/extract-specific-column-field-from-specific-line#t=201703241903301729017>



提供的示例不是CSV文件,因为没有任何值以逗号分隔。如果是,你可以这样做:

  awk -F,'{print{'''operator' '''''''''''''''''''''''''''''''''first_name' ':''''''$$'$' p>哪个有点疯狂的shell-friendly引用!



你应该知道,有一些工具,如 jq ,它们旨在创建和使用JSON数据。如果这不止一次性的任务,你可能会更好地看待这些。



使用意见建议编辑:

  awk -F,'{print{\047operator\047:\047EQ\047,\047property\047:\047first_name\ 047,\047value\047:\047$ 1\047},}'foo.txt 

(但是从您的原始问题看,您似乎使用单独的脚本文件,所以不必担心转义引号。)


Disclaimer: I used an extremely simple example thinking each argument had some hidden encoding I wasn't aware of. Turns out my formatting was entirely wrong. As @miken32 said, I should be using commas. I changed my format and it works perfectly. Valuable lesson learned.

I've exported a csvfile from an xlsl with Excel 2013 (on Windows). I emailed myself the new csv file and am running these tests on Unix (MacOS Sierra).

Consider the following CSV file:

John
Adam
Cameron
Jordan

I'm trying to format each line to look like this:

{'operator':'EQ', 'property':'first_name', 'value':'John'},
{'operator':'EQ', 'property':'first_name', 'value':'Adam'},
{'operator':'EQ', 'property':'first_name', 'value':'Cameron'},
{'operator':'EQ', 'property':'first_name', 'value':'Jordan'}

So value is the only argument changing between each line.

Here is the awk file I wrote:

BEGIN { }
{
    print "{'operator':'EQ', 'property':'first_name', 'value':'"$0"'},";
}
END { }

But after executing this is the output I get:

{'operator':'EQ', 'property':'first_name', 'value':'John
'},
{'operator':'EQ', 'property':'first_name', 'value':'Adam
'},

Notice how right after the argument ($0) is printed out, a newline is printed? This is messing with my JSON format. I have a feeling this has to do with the excel exporting (which was done by Save as .csv).

Any suggestions?

解决方案

In awk, $0 represents the entire line, whereas $1, $2, $n represent the delimited fields in the line.

The sample provided isn't a CSV file, since there aren't any values separated by commas. If it were, you could do this:

 awk -F, '{print "{'"'"'operator'"'"':'"'"'EQ'"'"', '"'"'property'"'"':'"'"'first_name'"'"', '"'"'value'"'"':'"'"'"$1"'"'"'},"}' foo.txt 

Which gets a bit crazy with the shell-friendly quoting!

You should be aware that there are tools such as jq, which are designed to create and work with JSON data. If this is more than a one-off task you might be better served looking at those.

Edit using a suggestion from comments:

 awk -F, '{print "{\047operator\047:\047EQ\047, \047property\047:\047first_name\047, \047value\047:\047"$1"\047},"}' foo.txt 

(But from your original question it looks like you're using a separate script file anyway, so won't have to worry about escaping quotes.)

这篇关于AWK参数打印不需要的换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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