This question already has answers here:
sed: print only matching group
(5 answers)
Closed 2 years ago.
how can I extract the TIC-9890 from a
branch name that looks like feature/TIC-9890/some-other-wording
I am not a SED expert, but I managed to come up with:
echo "feature/TIC-000/random-description" |
sed -n 's/.*\(TIC-[0-9]\{1,\}\).*/\1/'
This seems to work fine if the TIC-\d+ string is in there,
but returns the entire string if that is missing...
However, I need it to return null or empty string if the match isn't present.
You should add a p option to print and it should fly then. Why because we have stopped printing of sed by using -n option so when substitution happens then p needs to be used to print it.
echo "feature/TIC-000/random-description" | sed -n 's/.*\(TIC-[0-9]\{1,\}\).*/\1/p'
From man sed page:
-n, --quiet, --silent suppress automatic printing of pattern space
p Print the current pattern space.
OR as per #anubhava sir's comments one could use grep with -E option we could try:
echo "feature/TIC-000/random-description" | grep -oE 'TIC-[0-9]+'
Related
This question already has answers here:
Escape a string for a sed replace pattern
(17 answers)
Closed 4 years ago.
I cannot expand this variable in sed. I've tried everything I can think of.
I am trying to put the md5sum of file1 in line 10 of file2
I can take $x out of the regex and put some text and it works. It just will not accept the variable. printf the variable is fine.
#!/bin/bash
x=$(md5sum /etc/file1)
printf "$x \n"
sed -i 10"s/.*/$x/g" /usr/bin/file2
You may use this command that uses ~ as regex delimiter instead of / since output of md5sum contains /:
sed -i "10s~.*~$x~" /usr/bin/file2
After I reduced the variable from the md5sum output which includes the filename and directory by running $x thru:
x=$(echo $x | head -n1 | awk '{print $1;}')
Leaving only the MD5 it worked and quit erroring.
This question already has answers here:
How to extract string following a pattern with grep, regex or perl [duplicate]
(8 answers)
Closed 4 years ago.
Situation
There is a file called test that consists on the following text:
this is the first line
version=1.2.3.4
this is the third line
How can i print via bash only:
1.2.3.4
Note: I want always to print until end of line what is after "version=" not searching for 1.2.3.4
Thank you
Using GNU grep :
grep -Po '^version=\K.*'
-P enables PCRE regex, -o is used to only display what is matched rather than whole lines and the \K meta-character specifies not to display what precedes.
Using sed :
sed -n 's/^version=\(.*\)/\1/p'
-n disables auto-printing, then the substitution command will replace the "version=[...]" line by only its end through a capturing group. The substitution is only effective on the second line, which trigger the p instruction to print the (transformed) line.
you can use:
grep version file | cut -d\= -f2
This question already has answers here:
How can I output only captured groups with sed?
(11 answers)
Closed 5 years ago.
I saw many examples, but for some reason it still does not work for me.
This is the command I'm executing:
NUMBER=$(docker logs vault | grep Token)
NUMBER=${NUMBER##*": "}
NUMBER=$(echo $NUMBER | sed 's/^token=(.*)$//g')
echo $NUMBER
I want to get the value after '=', which is a string basically.
I tried using GREP, and other regex's but I either get nothing, or just the original string.
Please advise.
To get text after a delimiter better to use cut instead of sed as in this example:
echo 'token=dsa32e3' | cut -d= -f2
dsa32e3
-d= sets delimiter as = for cut
-f1 makes cut print first field
With sed you can simply remove the token=, with
NUMBER=$(echo token=dsa32e3 | sed 's/^token=//g')
echo $NUMBER
Other non-regexp based alternatives are possible, as other users pointed out.
Another fun possibility is using the negative lookbehind, not supported by sed, so I used perl.
NUMBER=$(echo token=dsa32e3 | perl -pe 's/.*(?<=token=)([a-z0-9]*)/$1/g')
echo $NUMBER
This question already has answers here:
get the next word after grep matching [duplicate]
(3 answers)
Closed 7 years ago.
I have a file called in.txt which contains a whole bunch of code, however I need to extract a user ID which is guaranteed to be of the form 'EID:nmb685', potentially with content before and/or after the guaranteed format. I want to extract the 'nmb685' using a bash script. I've tried some combinations of grep and sed but nothing has worked.
if your grep doesn't support -p but supports -o, you can combine grep and awk.
grep -o 'EID:\w\+' file|awk -F':' '{print $2}'
Though can it be done by awk alone, but this is more straightforward.
If your grep supports -P, perl-regexp parameter, you may use this.
grep -oP 'EID:\K\w+' file
What is being output after the ID? Is there anything consistent that you can match against?
If you know the length of the userid you can use:
grep "EID:......" in.txt > out.txt
or if you don't maybe something like this (checks all char/num followed by space, preceeded by EID:)
grep "EID:[A-Za-z0-9]* " in.txt > out.txt
Not very elegant, but this works:
grep "EID:" in.txt | sed 's/\(.*\EID:......\).*/\1/g' | sed 's/^.*EID://'
Select all lines with the substring "EID:"
Remove everything after "EID:" plus 6 characters
Remove everything before (and including) "EID:"
This question already has answers here:
sed search and replace strings containing / [duplicate]
(2 answers)
Closed 8 years ago.
I have a script which is getting value from a ".properties" file. It replaces the value successfully if it is a simple string but if contains escape characters like ('\') it does not work. Can anybody point out please that what to do, i have searched on the internet but unable to understand the "REGEX".
Script File:
#!/bin/bash
# Omer's First Script
#Include Properties File
. directoryPaths.properties
echo "Start"
sed -i "s/DONEDIRECTORY/$DoneDirectory/" *TestFile*
echo "finish"
directoryPaths.properties
DoneDirectory=/home/omerkhalid/Documents/Test/Done
TestFile.txt
This is a test Document.
DONEDIRECTORY
Error:
sed: -e expression #1, char 18: unknown option to `s'
Note:
If i change the value of "DoneDirectory" to simple string i.e. "Done" , it works fine. But with "/" escape characters it doesn't work.
Problem is not with the sed command but with the contents of the variable $DoneDirectory.
In the sed command
sed -i "s/DONEDIRECTORY/$DoneDirectory/" *TestFile*
/ is used as the delimitter, there by sed expects only 3 slashes following the s command. But once the variable $DoneDirectory is substituted there are 8 / which gives the error
Solution
Use some other delimeters in sed like
sed -i "s#DONEDIRECTORY#$DoneDirectory#" *TestFile*
Since the variable $DoneDirectory contains sed's default command delimiter / I would use a different delimiter:
sed -i "s#DONEDIRECTORY#$DoneDirectory#" *TestFile*
I would argue that sed is not the right tool for this job. Sure, you can work with other delimiters, but if the other delimiter shows up in your string (or a backslash, or something else that sed interprets), it's still going to explode. I believe awk is better suited:
awk -v subst="$DoneDirectory" '{ sub("DONEDIRECTORY", subst); print $0 }' TestFile
Note: use gsub instead of sub if DONEDIRECTORY can show up more than once in a line and both should then be substituted.