如何使用 Sed/gedit 删除文件中“:”后的所有内容并将输出写入另一个文件?
how to use Sed/gedit to remove everything after a " : " in a file and write output to another file?
我有一个文本文件:
"postalAddress2": "Postal Address Line 2",
"postalCity": "Postal City",
"postalPostCode": "Postal Code",
"postalState": "State"
我需要删除第二列,即“:”之后的所有内容
并删除所有换行符
预期输出:
"postalAddress2","postalCity","postalPostCode","postalState"
可以在 gedit 中使用 sed 或 regex 来完成吗?
我试过这个正则表达式,但它不起作用:
.*\:(.*) \:.*
能否请您尝试关注一次。
awk 'BEGIN{FS=":";OFS=","} {val=(val?val OFS:"")} END{print val}' Input_file
或者从@Sundeep 的评论中汲取灵感尝试:
awk 'BEGIN{FS=":"} {print }' Input_file | paste -sd,
使用 sed 和粘贴:
sed 's/:.*//' file | paste -d ',' -s > other_file
输出到other_file:
"postalAddress2","postalCity","postalPostCode","postalState"
$ cat tmp.txt
"postalAddress2": "Postal Address Line 2",
"postalCity": "Postal City",
"postalPostCode": "Postal Code",
"postalState": "State"
$ sed 's/\:.*$//' tmp.txt | tr '\n' ','
"postalAddress2","postalCity","postalPostCode","postalState",
这可能适合您 (GNU sed):
sed -Ez 's/:[^,]*//mg;s/,\n/,/g' file
将文件拖入内存并使用多行模式,删除所有行中从第一个 :
到下一个 ,
的所有内容。
然后将 ,\n
替换为 ,
。
我有一个文本文件:
"postalAddress2": "Postal Address Line 2",
"postalCity": "Postal City",
"postalPostCode": "Postal Code",
"postalState": "State"
我需要删除第二列,即“:”之后的所有内容 并删除所有换行符
预期输出:
"postalAddress2","postalCity","postalPostCode","postalState"
可以在 gedit 中使用 sed 或 regex 来完成吗?
我试过这个正则表达式,但它不起作用:
.*\:(.*) \:.*
能否请您尝试关注一次。
awk 'BEGIN{FS=":";OFS=","} {val=(val?val OFS:"")} END{print val}' Input_file
或者从@Sundeep 的评论中汲取灵感尝试:
awk 'BEGIN{FS=":"} {print }' Input_file | paste -sd,
使用 sed 和粘贴:
sed 's/:.*//' file | paste -d ',' -s > other_file
输出到other_file:
"postalAddress2","postalCity","postalPostCode","postalState"
$ cat tmp.txt
"postalAddress2": "Postal Address Line 2",
"postalCity": "Postal City",
"postalPostCode": "Postal Code",
"postalState": "State"
$ sed 's/\:.*$//' tmp.txt | tr '\n' ','
"postalAddress2","postalCity","postalPostCode","postalState",
这可能适合您 (GNU sed):
sed -Ez 's/:[^,]*//mg;s/,\n/,/g' file
将文件拖入内存并使用多行模式,删除所有行中从第一个 :
到下一个 ,
的所有内容。
然后将 ,\n
替换为 ,
。