【发布时间】:2017-12-02 03:57:49
【问题描述】:
我正在尝试重新编号 myfile1.txt 中的 line_id 字段,其中的每一行都有多个分隔符。最终目标是从这些数据中获取一个 Python 字典列表。所以每一行都会变成一个字典,所以分隔符“:”和“,”对我来说真的很重要。
这是来自 myfile.txt 的 sn-p:
"line_id":57,"name":"Test File","seq_number":26,"user":"user1","text_entry":"Entered the room"
"line_id":58,"name":"Test File","seq_number":26,"user":"user1","text_entry":"Left the room"
"line_id":59,"name":"Test File","seq_number":26,"user":"user1","text_entry":"Quit the group"
"line_id":60,"name":"Test File","seq_number":1,"user":"user2","text_entry":"Late to the party"
"line_id":61,"name":"Test File","seq_number":1,"user":"user2","text_entry":"Not responding"
以下 awk 语句运行良好,尽管我丢失了所有分隔符。它们被替换为空格。
awk -F [:,] '$2=$2-56' myfile1.json >> myfile2.txt
结果是:
"line_id" 1 "name" "Test File" "seq_number":26 "user" "user1" "text_entry" "Entered the room"
"line_id" 2 "name" "Test File" "seq_number":26 "user" "user1" "text_entry" "Left the room"
"line_id" 3 "name" "Test File" "seq_number":26 "user" "user1" "text_entry" "Quit the group"
"line_id" 4 "name" "Test File" "seq_number":1 "user" "user2" "text_entry" "Late to the party"
"line_id" 5 "name" "Test File" "seq_number":1 "user" "user2" "text_entry" "Not responding"
现在我留下了在适当的地方取回 : 和 , 的问题。我探索了 sed 但没有找到一种简单的方法来对第二个字段进行减法。
我已经通过this link 这对我的要求没有太大帮助。 请指教。
【问题讨论】: