Alex
Alex

Reputation: 21

Replace string of line

i have a file with lots of lines like that

Code:

randomstring | randomstring

and i want to remove everything until the "|".

Any ideas how to do this with sed/awk?

TIA!

Upvotes: 2

Views: 172

Answers (5)

clt60
clt60

Reputation: 63892

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`

Upvotes: 1

Sai
Sai

Reputation: 3947

Try this

sed 's/^[^|]*.//' 

Basically from the beginning of the line, substitute everything from the beginning till "|" with blank

Upvotes: 3

ghostdog74
ghostdog74

Reputation: 342283

try this

awk '{print $3}' file

Upvotes: 1

In awk, you can set the field separator to just about anything.

awk 'BEGIN{ FS="|" }{print FS, $2}' yourfilename

Upvotes: 1

Adam Dymitruk
Adam Dymitruk

Reputation: 129526

Make sure to baruch the beginning and end of the line:

sed -e  's/^.*\(|.*\)$/\1/'

Upvotes: 2

Related Questions