awk或grep在迭代中查找2个单词并逐行输出。

huangapple go评论67阅读模式
英文:

awk or grep 2 words in iterations and output ine by line

问题

我尝试在多次迭代中搜索2个单词并逐行生成输出
我有一个类似这样的源文件:

    "Resource": [
        {
            "ID": "I8745",
            "UserName": "user1",
            "IpAddress": "10.10.10.2",
            "State": "AVAILABLE",
            },
        },
        {
            "ID": "I8746",
            "UserName": "user2",
            "IpAddress": "10.10.10.3",
            "State": "AVAILABLE",
        },
        {
            "ID": "I8747",
            "UserName": "user3",
            "IpAddress": "10.10.10.4",
            "State": "AVAILABLE",
        },
        {
            "ID": "I8748",
            "UserName": "user4",
            "IpAddress": "10.10.10.5",
            "State": "AVAILABLE",
        },
        ....

期望的结果应该是这样的:

"ID": "I8745", "UserName": "user1",
"ID": "I8746", "UserName": "user2",
"ID": "I8747", "UserName": "user3",
"ID": "I8748", "UserName": "user4",
....

如何使用awk或grep完成这个任务,如果可能的话?

非常感谢

英文:

I'm tying to search for 2 words in several iterations and make the output line by line
I have a source file like this :

    "Resource": [
        {
            "ID": "I8745",
            "UserName": "user1",
            "IpAddress": "10.10.10.2",
            "State": "AVAILABLE",
            },
        },
        {
            "ID": "I8746",
            "UserName": "user2",
            "IpAddress": "10.10.10.3",
            "State": "AVAILABLE",
        },
        {
            "ID": "I8747",
            "UserName": "user3",
            "IpAddress": "10.10.10.4",
            "State": "AVAILABLE",
        },
        {
	    "ID": "I8748",
            "UserName": "user4",
            "IpAddress": "10.10.10.5",
            "State": "AVAILABLE",
        },
		....

The expected result, should be like this:

"ID": "I8745", "UserName": "user1",
"ID": "I8746", "UserName": "user2",
"ID": "I8747", "UserName": "user3",
"ID": "I8748", "UserName": "user4",
....

How can do this with awk or grep if it's possible?

Thank you very much

答案1

得分: 1

是的。

"ID": "I8745", "UserName": "user1",
"ID": "I8746", "UserName": "user2",
"ID": "I8747", "UserName": "user3",
"ID": "I8748", "UserName": "user4",

Paste命令将两个文件并排组合在一起。在bash中,您可以使用<(command)作为一个"虚拟"文件。

英文:

Yes.

$ paste &lt;(grep ID /tmp/in) &lt;( grep user /tmp/in)  | sed &#39;s/\ \+/ /g&#39;
 &quot;ID&quot;: &quot;I8745&quot;,	 &quot;UserName&quot;: &quot;user1&quot;,
 &quot;ID&quot;: &quot;I8746&quot;,	 &quot;UserName&quot;: &quot;user2&quot;,
 &quot;ID&quot;: &quot;I8747&quot;,	 &quot;UserName&quot;: &quot;user3&quot;,
 &quot;ID&quot;: &quot;I8748&quot;,	 &quot;UserName&quot;: &quot;user4&quot;,

Paste combines 2 files side by side. In bash you can use &lt;(command) as a "fake" file.

答案2

得分: 1

如果您拥有合法的JSON,使用处理JSON的工具可能更可靠,但如果您严格限制在使用AWK,则可以按以下方式进行操作。假设file.txt的内容如下:

