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

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
"""
}```

Related

extract starting and ending with a special character [duplicate]

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/

Insert text in specific line of file by using sed. Text is including variable, which should be added into file in double quotation [duplicate]

This question already has answers here:
How to escape single quotes within single quoted strings
(25 answers)
Closed 3 years ago.
I need your help.
I am creating bash script and I have problem in part where I need to add text in line 4 of test.txt file. My text is including variable. I know how to add text with variable, but in this case this variable must be in double quotation.
So, I am using this command:
sed -i "4s/$/Username = "$var1"/g" $dir/output/test.txt
but I get next results in test.txt file:
$Username = example
I tried many options but I can not achive to get variable in double quotation:
$Username = "example"
sed -i "4s/$/Username = \"$var1\"/" $dir/output/test.txt
Global switch is meaningless and can be removed since you're appending to the end of the line

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

I cannot make echo display the name with an _ symbol [duplicate]

This question already has answers here:
How to echo "$x_$y" in Bash script?
(4 answers)
When do we need curly braces around shell variables?
(7 answers)
Closed 5 years ago.
I am new at this and I'll simplify my problem. I doing the following script:
echo "Insert number of elements:"
read elements
echo "Insert md number:"
read sim
echo "uni_$elements_md$sim.tpr" >> doc.bash
But when I run the doc.bash file it only shows inside:
uni_4.tpr
Please help me, how must I do my echo so that if I put elements as 14 and sim as 4 (just to put an example), it displays:
uni_14_md4.tpr
Thanks.

Passing multiple arguments in Bash Script containing space [duplicate]

This question already has answers here:
Iterate over a list of files with spaces
(12 answers)
How to iterate over arguments in a Bash script
(9 answers)
Closed 5 years ago.
I just started learning Bash Script.
My script is accepting 1 to n number of arguments. Each argument is pass to a function rename.
My problem is that the argument I pass does not accept space.
script.sh
#!/bin/bash
for FILE in $#
do
echo "$FILE"
rename $FILE
done
For Ex:
./script.sh /Users/xyz/Section 5/abc/ /Users/xyz/pqr/ /Users/z/abc
The above argument "/Users/xyz/Section 5/abc/" should be one even if it contains space.
But the code in script.sh will break it into two argument.
So the output is:
/Users/xyz/Section
5/abc/
/Users/xyz/pqr/
/Users/z/abc
But My Expected Output should be:
/Users/xyz/Section 5/abc/
/Users/xyz/pqr/
/Users/z/abc
Note: Different solution I tried till now:
1) "/Users/xyz/Section 5/abc/" --> Same output ie 2 different argument
2) '/Users/xyz/Section 5/abc/' --> Same output ie 2 different argument
3) /Users/xyz/Section\ 5/abc/ --> Same output ie 2 different argument

Resources