This question already has answers here:
How can I get sed to change all of the instances of each letter only once?
(3 answers)
Closed 3 years ago.
I have a text with random alphabetic characters and I want to change every character to his opposit one.
ex. i have a character z, i change it in to a, b to y etc.
I can't really find a better way to do this unless i do
sed -r -e 's/a/z/' -e 's/b/y/' ... 's/z/a/'
Is there a way to do this in a more simple way?
I just want to use the -r option in sed.
Using the y command maybe?
tr is easier,
e.g. for lowercase chars
$ z_a=$(echo {z..a} | tr -d ' '); echo adfa alfja | tr a-z $z_a
zwuz zouqz
the detour to create z-a is required since tr can't handle "reverse collating sequence order".
I know this isn't a sed solution, but this seems a simple, straight forward use of perl:
cat input_file | perl -ple 's/(.)/chr(25-ord($1)+ord("a")*2)/eg'
sed comes, one way is like this:
sed -r 'y/'"$(echo {a..z})"'/'"$(echo {z..a})"'/' file
Related
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]+'
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:
Difference between single and double quotes in Bash
(7 answers)
Closed 8 years ago.
I am trying to comment the lines in my scripts where a pattern from a given list of patterns is present. Now, I am able to do it the following way on command line :
sed '/abcdefg/ s/^/#/' file.sql > file.commented
But if I use a variable for pattern (instead of abcdefg directly as above) I'm not able to do the same.
pattern=abcdefg
sed '/$pattern/ s/^/#/' file.sql > file.commented
Looks like it is escaping the dollar character and not taking the value of the variable.
How do you do the same with awk?
You need to use double quote to make it work with variables in shell:
sed "/$pattern/ s/^/#/" file.sql > file.commented
You can also use inline feature of shell to save changes in input file itself
sed -i.bak "/$pattern/ s/^/#/" file.sql
However it is best to avoid sed for this job since it uses regex and above command will break if $pattern contains / or some special regex meta character. Better to use awk like this:
awk -v p="$pattern" 'index($0, p) {$0 = "#" $0} 1' file.sql > file.commented
This question already has answers here:
Use sed to replace all backslashes with forward slashes
(9 answers)
Closed 8 years ago.
I want to be able to use sed to take an input such as:
\\servername\folder\folder\folder
To
//servername/folder/folder/folder
seen this question here but the answer "echo 'C:\foo\bar.xml' | sed 's/\///g'" provide this string: "/servername/folder/folder/folder" (one forward slash is missing).
sed seems a bit overkill since you want only transliterate a single character. You can use tr that is designed for this specific task:
echo '\\servername\folder\folder\folder' | tr '\\' '/'
As #Kent notices it, sed has the transliteration command y:
echo '\\servername\folder\folder\folder' | sed 'y#\\#/#'
You can use:
s='\\servername\folder\folder\folder'
sed 's~\\~/~g' <<< "$s"
//servername/folder/folder/folder