我正在尝试解析从curl请求返回的JSON,如下所示:
curl 'http://twitter.com/users/username.json' |
sed -e 's/[{}]/''/g' |
awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}'
Run Code Online (Sandbox Code Playgroud)
以上将JSON拆分为字段,例如:
% ...
"geo_enabled":false
"friends_count":245
"profile_text_color":"000000"
"status":"in_reply_to_screen_name":null
"source":"web"
"truncated":false
"text":"My status"
"favorited":false
% ...
Run Code Online (Sandbox Code Playgroud)
如何打印特定字段(用-v k=text?表示)?
我需要在目录中搜索模式,并将包含它的文件的名称保存在数组中.
搜索模式:
grep -HR "pattern" . | cut -d: -f1
Run Code Online (Sandbox Code Playgroud)
这将打印出包含"pattern"的所有文件名.
如果我尝试:
targets=$(grep -HR "pattern" . | cut -d: -f1)
length=${#targets[@]}
for ((i = 0; i != length; i++)); do
echo "target $i: '${targets[i]}'"
done
Run Code Online (Sandbox Code Playgroud)
这只打印一个包含所有filnames字符串的元素.
output: target 0: 'file0 file1 .. fileN'
Run Code Online (Sandbox Code Playgroud)
但是我需要:
output: target 0: 'file0'
output: target 1: 'file1'
.....
output: target N: 'fileN'
Run Code Online (Sandbox Code Playgroud)
如果不对目标执行无聊的拆分操作,我怎样才能实现结果?
我无法在google上找到这个问题的简单答案,也不能在stackoverflow上找到答案.
基本上我有两个数组,我需要并排打印到终端,因为一个数组是一个术语列表,另一个数组是术语的定义.有谁知道这样做的好方法?
提前致谢.