andor kesselman
andor kesselman

Reputation: 1169

Grep entire line after word

What would be the grep command to get an everything in the line after a match? For example on a file path:

/home/usr/we/This/is/the/file/path

and I want the output to be

/we/This/is/the/File/Path

Matching the /we as the regex.

Upvotes: 0

Views: 4258

Answers (3)

NeronLeVelu
NeronLeVelu

Reputation: 10039

YourInput | sed 's|/home/usr\(/we.*\)|\1|'

assuming it's always (and only) starting with /home/usr else

YourInput | sed -n 's|^.*\(/we.*\)||p'

return only line(s) having /we and remove text before /we

Upvotes: 1

Jotne
Jotne

Reputation: 41446

OP like to use we as a trigger. Using awk

awk -F/ '{for (i=1;i<=NF;i++) {if ($i~/we/) f=1;if (f) printf "/%s",$i}print ""}' file
/we/This/is/the/file/path

Using gnu awk

awk '{print gensub(/.*(\/we)/,"\\1","g")}' file
/we/This/is/the/file/path

Upvotes: 1

user2719058
user2719058

Reputation: 2233

grep -o does what you want.

grep -o '/we.*'

Upvotes: 2

Related Questions