extract starting and ending with a special character [duplicate] - linux

This question already has answers here:
Extracting directory name from an absolute path using sed or awk
(7 answers)
How to extract directory path from file path?
(9 answers)
Closed 2 months ago.
extract starting and ending with a special character
Content from the file:
/genomes/date/pa341/abc.txt
/genomes/date/ha76870/xyz/a1.pdf
Result should be
/genomes/date/pa341/
/genomes/date/ha76870/xyz/

Related

Directory with colon in PATH variable [duplicate]

This question already has answers here:
How to escape colon (:) in $PATH on UNIX?
(4 answers)
If there is a colon (:) in the directory name, how could I add it to $PATH? [duplicate]
(1 answer)
Closed 2 years ago.
Linux allows : character in file (or directory) names. Can a directory containing : in its name be added to shell PATH variable without screwing up PATH?
According to POSIX specification:
The prefixes shall be separated by a colon ( ':' )
Then, if you add a directory with <colon> to PATH It'll interpreted like a different path and sure you'll get a error.
https://pubs.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap08.html#tag_08_03

Delete the data instead of updating using sed? [duplicate]

This question already has answers here:
How can I use a file in a command and redirect output to the same file without truncating it?
(14 answers)
Is it possible to modify lines in a file in-place?
(5 answers)
How to search and replace text in a file?
(22 answers)
Closed 3 years ago.
I am facing a strange issue, I have a piece of code written in groovy and contains a shell script to update the values of variables in system.properties file.
Most of the times the script works fine, but 20% of the times it deletes all the file content instead of updating the variables
def call(credentialsId, repoName, repoAddress, variableToUpdate, releaseType, BRANCH_NAME, branchToCheckoutInRepo, OS='linux'){
def now = new Date()
sh """
cat system.properties | sed -e 's#${variableToUpdate}=.*#${variableToUpdate}=${BRANCH_NAME}#g; s#releaseType=.*#releaseType=${releaseType}#g; s#lastModified=.*#lastModified="${now}"#g' | tee system.properties
"""
}```

expand unix variable inside sed command [duplicate]

This question already has answers here:
Replace a string in shell script using a variable
(12 answers)
sed substitution with Bash variables
(6 answers)
Closed 4 years ago.
I need to replace current value in configuration file with new value which is assigned to variable ,
like
file_name=abc.txt
needs to be replaced like
file_name=xyz.txt
where $file=xyz.txt
I tried
sed -i 's/file_name=.*/file_name=$file/g' conf_file.conf
however the variable is not getting expanded,
it comes like file_name=$file.
any pointers?
This should work,assuming that variable file has value:xyz.txt assigned to it:
sed "s/file_name=.*/file_name=${file}/g" file_name
Output:
file_name=xyz.txt

Passing variable and evaluating string [duplicate]

This question already has answers here:
Lookup shell variables by name, indirectly [duplicate]
(5 answers)
How to get a variable value if variable name is stored as string?
(10 answers)
Bash - variable variables [duplicate]
(4 answers)
Closed 4 years ago.
I have a set of directories listed inside a text file as
DIR_A= (name of directory 1)
DIR_B= (name of directory 2)
....
I have a second script to which I would like to pass an argument like sh Scriptname varname where varname could be A, B, ...
Scriptname sources the initial text file and I would like this script to accept the passed varname (using $1) to echo DIR_varname.
Any ideas? TIA

Removing everything but filename extension [duplicate]

This question already has answers here:
Extract filename and extension in Bash
(38 answers)
Closed 7 years ago.
Let's say I have a string:
x=file.tar.sh
I know how to remove everything but last n-characters. Like this (removing everything but last 3 characters:
${x: -3}
But this doesn't work for files with different suffix lengths. (len .tar != len .sh)
I would tackle this by removing everything until the last dot. I've tried this:
${x##.}
This removes the longest matching until "." but somehow it just returns the full string without removing anything?
Try this:
x=file.tar.sh
echo ${x##*.}
This will print sh
If you want to get tar.sh, then:
echo ${x#*.}
Here * matches any set of characters before the occurrence of .

Resources