&quot;Resource&quot;: [
    {
        &quot;ID&quot;: &quot;I8745&quot;,
        &quot;UserName&quot;: &quot;user1&quot;,
        &quot;IpAddress&quot;: &quot;10.10.10.2&quot;,
        &quot;State&quot;: &quot;AVAILABLE&quot;,
    },
    {
        &quot;ID&quot;: &quot;I8746&quot;,
        &quot;UserName&quot;: &quot;user2&quot;,
        &quot;IpAddress&quot;: &quot;10.10.10.3&quot;,
        &quot;State&quot;: &quot;AVAILABLE&quot;,
    },
    {
        &quot;ID&quot;: &quot;I8747&quot;,
        &quot;UserName&quot;: &quot;user3&quot;,
        &quot;IpAddress&quot;: &quot;10.10.10.4&quot;,
        &quot;State&quot;: &quot;AVAILABLE&quot;,
    },
    {
    &quot;ID&quot;: &quot;I8748&quot;,
        &quot;UserName&quot;: &quot;user4&quot;,
        &quot;IpAddress&quot;: &quot;10.10.10.5&quot;,
        &quot;State&quot;: &quot;AVAILABLE&quot;,
    },
    ....

然后执行以下AWK命令:

awk &#39;{gsub(/^[[:space:]]*/,&quot;&quot;)}/ID/{i=$0}/UserName/{print i,$0}&#39; file.txt

将得到以下输出:

&quot;ID&quot;: &quot;I8745&quot;, &quot;UserName&quot;: &quot;user1&quot;,
&quot;ID&quot;: &quot;I8746&quot;, &quot;UserName&quot;: &quot;user2&quot;,
&quot;ID&quot;: &quot;I8747&quot;, &quot;UserName&quot;: &quot;user3&quot;,
&quot;ID&quot;: &quot;I8748&quot;, &quot;UserName&quot;: &quot;user4&quot;,

解释:我使用sub函数删除前导空格,然后如果行包含ID,我将整行存储在变量i中,如果行包含UserName,我会打印存储在变量中的内容,然后跟随当前行。免责声明:我仅在您的示例输入中进行了测试,请使用所有可能的输入测试该代码。

(在GNU Awk 5.0.1中测试通过)

英文:

If you have legal JSON it might be more reliable to use tool for working with JSON, but if you are strictly limited to AWK then you could harness it following way. Let file.txt content be

&quot;Resource&quot;: [
    {
        &quot;ID&quot;: &quot;I8745&quot;,
        &quot;UserName&quot;: &quot;user1&quot;,
        &quot;IpAddress&quot;: &quot;10.10.10.2&quot;,
        &quot;State&quot;: &quot;AVAILABLE&quot;,
        },
    },
    {
        &quot;ID&quot;: &quot;I8746&quot;,
        &quot;UserName&quot;: &quot;user2&quot;,
        &quot;IpAddress&quot;: &quot;10.10.10.3&quot;,
        &quot;State&quot;: &quot;AVAILABLE&quot;,
    },
    {
        &quot;ID&quot;: &quot;I8747&quot;,
        &quot;UserName&quot;: &quot;user3&quot;,
        &quot;IpAddress&quot;: &quot;10.10.10.4&quot;,
        &quot;State&quot;: &quot;AVAILABLE&quot;,
    },
    {
    &quot;ID&quot;: &quot;I8748&quot;,
        &quot;UserName&quot;: &quot;user4&quot;,
        &quot;IpAddress&quot;: &quot;10.10.10.5&quot;,
        &quot;State&quot;: &quot;AVAILABLE&quot;,
    },
    ....

then

awk &#39;{gsub(/^[[:space:]]*/,&quot;&quot;)}/ID/{i=$0}/UserName/{print i,$0}&#39; file.txt

gives output

&quot;ID&quot;: &quot;I8745&quot;, &quot;UserName&quot;: &quot;user1&quot;,
&quot;ID&quot;: &quot;I8746&quot;, &quot;UserName&quot;: &quot;user2&quot;,
&quot;ID&quot;: &quot;I8747&quot;, &quot;UserName&quot;: &quot;user3&quot;,
&quot;ID&quot;: &quot;I8748&quot;, &quot;UserName&quot;: &quot;user4&quot;,

Explanation: I use sub function to remove leading whitespaces, then if line contains ID I store whole line in variable i, if line contains UserName I print what is stored inside variable followed by current line. Disclaimer: I tested it only with your sample input, please test that code with all possible inputs.

(tested in GNU Awk 5.0.1)

答案3

得分: 0

Sed 变体:

$ sed -n 's/[[:space:]]*//;/ID/N;s/\n[[:space:]]*/ /gp' 文件
"ID": "I8745", "UserName": "user1",
"ID": "I8746", "UserName": "user2",
"ID": "I8747", "UserName": "user3",
"ID": "I8748", "UserName": "user4",
英文:

Sed variant:

$ sed -n &#39;s/[[:space:]]*//;/ID/N;s/\n[[:space:]]*/ /gp&#39; file
&quot;ID&quot;: &quot;I8745&quot;, &quot;UserName&quot;: &quot;user1&quot;,
&quot;ID&quot;: &quot;I8746&quot;, &quot;UserName&quot;: &quot;user2&quot;,
&quot;ID&quot;: &quot;I8747&quot;, &quot;UserName&quot;: &quot;user3&quot;,
&quot;ID&quot;: &quot;I8748&quot;, &quot;UserName&quot;: &quot;user4&quot;,

huangapple
  • 本文由 发表于 2023年1月9日 19:30:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75056651.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定