Tree
Tree

Reputation: 10322

extracting-the-values-using-grep

From extracting the values using grep

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

I have tried with this : cat a.txt | grep RG1|grep '-'| cut -d':' -f3-| cut -d'-' -f2 |sed -e 's/ //'

This wont work because it will extract wrong path because some of + also having -

How to reslove this issue

Upvotes: 1

Views: 123

Answers (5)

Dennis Williamson
Dennis Williamson

Reputation: 359905

Try this variation on the answer I gave to your previous question:

grep -Po '(?<=RG1:- ).*(?=:\d*$)' a.txt 

Upvotes: 0

sid_com
sid_com

Reputation: 25117

If Perl is alowed
- or +

perl -nE 'say $1 if /\AReading:RG[1-9]:[+-]\s+(.*)\Z/' file

only -

perl -nE 'say $1 if /\AReading:RG[1-9]:-\s+(.*)\Z/' file

Upvotes: 0

sorpigal
sorpigal

Reputation: 26086

Try it with sed

sed -r -e '/:RG1:/s/.*:[+-] //;s/:[0-9]+$//' a.txt

Which will operate only on lines with :RG1: in them. You can generalize this for all lines:

sed -r -e 's/.*:[+-] //;s/:[0-9]+$//' a.txt

Or just lines with RG and a number

sed -r -e '/:RG[0-9]+:/s/.*:[+-] //;s/:[0-9]+$//' a.txt

If you want to keep the trailing :12 simply omit the final substitution, e.g.:

sed -r -e '/:RG[0-9]+:/s/.*:[+-] //' a.txt

Upvotes: 1

aioobe
aioobe

Reputation: 420951

Try this:

egrep "^[^:]*:RG1:-" a.txt | cut -d: -f3 | cut -b3-

Sample run:

$ cat a.txt 
Reading:RG1:+ /user/reading-2/Monday:12
Reading:RG1:- /user/**/Friday:12
Reading:RG1:- /user/**/*.txt:12
Reading:RG1:- /user/tet-23/**/*.txt:12
Reading:RG2:- /user/tet-23/**/*.txt:12
Reading:RG2:+ /user/reading-2/Monday:12
$ egrep "^[^:]*:RG1:-" a.txt | cut -d: -f3 | cut -b3-
/user/**/Friday
/user/**/*.txt
/user/tet-23/**/*.txt

"^[^:]*:RG1:-" means "start with anything but : zero or more times, then a :, then a RG1, followed by -.

Upvotes: 2

Diego Sevilla
Diego Sevilla

Reputation: 29011

You may try to get the complete "RG1:+" string, and then cut by space, for example:

grep "RG1:+" a.txt | cut -d" " -f2

Upvotes: 1

Related Questions