简体   繁体   English

在Linux上使用AWK写入文件

[英]Write a file using AWK on linux

I have a file that has several lines of which one line is 我有一个包含多行的文件,其中一行是

-xxxxxxxx()xxxxxxxx

I want to add the contents of this line to a new file 我想将此行的内容添加到新文件中

I did this : 我是这样做的:

awk ' /^-/ {system("echo" $0 ">" "newline.txt")} '

but this does not work , it returns an error that says : 但这不起作用,它返回一条错误消息:

Unnexpected token '(' 

I believe this is due to the () present in the line. 我相信这是由于该行中存在()。 How to overcome this issue? 如何克服这个问题?

You don't need the system , echo commands, simply: 您不需要systemecho命令,只需:

awk '/^-/ {print $1}' file > newfile

This will capture lines starting with - and truncate the rest if there's a space. 这将捕获以-开头的行,并在有空格的情况下截断其余行。

awk '/^-/ {print $0}' file > newfile

Would capture the entire line including spaces. 将捕获包括空格在内的整行。

You could use grep also: 您也可以使用grep

grep -o '^-.*' file > newfile

Captures any lines starting with - 捕获以-开头的任何行

grep -o '^-.*().*' file > newfile

Would be more specific and capture lines starting with - also containing () 会更具体,并且捕获行以-开头,还包含()

First of all for simple extraction of patterns from file, you do not need to use awk it is an overkill, grep would be more than enough for the task: 首先,为了从文件中简单提取模式,您不需要使用awk这是一个过大的选择, grep足以完成任务:

INPUT: INPUT:

$ more file
123
-xxxxxxxx()xxxxxxxx
abc
-xyxyxxux()xxuxxuxx
123
abc
123

command: 命令:

$ grep -oE '^-[^(]+\(\).*' file                                                                                                  
-xxxxxxxx()xxxxxxxx
-xyxyxxux()xxuxxuxx

explanations: 解释:

Option: -oE to define the output as the pattern and not the whole line (can be removed) Regex: ^-[^(]+\\(\\).* will select lines that starts with - and contains () 选项: -oE将输出定义为模式,而不是整行(可以删除)正则表达式: ^-[^(]+\\(\\).*将选择以-开头并包含()

You can redirect your output to a new_file by adding > new_file at the end of your command. 您可以通过在命令末尾添加> new_file将输出重定向到new_file

You need to add proper spaces! 您需要添加适当的空间!

With your erronous awk ' /^-/ {system("echo" $0 ">" "newline.txt")} ' , the shell command is essentially echo-xxxxxxxx()xxxxxxxx>newline.txt , which surely doesn't work. 使用错误的awk ' /^-/ {system("echo" $0 ">" "newline.txt")} ' ,shell命令本质上是echo-xxxxxxxx()xxxxxxxx>newline.txt ,这肯定不起作用。 You need to construct a proper shell command inside the awk string, and obey awk s string concatenation rules, ie your intended script should look like this (which is still broken, because $0 is not properly quoted in the resulting shell command): 您需要在awk字符串中构造一个正确的shell命令,并遵守awk的字符串连接规则,即您想要的脚本应如下所示(由于在生成的shell命令中未正确引用$0 ,因此该脚本仍然无效):

awk '/^-/ { system("echo " $0 " > newline.txt") }'

However, if you really just need to echo $0 into a file, you can simply do: 但是,如果您真的只需要在文件中echo $0 ,则可以执行以下操作:

awk '/^-/ { print $0 > "newline.txt" }'

Or even more simply 或更简单

awk '/^-/' > newline.txt

Which essentially applies the default operation to all records matching /^-/ , whereby the default operation is to print , which is short for neatly printing the current record, ie this script simply filters out the desired records. 它实质上将默认操作应用于所有匹配/^-/记录,其中默认操作print ,这是整齐地打印当前记录的缩写,即此脚本仅过滤出所需的记录。 The > newline.txt redirection outside awk simply puts it into a file. awk之外的> newline.txt重定向只是将其放入文件中。

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

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