PATH="/dir1/subdir1"
Want to replace "/" with "-" from the above string
and the result should be
-dir1-subdir1
You can check the more detailed version.
$ var="001244abc"
$ replace="polo"
$ echo ${var//001244/$replace}
poloabc
Hope it helps..
You can use tr for that since you want to replace a single character with another one:
echo $PATH | tr "/" "-"
Result is: -dir1-subdir1
In bash you can use a pattern substitution of the form ${parameter//pattern/string} to replace all matches of pattern with string:
echo "${PATH//\//-}"
Note that your pattern string / must be escaped with \/.
Related
Given a list of paths separated by a single space:
/home/me/src/test /home/me/src/vendor/a /home/me/src/vendor/b
I want to remove the prefix /home/me/src/ so that the result is:
test vendor/a vendor/b
For a single path I would do: ${PATH#/home/me/src/} but how do I apply it to this series?
You can use // to replace all occurrences of substring. Replace it with null string to remove them.
$ path="/home/me/src/test /home/me/src/vendor/a /home/me/src/vendor/b"
$ echo ${path//\/home\/me\/src\/}
test vendor/a vendor/b
Reference: ${parameter/pattern/string} in Bash reference manual
Using shell parameter expansion doesn't seem to be the solution for this, since it would remove everything up to / from a given point is useful, as nu11p01n73R's answer reveals.
For clarity, I would use sed with the syntax sed 's#pattern#replacement#g':
$ str="/home/me/src/test /home/me/src/vendor/a /home/me/src/vendor/b"
$ sed 's#/home/me/src/##g' <<< "$str"
test vendor/a vendor/b
Like always a grep solution from my side :
echo 'your string' | grep -Po '^/([^ /]*/)+\K.+'
Please note that the above regex do this for any string like /x/y/z/test ... But if you are interested only in replacing /home/me/src/, try the following :
echo 'your string' | grep -Po '^/home/me/src/\K.+' --color
I have this text
" File: 'space folder' "
I want to replace this with only this
" space folder "
using sed or awk?
But when i try to do with it using sed it's not taking the command!
Does anyone have solution for this.
If I get your intent correctly, you need all text between single quotes; you can use this:
$ sed -r "s/^.*'([^']*)'.*$/\"\1\"/g" <<< "\" File: 'space folder' \""
"space folder"
$
Edit1: explanation
command <<< string => <<< indicates here string that is you pass a string to the command.
Our final string is this:
$ echo -e "\" File: 'space folder' \""
" File: 'space folder' "
$
since our string contains single quotes we use double quotes for sed command:
-r switch enables extended regular expression
"s/^.*'([^']*)'.*$/\"\1\"/g"
the above command substitutes the whole line with text present between single quotes.
Regular expression breakdown:
^ matches start of line
.* matches 0 or more characters
' matches a literal single quote
([^']*) matches 0 or more characters that are not single quote
and remembers it as a captured group with backreference number \1
' matches literal single quote
.* matches 0 or more chars
$ matches end of line
I have a txt file contains many strings(every string lies in a line). A typical string has this shape:
sno_Int-INT1_Exp-INT2_INT3.fits.fz_ovsc_rms_D4_D5_D6_D7_D8_D9
In the above string, "INT1", "INT2" and "INT3" are all integer types and their values might variant for each string in the text file, "D4 - 9" are double type(not fixed value also).
What I need to do is to change the above string to a new string like :
INT3_ovsc_rms_D4_D5_D6_D7_D8_D9
Can anybody tell me how to do it ?
Thanks!
#!/bin/bash
input=$1
left=${input%%.*}
right=${input#*.fz_}
int3=${left##*_}
output=${int3}_${right}
echo "${output}"
.
$ ./foo.sh sno_Int-INT1_Exp-INT2_INT3.fits.fz_ovsc_rms_D4_D5_D6_D7_D8_D9
INT3_ovsc_rms_D4_D5_D6_D7_D8_D9
$ ./foo.sh sno_Int-300_Exp-1000_1051.fits.fz_ovsc_rms_10.6_2.35_53.2_0_5.92_2.14
1051_ovsc_rms_10.6_2.35_53.2_0_5.92_2.14
Depending on your real input this might break horribly, though.
If you really want to do this in pure Bash, you'll need to split the string by setting IFS and then using read with a "here string". For details, see here: How do I split a string on a delimiter in Bash?
You will probably need to split it multiple times--once by underscore and then by dash, I guess.
If you don't mind awk:
echo sno_Int-INT1_Exp-INT2_INT3.fits.fz_ovsc_rms_D4_D5_D6_D7_D8_D9 | awk -F_ 'BEGIN{OFS="_"}{sub(/.fits.fz/,"",$4);print $4,$5,$6,$7,$8,$9,$10,$11,$12}'
INT3_ovsc_rms_D4_D5_D6_D7_D8_D9
This awk should work:
s='1000_1051.fits.fz_ovsc_rms_10.6_2.35_53.2_0_5.92_2.14'
awk -F'[_.]' 'NR==1{i3=$2;next} {printf "%s%s%s", i3, RS, $0}' RS='_ovsc_rms' <<< "$s"
1051_ovsc_rms_10.6_2.35_53.2_0_5.92_2.14
I need to replace a space ( ) with a dot (.) in a string in bash.
I think this would be pretty simple, but I'm new so I can't figure out how to modify a similar example for this use.
Use inline shell string replacement. Example:
foo=" "
# replace first blank only
bar=${foo/ /.}
# replace all blanks
bar=${foo// /.}
See http://tldp.org/LDP/abs/html/string-manipulation.html for more details.
You could use tr, like this:
tr " " .
Example:
# echo "hello world" | tr " " .
hello.world
From man tr:
DESCRIPTION
Translate, squeeze, and/or delete characters from standard input, writ‐
ing to standard output.
In bash, you can do pattern replacement in a string with the ${VARIABLE//PATTERN/REPLACEMENT} construct. Use just / and not // to replace only the first occurrence. The pattern is a wildcard pattern, like file globs.
string='foo bar qux'
one="${string/ /.}" # sets one to 'foo.bar qux'
all="${string// /.}" # sets all to 'foo.bar.qux'
Try this
echo "hello world" | sed 's/ /./g'
Use parameter substitution:
string=${string// /.}
Try this for paths:
echo \"hello world\"|sed 's/ /+/g'|sed 's/+/\/g'|sed 's/\"//g'
It replaces the space inside the double-quoted string with a + sing, then replaces the + sign with a backslash, then removes/replaces the double-quotes.
I had to use this to replace the spaces in one of my paths in Cygwin.
echo \"$(cygpath -u $JAVA_HOME)\"|sed 's/ /+/g'|sed 's/+/\\/g'|sed 's/\"//g'
The recommended solution by shellcheck would be the following:
string="Hello World" ; echo "${string// /.}"
output: Hello.World
I know I can translate upper to lower case letters by
echo 'linux' | tr "a-z" "A-Z"
how would I translate or replace an occurrence of & with %20%26%20. Maybe something like this
echo '&' | tr "&" "%20%26%20"
sed -i 's/&/%20%26%20/g' inputfile
will edit the file in place.
you can just use the shell.
$ var="&test"
$ echo ${var//&/%20%26%20}
%20%26%20test
The reason that tr command does not work is that tr treats it's 2 arguments, not as arbitrary strings, but as lists. tr finds "&" in the string; it's the first element in the first argument; it gets replaced by the corresponding first element in the second argument.
From the man page:
When the -d option is not specified:
o Each input character found in the array specified by
string1 is replaced by the character in the same rela-
tive position in the array specified by string2. When
the array specified by string2 is shorter that the one
specified by string1, the results are unspecified.
The sed and bash solutions offered are what you want.