I have the Path:
GarbageContainingSlashesAndDots/TOKEN/xyz/TOKEN/abc
How coukt I remove GarbageContainingSlashesAndDots?
I know, it is before TOKEN, but Unfortunately, there are two substrings TOKEN in string.
using sed s/.*TOKEN// makes my string to /abc,
but I need /TOKEN/xyz/TOKEN/abc
Thank You!!!
Divide and conquer:
$ echo 'Garbage.Containing/Slashes/And.Dots/TOKEN/xyz/TOKEN/abc' |
sed -n 's|/TOKEN/|\n&|;s/.*\n//;p'
/TOKEN/xyz/TOKEN/abc
Is perl instead of sed allowed?
perl -pe 's!.*?(?=/TOKEN)!!'
echo 'GarbageContainingSlashesAndDots/TOKEN/xyz/TOKEN/abc' | perl -pe 's!.*?(?=/TOKEN)!!'
# returns:
/TOKEN/xyz/TOKEN/abc
Sed does not support non-greedy matching. Perl does.
I think you have bash, so it can be a simple as
$ s="GarbageContainingSlashesAndDots/TOKEN/xyz/TOKEN/abc"
$ echo ${s#*/}
TOKEN/xyz/TOKEN/abc
or if you have Ruby(1.9+)
echo $s | ruby -e 'print gets.split("/",2)[-1]'
Thank you for all suggestions, I've learnt something new.
Finally I was able to reach my goal using grep -o
echo "GarbageContainingSlashesAndDots/TOKEN/xyz/TOKEN/abc" | grep -o "/TOKEN/.*/TOKEN/.*"
Using grep:
word='GarbageContainingSlashesAndDots/TOKEN/xyz/TOKEN/abc'
echo $word | grep -o '/.*'
echo "./a//...b/TOKEN/abc/TOKEN/xyz"|sed 's#.*\(/TOKEN/.*/TOKEN/.*\)#\1#'
UPDATE 2: have you tried this?
s!.*\(/TOKEN.+TOKEN.*\)!\1!
UPDATE: sorry, non-greedy matches are not supported by sed
Try this:
s/.*?TOKEN//
.*? matches only for the first occurance of TOKEN.
Related
I have a Build called 700-I20190808-0201. I need to convert it to 7.0.0-I20190808-0201. I can do that with regular expression:
sed 's/\([0-9]\)\([0-9]\)\([0-9]\)\(.\)/\1.\2.\3\4/' abc.txt
But the solution does not work when the build ID is 7001-I20190809-0201. Can we make the regular expression dynamic so that it works for both (700 and 7001)?
Could you please try following.
awk 'BEGIN{FS=OFS="-"}{gsub(/[0-9]/,"&.",$1);sub(/\.$/,"",$1)} 1' Input_file
If you have Perl available, lookahead regular expressions make this straightforward:
$ cat foo.txt
700-I20190808-0201
7001-I20190809-0201
$ perl -ple 's/(\d)(?=\d+\-I)/\1./g' foo.txt
7.0.0-I20190808-0201
7.0.0.1-I20190809-0201
You can implement a simple loop using labels and branching using sed:
$ echo '7001-I20190809-0201' | sed ':1; s/^\([0-9]\{1,\}\)\([0-9][-.]\)/\1.\2/; t1'
7.0.0.1-I20190809-0201
$ echo '700-I20190809-0201' | sed ':1; s/^\([0-9]\{1,\}\)\([0-9][-.]\)/\1.\2/; t1'
7.0.0-I20190809-0201
If your sed support -E flag:
sed -E ':1; s/^([0-9]+)([0-9][-.])/\1.\2/; t1'
sed -e 's/\([0-9]\)\([0-9]\)\([0-9]\)\(.\)/\1.\2.\3.\4/' -e 's/\.\-/\-/' abc.txt
This worked for me, very simple one. Just needed to extract it in my ant script using replaceregex pattern
I'm new to Sed, I'm trying to learn some pattern using Sed.
I got a filenamne.txt that has the following entry:
ppp/jjj qqq/kkk rrr/lll
My goal is to swap the word before the slash and the word after the slash in each of the three word1/word2 columns:
jjj/ppp kkk/qqq lll/rrr
I tried using sed –re ‘s!(.*)(/)(.*)!\1\2\!’ filename.txt, but it didn't work. Any idea how can I go about it?
$ echo "ppp/jjj qqq/kkk rrr/lll" | sed -e 's/$/ /' -e 's!\([^/]*\)/\([^ ]*\) !\2/\1 !g'
jjj/ppp kkk/qqq lll/rrr
Use replacement in perl command-line is a lot more straight-forward :-
perl -pe 's/(\w+)\/(\w+)/$2\/$1/g' file
jjj/ppp kkk/qqq lll/rrr
$ sed 's#\([^ ]*\)/\([^ ]*\)#\2/\1#g' file
jjj/ppp kkk/qqq lll/rrr
I'm trying to get some sed command to work without success...
echo -e "This.Is.a.Test.V03.r501.dump" | sed "s/^\(\w+(\.\w+)*\)\.V[0-9]{2}.*$/\1/g"
Basically, I want to match and return This.Is.a.Test while this \.V[0-9]{2} is fixed, but instead it returns the whole input string.
Any help is appreciated, thanks in advance!
\w matches alphanumerics, you are looking to capture only alphabets, so replace \w with [:alpha:]. Additionally {2} needs to be replaced with \{2\}. The following works with GNU sed
echo -e "This.Is.a.Test.V03.r501.dump" |
sed "s/^\([[:alpha:].]\+\)\.V[0-9]\{2\}.*$/\1/g"
This.Is.a.Test
Try this.
echo -e "This.Is.a.Test.V03.r501.dump" | sed -e "s/\(.*\)\.V[0-9]*.*/\1/"
Another way with sed
sed -r 's/^(([^.]+.){3})([^.]+).*/\1\3/'
Are you looking for this?
One way is to use awk
$ echo "This.Is.a.Test.V03.r501.dump" | awk -F'.' 'BEGIN{OFS=FS}{NF=4}1'
This.Is.a.Test
I'm trying to make sed match the last part of a url and output just that. For example:
echo "http://randomurl/suburl/file.mp3" | sed (expression)
should give the output:
file.mp3
So far I've tried sed 's|\([^/]+mp3\)$|\1|g' but it just outputs the whole url. Maybe there's something I'm not seeing here but anyways, help would be much appreciated!
this works:
echo "http://randomurl/suburl/file.mp3" | sed 's#.*/##'
basename is your good friend.
> basename "http://randomurl/suburl/file.mp3"
=> file.mp3
This should do the job:
$ echo "http://randomurl/suburl/file.mp3" | sed -r 's|.*/(.*)$|\1|'
file.mp3
where:
| has been used instead of / to separate the arguments of the s command.
Everything is matched and replaced with whatever if found after the last /.
Edit: You could also use bash parameter substitution capabilities:
$ url="http://randomurl/suburl/file.mp3"
$ echo ${url##*/}
file.mp3
echo 'http://randomurl/suburl/file.mp3' | grep -oP '[^/\n]+$'
Here's another solution using grep.
The file i am working with (oraInst.loc) looks like this:
inventory_loc=/u01/app/ORAENV/oracle/oraInventory
inst_group=dba
I need to use a regular expression to grab the value between app/ and /oracle. In this case it will be ORAENV but it could be any alphanumeric string of any case and length but with no spaces.
From what I have read so far using grouping seems to be the way to do this but I just can't get my head round it.
I am using egrep on Solaris 10 as the regex engine.
Try this:
\/app\/([\d\w]+)\/oracle\/
Try this, assuming your egrep has -o:
$ echo '/u01/app/ORAENV/oracle/oraInventory' | egrep -o '/app/[0-9A-Za-z]+/oracle/' | cut -d'/' -f3
Output:
ORAENV
Update, solution using sed:
$ echo '/u01/app/ORAENV/oracle/oraInventory' | sed 's:.*/app/\(.*\)/oracle/.*:\1:'
Something like:
app/(.*)/oracle
Would do the trick.
$ echo "inventory_loc=/u01/app/ORAENV/oracle/oraInventory"| nawk -F"/" '{for(i=1;i<=NF;i++)if($i=="app") {print $(i+1);exit} } '
ORAENV