我有一个包含许多行的文件
代码:
randomstring | randomstring
我希望删除所有内容,直到“|”。
有关如何使用sed / awk执行此操作的任何想法?
TIA!
答案 0 :(得分:3)
试试这个
sed 's/^[^|]*.//'
基本上从行的开头,替换从开头到“|”的所有内容空白
答案 1 :(得分:2)
确保该行的开头和结尾:
sed -e 's/^.*\(|.*\)$/\1/'
答案 2 :(得分:1)
在awk中,您可以将字段分隔符设置为几乎任何内容。
awk 'BEGIN{ FS="|" }{print FS, $2}' yourfilename
答案 3 :(得分:1)
试试这个
awk '{print $3}' file
答案 4 :(得分:1)
对于行中的多个|
:
astr | bstr | cstr | dstr
贪婪的比赛
sed 's/.*|//' < file # will result: ` dstr`
sed 's/.*|/|/' < file # will result: `| dstr`
非贪婪的比赛
sed 's/^[^|]*|//' < file # will result: ` bstr | cstr | dstr`
sed 's/^[^|]*|/|/' < file # will result: `| bstr | cstr | dstr`
更短 - 使用cut
命令
cut -d'|' -f-1 < file # will result: `astr `
cut -d'|' -f-2 < file # will result: `astr | bstr `
cut -d'|' -f2- < file # will result: ` bstr | cstr | dstr`
cut -d'|' -f3- < file # will result: ` cstr | dstr`
cut -d'|' -f2 < file # will result: ` bstr `
cut -d'|' -f2-3 < file # will result: ` bstr | cstr`
cut -d'|' -f2,4 < file # will result: ` bstr | dstr`