Tree
Tree

Reputation: 10322

extracting the values using grep

Sample String : a.txt

Reading:RG1:+ /user/reading/Monday:12
Reading:RG1:- /user/**/Friday:12
Reading:RG1:- /user/**/*.txt:12
Reading:RG1:- /user/tet/**/*.txt:12

I am looking to extract the string

after + or - what ever the string i want it 

using :

cat a.txt | grep RG1|grep '+'| cut -d':' -f3| cut -d'+' -f2 |sed -e 's/ //

I am getting /user/reading/Monday

But i amlooking

/user/reading/Monday:12

Upvotes: 1

Views: 429

Answers (3)

Dennis Williamson
Dennis Williamson

Reputation: 359905

$ grep -Po '(?<=[-+] ).*' a.txt
/user/reading/Monday:12
/user/**/Friday:12
/user/**/*.txt:12
/user/tet/**/*.txt:12

Change the characters with the square brackets to change which lines you select.

Upvotes: 1

Fred Foo
Fred Foo

Reputation: 363507

Use egrep -o:

$ egrep -o '/user/reading/[A-Z][a-z]+day:[0-9]+' a.txt
/user/reading/Monday:12
/user/reading/Friday:12

Edit: for your new example, use something like

$ egrep -o '/user/[^ ]*:[0-9]+' a.txt
/user/reading/Monday:12
/user/**/Friday:12
/user/**/*.txt:12
/user/tet/**/*.txt:12

Assuming no spaces in your paths.

Upvotes: 3

dogbane
dogbane

Reputation: 274532

To fix your command, use -f3- because you want everything from the 3rd field to the end of the line.

cat a.txt | grep RG1|grep '+'| cut -d':' -f3-| cut -d'+' -f2 |sed -e 's/ //'

Upvotes: 1

Related Questions