bash:从文件中提取特定字符串并删除字符

时间:2018-12-13 09:44:01

标签: json bash parsing

我正在尝试从文本文件中提取特定的字符串值,然后从他中删除反斜杠。值的名称是“ display_url”

我的脚本:

url=$(cat /var/scripts/string.txt | grep -oP '(?<=display_url":")[^"]+')

for link in $url; do
     echo 'https://'$link
done

输出:

https://pastebin.com\/WRv5ir4Y
https://reddit.com\/r\/IBO\/comments\u2026

所需的输出:

https://pastebin.com/WRv5ir4Y
https://reddit.com/r/IBO/comments/u2026

文本文件:

{"created_at":"Thu Dec 13 08:43:38 +0000 2018","id":1073136349845303297,"id_str":"1073136349845303297","text":"https:\/\/t.co\/aPu5ln7yjO\nhttps:\/\/t.co\/pBvevjSCc9\n\n#osectraining","source":"\u003ca href=\"http:\/\/twitter.com\" rel=\"nofollow\"\u003eTwitter Web Client\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":961508561217052675,"id_str":"961508561217052675","name":"Online Security","screen_name":"osectraining","location":"Israel","url":"https:\/\/www.onlinesecurity.co.il","description":"OnlineSecurity provides online cyber-security training courses and certification, from beginner to advanced with the most advanced virtual labs in the field.","translator_type":"none","protected":false,"verified":false,"followers_count":2,"friends_count":51,"listed_count":0,"favourites_count":0,"statuses_count":1,"created_at":"Thu Feb 08 07:54:39 +0000 2018","utc_offset":null,"time_zone":null,"geo_enabled":false,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"000000","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_link_color":"1B95E0","profile_sidebar_border_color":"000000","profile_sidebar_fill_color":"000000","profile_text_color":"000000","profile_use_background_image":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/961510231346958336\/d_KhBeTD_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/961510231346958336\/d_KhBeTD_normal.jpg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/961508561217052675\/1518076913","default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"quote_count":0,"reply_count":0,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[{"text":"osectraining","indices":[49,62]}],"urls":[{"url":"https:\/\/t.co\/aPu5ln7yjO","expanded_url":"https:\/\/pastebin.com\/WRv5ir4Y","display_url":"pastebin.com\/WRv5ir4Y","indices":[0,23]},{"url":"https:\/\/t.co\/pBvevjSCc9","expanded_url":"https:\/\/www.reddit.com\/r\/IBO\/comments\/9ragj7\/ioc_in_10_hours\/","display_url":"reddit.com\/r\/IBO\/comments\u2026","indices":[24,47]}],"user_mentions":[],"symbols":[]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"filter_level":"low","lang":"und","timestamp_ms":"1544690618369"}

有什么想法吗?

3 个答案:

答案 0 :(得分:0)

将脚本更改为:

url=$(grep -oP '(?<=display_url":")[^"]+' /var/scripts/string.txt )
sed 's/\\//g;s@^@https://@' <<< "$url"

应该有帮助。

  • 删除了没用的猫
  • 使用sed进行替换

答案 1 :(得分:0)

我将使用JSON命令行解析器 jq

jq -r '"https://" + .entities.urls[].display_url' /var/scripts/string.txt
  • -r代表“返回原始输入”(不带引号的字符串)
  • "https://" + concat ...
  • .entities.urls[] ...对于数组.entities.urls的每个项目...
  • .display_url ... display_url成员的值”

结果:

https://pastebin.com/WRv5ir4Y
https://reddit.com/r/IBO/comments

答案 2 :(得分:0)

同时需要function applyMixins(derivedCtor: any, baseCtors: any[]) { baseCtors.forEach(baseCtor => { Object.getOwnPropertyNames(baseCtor.prototype).forEach(name => { derivedCtor.prototype[name] = baseCtor.prototype[name] }); }); } 和单独的grep进行解析的原因是,sed能够使用Perl正则表达式(power ...)< / em>,但grep不能。您可能有Perl可用-如果可以,请使用它。

sed

几乎总是有perl -pe ' s/\\//g; s{.*?display_url":"}{https://}; s{",".*display_url":"}{\nhttps://} while /display_url/; s/",".*/\n/; ' /var/scripts/string.txt https://pastebin.com/WRv5ir4Y https://reddit.com/r/IBO/commentsu2026

awk

但是,如果您不能使用其中任何一个,那么一个awk '{ gsub("\\\\","",$0); split($0, chnk, "display_url.:."); for (x=2; x<=length(chnk); x++) { gsub("\".*","", chnk[x]); printf "https://%s\n", chnk[x]; } }' /var/scripts/string.txt https://pastebin.com/WRv5ir4Y https://reddit.com/r/IBO/commentsu2026 可以在循环中剥离反斜杠和一些基本的shell字符串处理,因为这很有趣。 :D

sed

最棘手的部分是解决shell解析中的双引号,但是我敢肯定有人可以提出更好的方法。