PROL\tests_li004_developers:VAS:3543346:q34243,d3hs35,34bdf3,24sfgg,a3s234
Im trying to grep all text after VAS:3543346, so im trying to just have this
q34243,d3hs35,34bdf3,24sfgg,a3s234
Im confused on how I would do this
PROL\tests_li004_developers:VAS:3543346:q34243,d3hs35,34bdf3,24sfgg,a3s234
Im trying to grep all text after VAS:3543346, so im trying to just have this
q34243,d3hs35,34bdf3,24sfgg,a3s234
Im confused on how I would do this
If you have that in a variable:
var='PROL\tests_li004_developers:VAS:3543346:q34243,d3hs35,34bdf3,24sfgg,a3s234'
echo "${var#*VAS:3543346:}"
q34243,d3hs35,34bdf3,24sfgg,a3s234
grep
can't do that; try
awk -F: '/VAS:3543346/ {print $4}' file
q34243,d3hs35,34bdf3,24sfgg,a3s234
It always helps us provide better answers if you tell us what OS and what shell you're using.
I don't think it can be done with grep
if there are an unknown number of colons in the text you want to print (but that isn't the case in the given sample). It can't be done with grep
using only standard options, and as far as I know it can't be done with a single grep
...
But if you have a grep
(such as the BSD version) with a -o
option that prints only the matched text, the two grep
pipeline:
grep 'VAS:3543346' file | grep -o '[^:]*$'
does what you want with you sample input. And to specifically check field 2 for VAS
and field 3 for 3543346
, you could tighten up the 1st grep to:
grep '^[^:]*:VAS:3543346:' file | grep -o '[^:]*$'
But, unless you're working with big files, it is probably faster to invoke awk
once than to invoke grep
twice. And, even if the input file is large enough to overcome the cost of invoking a second process, you'd have to benchmark whether awk
or a two grep
pipeline would be faster. (Without benchmarking, I'd guess that awk
would beat the pipeline.)
If i use this awk method, what would I do when the number next to VAS is diffrent, like if I want it to do that to a bunch of lines
You enter a different number.
Seriously: how would you supply that "bunch of" numbers?
for colon delimited input file try:
awk '
FILENAME ~ /list/ {vas[$1]=$1; next; }
vas[$3] {$1=$2=$3=""; sub(":::", ""); print $0;}
' list_of_VAS FS=: OFS=: infile
where list_of_VAS
is VAS numbers list file
Shell globbing: use *
to match any characters rather than a specific string:
var=`vastool list group tests_li004_developers`
echo "${var#*:VAS:*:}"
echo "${var#*:*:*:}"
Regular expression: use .*
to match any characters, and [^:]*
to match any but a :
character:
vastool list groups | sed "s/^[^:]*:VAS:[^:]*://"
vastool list groups | sed -n "/^tests_li004_developers:/ s/^[^:]*:VAS:[^:]*://p"
vastool list groups | sed -n "s/^tests_li004_developers:VAS:[^:]*://p"
awk can use regular expressions, too, but divides into fields $1 $2 ... that is often easier to deal with:
vastool list groups | awk -F":" '($1=="tests_li004_developers") {print $4}'
NB some awk versions have restrictions regarding line length.