英文:
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 <(grep ID /tmp/in) <( grep user /tmp/in) | sed 's/\ \+/ /g'
"ID": "I8745", "UserName": "user1",
"ID": "I8746", "UserName": "user2",
"ID": "I8747", "UserName": "user3",
"ID": "I8748", "UserName": "user4",
Paste combines 2 files side by side. In bash you can use <(command)
as a "fake" file.
答案2
得分: 1
如果您拥有合法的JSON,使用处理JSON的工具可能更可靠,但如果您严格限制在使用AWK,则可以按以下方式进行操作。假设file.txt
的内容如下:
"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",
},
....
然后执行以下AWK命令:
awk '{gsub(/^[[:space:]]*/,"")}/ID/{i=$0}/UserName/{print i,$0}' file.txt
将得到以下输出:
"ID": "I8745", "UserName": "user1",
"ID": "I8746", "UserName": "user2",
"ID": "I8747", "UserName": "user3",
"ID": "I8748", "UserName": "user4",
解释:我使用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
"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",
},
....
then
awk '{gsub(/^[[:space:]]*/,"")}/ID/{i=$0}/UserName/{print i,$0}' file.txt
gives output
"ID": "I8745", "UserName": "user1",
"ID": "I8746", "UserName": "user2",
"ID": "I8747", "UserName": "user3",
"ID": "I8748", "UserName": "user4",
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 's/[[:space:]]*//;/ID/N;s/\n[[:space:]]*/ /gp' file
"ID": "I8745", "UserName": "user1",
"ID": "I8746", "UserName": "user2",
"ID": "I8747", "UserName": "user3",
"ID": "I8748", "UserName": "user4",
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论