regular expression pattern replacement *.xml - regex

I am new to regular expression, and don't deal with it regularly so posting it as a question.
I want to replace
blah.xml
haha.xml
to
user/home/blah.xml
user/home/haha.xml
I would prefer to do it with sed.
Cheers
SK

You can use sed as:
$ cat file
foo
blah.xml
haha.xml
bar
$ sed -r 's#([^.]*\.xml)#user/home/\1#g' file
foo
user/home/blah.xml
user/home/haha.xml
bar
To answer your question in the comments, try:
$ echo "file is blah.xml" | sed -r 's#(\w+\.xml)#user/home/\1#'
file is user/home/blah.xml

sed 's=\(.*\.xml\)=user/home/\1=g'

sed -e 's#^#user/home/#'
inserts user/home/ at the beginning of each line.

Related

Transform a dynamic alphanumeric string

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

Swap columns in bash using SED without using loop

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

Regular expression in sed

I try to use regex in a sed command. I try to replace a pattern. My pattern is a line in /etc/security/policy.conf file. The value is unknown it should be either:
#LOCK_AFTER_RETRIES=NO or LOCK_AFTER_RETRIES=4 (or any number here). I want to replace found pattern with the value LOCK_AFTER_RETRIES=6. So I tried the command:
sed 's/#*LOCK_AFTER_RETRIES=[a-zA-Z0-9][a-zA-Z0-9]/LOCK_AFTER_RETRIES=6/g' ./policy.conf
But it didn't look to be working.
Thanks.
give this a try:
sed 's/^#\?\s*LOCK_AFTER_RETRIES=[^=]*/LOCK_AFTER_RETRIES=6/'
test with some example:
# LOCK_AFTER_RETRIES=NO
#LOCK_AFTER_RETRIES=NO
LOCK_AFTER_RETRIES=4
LOCK_AFTER_RETRIES=4
kent$ sed 's/^#\?\s*LOCK_AFTER_RETRIES=[^=]*/LOCK_AFTER_RETRIES=6/' f
LOCK_AFTER_RETRIES=6
LOCK_AFTER_RETRIES=6
LOCK_AFTER_RETRIES=6
LOCK_AFTER_RETRIES=6
Is this what you want?
sed -i 's/LOCK_AFTER_RETRIES=.*/LOCK_AFTER_RETRIES=6/g' filename
I test like this and it works:
echo "LOCK_AFTER_RETRIES=4\n#LOCK_AFTER_RETRIES=NO" | sed 's/LOCK_AFTER_RETRIES=.*/LOCK_AFTER_RETRIES=6/g'
[EDIT]
After checking other's answer, I am not sure whether you want to replace #LOCK_AFTER_RETRIES=NO to LOCK_AFTER_RETRIES=6 or #LOCK_AFTER_RETRIES=6.
My above answer will change #LOCK_AFTER_RETRIES=NO to #LOCK_AFTER_RETRIES=6
If you want to change change #LOCK_AFTER_RETRIES=NO to LOCK_AFTER_RETRIES=6, try:
sed -i 's/^#*LOCK_AFTER_RETRIES=.*/LOCK_AFTER_RETRIES=6/g' filename
In summary:
echo "LOCK_AFTER_RETRIES=4\n#LOCK_AFTER_RETRIES=NO" | sed 's/LOCK_AFTER_RETRIES=.*/LOCK_AFTER_RETRIES=6/g'
LOCK_AFTER_RETRIES=6
#LOCK_AFTER_RETRIES=6
echo "LOCK_AFTER_RETRIES=4\n#LOCK_AFTER_RETRIES=NO" | sed 's/^#*LOCK_AFTER_RETRIES=.*/LOCK_AFTER_RETRIES=6/g'
LOCK_AFTER_RETRIES=6
LOCK_AFTER_RETRIES=6
Try this:
sed '/LOCK_AFTER_RETRIES/s/=.*/=6/g' ./policy.conf

BASH: replacing PERL with SED for in-place substitution

Would like to replace this statement with perl:
perl -pe "s|(?<=://).+?(?=/)|$2:80|"
with
sed -e "s|<regex>|$2:80|"
Since sed has a much less powerful regex engine (for example it does not support look-arounds) the task boils down to writing a sed compatible regex to match only a domain name in a fully qualitied URL. Examples:
http://php2-mindaugasb.c9.io/Testing/JS/displayName.js
http://php2-mindaugasb.c9.io?a=Testing.js
http://www.google.com?a=Testing.js
Should become:
http://$2:80/Testing/JS/displayName.js
http://$2:80?a=Testing.js
http://$2:80?a=Testing.js
A solution like this would be ok:
sed -e "s|<regex>|http://$2:80|"
Thanks :)
Use the below sed command.
$ sed "s~//[^/?]\+\([?/]\)~//\$2:80\1~g" file
http://$2:80/Testing/JS/displayName.js
http://$2:80?a=Testing.js
http://$2:80?a=Testing.js
You must need to escape the $ at the replacement part.
sed 's|http://[^/?]*|http://$2:80|' file
Output:
http://$2:80/Testing/JS/displayName.js
http://$2:80?a=Testing.js
http://$2:80?a=Testing.js

Regex with sed to parse archive name

I'd like to parse different kinds of Java archive with the sed command line tool.
Archives can have the followin extensions:
.jar, .war, .ear, .esb
What I'd like to get is the name without the extension, e.g. for Foobar.jar I'd like to get Foobar.
This seems fairly simple, but I cannot come up with a solution that works and is also robust.
I tried something along the lines of sed s/\.+(jar|war|ear|esb)$//, but could not make it work.
You were nearly there:
sed -E 's/\.+(jar|war|ear|esb)$//' file
Just needed to add the -E flag to sed to interpret the expression. And of course, respect the sed 's/something/new/' syntax.
Test
$ cat a
aaa.jar
bb.war
hello.ear
buuu.esb
hello.txt
$ sed -E 's/\.+(jar|war|ear|esb)$//' a
aaa
bb
hello
buuu
hello.txt
Using sed:
s='Foobar.jar'
sed -r 's/\.(jar|war|ear|esb)$//' <<< "$s"
Foobar
OR better do it in BASH itself:
echo "${s/.[jwe]ar/}"
Foobar
You need to escape the | and the () and also add ' if you do not add option like -r or -E
echo "test.jar" | sed 's/\.\(jar\|war\|ear\|esb\)$//'
test
* is also not needed, sine you normal have only one .
On traditionnal UNIX (tested with AIX/KSH)
File='Foobar.jar'
echo ${File%.*}
from a list having only your kind of file
YourList | sed 's/\....$//'
form a list of all kind of file
YouList | sed -n 's/\.[jew]ar$/p
t
s/\.esb$//p'