Replace string of line
i have a file with lots of lines like that
Code:
randomstrin开发者_如何学编程g | randomstring
and i want to remove everything until the "|".
Any ideas how to do this with sed/awk?
TIA!
Try this
sed 's/^[^|]*.//'
Basically from the beginning of the line, substitute everything from the beginning till "|" with blank
Make sure to baruch the beginning and end of the line:
sed -e 's/^.*\(|.*\)$/\1/'
In awk, you can set the field separator to just about anything.
awk 'BEGIN{ FS="|" }{print FS, $2}' yourfilename
try this
awk '{print $3}' file
For the multiple |
in the lines:
astr | bstr | cstr | dstr
Greedy match
sed 's/.*|//' < file # will result: ` dstr`
sed 's/.*|/|/' < file # will result: `| dstr`
Non-greedy match
sed 's/^[^|]*|//' < file # will result: ` bstr | cstr | dstr`
sed 's/^[^|]*|/|/' < file # will result: `| bstr | cstr | dstr`
shorter - with the cut
command
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`
精彩评论