Bash(文本提取)
我在一个文件中有一行很长的信息,我想把它提取出来,这样最终的结果就像这个output.txt?请帮忙 我的输入看起来像:Bash(文本提取),bash,shell,awk,sed,grep,Bash,Shell,Awk,Sed,Grep,我在一个文件中有一行很长的信息,我想把它提取出来,这样最终的结果就像这个output.txt?请帮忙 我的输入看起来像: [{"city":"london","first name":"peter","last name":"hansen","age":"40"}, {"city":"new york","first name":"celine","last name":"parker","age":"36"] Output.txt peter (40) celine (36) 如果是正确的
[{"city":"london","first name":"peter","last name":"hansen","age":"40"},
{"city":"new york","first name":"celine","last name":"parker","age":"36"]
Output.txt
peter (40)
celine (36)
如果是正确的json,另一种选择是使用jq:
假设输入字符串存储在文件input.txt中,即 你可以通过
ruby -e "puts $(<input.txt).map {|x| %(#{x[:'first name']} (#{x[:age]}))}"
这使用了一个事实,即输入恰好是一个语法有效的Ruby表达式,表示一个由两个哈希组成的数组。在右方括号之前不应该有一个右大括号吗?
echo '[{"city":"london","first name":"peter","last name":"hansen","age":"40"},{"city":"new york","first name":"celine","last name":"parker","age":"36"}]' >input.txt
ruby -e "puts $(<input.txt).map {|x| %(#{x[:'first name']} (#{x[:age]}))}"