Question:
I have a shell script that’s auto generated by a software and it looks something like:#!/bin/sh
# The directory of this script is the expanded absolute path of the "$qt_prefix/bin" directory.
script_dir_path=`dirname $0`
script_dir_path=`(cd "$script_dir_path"; /bin/pwd)`
/home/akshay/Qt/6.2.4/gcc_64/bin/qmake -qtconf "$script_dir_path/target_qt.conf" $*
I want to replace the last line’s first three separators to:#!/bin/sh
# The directory of this script is the expanded absolute path of the "$qt_prefix/bin" directory.
script_dir_path=`dirname $0`
script_dir_path=`(cd "$script_dir_path"; /bin/pwd)`
/opt/qt/6.2.4/gcc_64/binqmake -qtconf "$script_dir_path/target_qt.conf" $*
I tried getting the last line by doing:qmake_path=$(tail -n 1 $(find ${QT_ROOT}/android* -maxdepth 0 -type d | sort -r | head -1)/bin/qmake)
Then I used read
to split it by space, I don’t know how to get the first index, in this case the path. Any help on this is appreciated.Answer:
If it is just the short script you show, you can handle the change withsed
, e.g.,sed -i 's/^\/home\/akshay\/Qt/\/opt\/qt/' script.sh
Where -i
will edit in-place and the normal substitution of s/find/replace/
is used to locate a line beginning with ^\/home\/akshay\/Qt
(e.g. "/home/akshaw/Qt"
) and replace as \/opt\/qt
(e.g. "/opt/qt"
)Example Use/Output
Showing the result of the substitution output to
stdout
you would have:$ sed 's/^\/home\/akshay\/Qt/\/opt\/qt/' script.sh
#!/bin/sh
# The directory of this script is the expanded absolute path of the "$qt_prefix/bin" directory.
script_dir_path=`dirname $0`
script_dir_path=`(cd "$script_dir_path"; /bin/pwd)`
/opt/qt/6.2.4/gcc_64/bin/qmake -qtconf "$script_dir_path/target_qt.conf" $*
note: you can also use alternate delimiters to avoid having to escape '/'
, e.g.sed 's#^/home/akshay/Qt#/opt/qt#' script.sh
That may be easier on the eyes.Replacing 3-Words Generically
If the text of the first three words in
"/one/two/three/..."
can change and need to be replaced generically, you can simply use a character list to require one-or-more characters not '/'
followed by '/'
using [^/]+/
, you can do:sed -E -i 's#^/[^/]+/[^/]+/[^/]+/#/opt/qt/#' script.sh
The -E
option is also used to specify “extended REGEX” syntax making the '+'
(one-or-more) repetition character available.With the change, it doesn’t matter what the first 3-words are (so long as there is a
'/'
after the 3rd word, they will be replaced with "/opt/qt/"
.If you have better answer, please add a comment about this, thank you!