Changing contents of a specific parameter in a file through shell script [duplicate] - linux

This question already has answers here:
sed + replace only if match the first word in line
(3 answers)
Closed 1 year ago.
I have a file called local.conf:
db.default.driver="com.mysql.jdbc.Driver"
db.default.url="jdbc:mysql://localhost:3306/xyz31"
db.default.user="ron"
db.default.password=""
Here xyz31 is the variable ${DB_NAME}. I need to update only the ${DB_NAME} which is xyz31 (in this instance), but which varies depending on what the current of value ${DB_NAME} is, and which needs to be updated to whatever value the user has entered, for ex:abc22.
db.default.driver="com.mysql.jdbc.Driver"
db.default.url="jdbc:mysql://localhost:3306/abc22"
db.default.user="ron"
db.default.password=""
Is there a way to achieve this?

Use an editor like Unix' sed(1):
sed -i -e 's;xyz31;abc22; /your/funny/file/here
Might need to decorate with word beginning/end to avoid false positive matches (check your manual what is available). And/or write out another file and check with diff(1) that the change is right.

sed should be enough:
i=abc22 # or input from user by read i
sed "/^db.default.url/s/$DB_NAME/$i/" local.conf
Add -i option if you want to make change to the file.

Related

Removing everything after last hyphen in a string in Bash script? [duplicate]

This question already has answers here:
How can I remove all text after a character in bash?
(7 answers)
Closed last month.
Working on a script where I need to take a string, and remove everything after the last occurence of a certain character. In this case a hyphen.
For example, This-is-a-filename-0001.jpg should result in This-is-a-filename
You can cut strings in bash:
line="This-is-a-filename-0001.jpg"
echo "${line%-*}" # prints: This-is-a-filename
The %-*operator removes all beginning with the last hyphen.
You're looking for a sed within your script, something close to what's below.
sed 's!/[^/]*$!/!'
Generally, I would say, please do research before posting a question like yours since it's relatively easy to find the answers

Usage of '-' after pipe [duplicate]

This question already has answers here:
What's the magic of "-" (a dash) in command-line parameters?
(5 answers)
Closed 5 years ago.
I saw this line of code the other day and I didn't know exactly what is this - or when to use it. This is a simple code and from what I understood is that - takes the piped output and treats it as an argument of paste (correct me if I'am wrong)
seq $size | paste - $file
My question is when can we use this and is there another way to do the same thing?
Thanks,
This is documented in the man page for paste which says:
With no FILE, or when FILE is -, read standard input.
That is, paste expects you to give it a filename, but you can instead give it a single - instead, which paste will interpret as it should read data from stdin , your pipe in this case, instead of opening a file and read data from that file.

find string and replace

Hi I have a file like this
L_00001_mRNA_interferase_MazF
ATGGATTATCCAAAACAAAAGGATATTGTCTGGATTGATTTTGACCCTTCTAAAGGCAAA
GAGATAAGAAAGCGGAGACCTGCGTTAGTAGTTAGTAAAGATGAATTTAATGAACGTACA
GGTTTCTGTTTAGTTTGCCCCATCACATCTACTAAAAGGAACTTTGCAACGTATATTGAA
ATAACAGACCCACAGAAAGTAGAAGGGGACGTAGTTACCCATCAATTGCGAGCGGTTGAT
TACACCACAAGAAATATCGAAAAAATTGAACAATGTGATATGTTGACGTGGATTGATGTA
GTAGAAGTAATCGGAATGTTTATTTAA
L_00002_hypothetical_protein
ATGGAAACGGTAGTTAGAAAGATAGGGAATTCAGTAGGAACTATTTTTCCGAAAAGTATT
TCACCACAAGTTGGAGAAAAGTTCACTATTCTTAAAGTTGGGGAAGCGTATATATTGAAA
CCTAAGAGAGAAGATATTTTTAAAAATGCTGAAGATTGGGTAGGGTTTAGAGAAGCTTTG
ACTAATGAAGATAAAGAATGGGACGAGATGAAACTTGAGGGAGGAGAACGCTAG
L_00003_hypothetical_protein
ATGACAACGTTTGGAGAAATTCATAGCAATGCAGAAGGTTATAAAAACGATTTTAATGAG
TTGAATAAATTAGTATTACGTGTAGCTGAAGAAAAAGCAAAAGGAGAGCCATTAGTAACG
TGGTTTCGGTTGCGGAATCGTAGGATTGCACAAGTATTAGACCCAATGAAAGAAGAAGTA
GAAAGTAAATCAAAGTACGAAAAAAGAAGAGTAGCAGCAATTAGTAAAAGCTTTTTTCTA
CTTAAAAAAGCTTTTAACTTTATTGAAGCAGAACAATTTGAAAAAGCAGAAAAATTAATT
I would like to substitute the header of each sequence with a string.
I have a conversion file like
L_00001_mRNA_interferase_MazF galM,GALM,aldose1-epimerase[EC:5.1.3.3]
L_00002_hypothetical_protein E3.2.1.85,lacG,6-phospho-beta-galactosidase[EC:3.2.1.85]
L_00003_hypothetical_protein PTS-Lac-EIIB,lacE,PTSsystem,lactose-specificIIBcomponent[EC:2.7.1.69]
Your question is unclear as to what platform you're on (Windows, Linux, Mac, ...), what languages you're constrained to, and the exact details of your input files.
On the assumption that you're on Linux, or otherwise have sed and awk available and a command shell, it could be as simple as (where $ indicates a Bourne-like shell prompt):
$ awk '{print "s/^" $1 "/" $2 "/"}' conversions.txt > conversions.sed
$ sed -f conversions.sed sequences.txt > relabeled.txt
This assumes that your first file (with the headings you want changed) is called sequences.txt and your second file (the “conversion file”) is called conversions.txt. It is further assumed that the “conversion file” contains one record per line with exactly two fields — the original and substitute headers — separated by whitespace (i.e. neither the original header nor the new header contain any spaces) and no blank lines.
In this solution, the first (awk) line converts the conversions.txt file into a sed script, conversions.sed; the second (sed) line then runs this script on the sequences.txt file, producing the relabeled.txt file, which may (or may not) be what you're looking for.
Depending on the exact nature of your input files, which isn't clear from your question, this may need a bit of tweaking.
Hope this helps.

How to Format grep Output When Saving to a Variable [duplicate]

This question already has answers here:
How to preserve line breaks when storing command output to a variable?
(2 answers)
Closed 7 years ago.
If I grep our syslogs for a specific term, I get a nice output of those logs matching my term and each entry on a separate line.
If I save that to a variable so I can use it in a script as such:
results=$( grep "term" logs )
echo $results
then all the logs run together and are not human readable.
How can I make it look cleaner so when I do echo $results, I can actually read the output?
Thanks,
Quote it:
echo "$results"
This preserves all the whitespace, instead of using it for word splitting.
In general, you should almost always quote variables, unless you have a specific reason not to.

Changing the previous command in linux [duplicate]

This question already has answers here:
How can I replace ALL instances of a string in the previous command in Bash? [duplicate]
(6 answers)
^word^replacement^ on all matches in Bash?
(6 answers)
Closed 1 year ago.
Is there a way to change the previous command in linux? I'm copying a bunch of files using
cp path/to/source1 path/to/target2
and I want to change it to
cp path/to/source2 path/to/target2
so I want to replace the 1 with the 2
I know I can put this inside a loop but I need to do this after checking something in my notebook.
!!:gs/1/2/
Here are some more examples:
http://mark.stosberg.com/Tech/tips/bash.tips
the command for this manipulation of history is:
^old-text^new-text
For more reading I can just recommend the man page of bash, esp. the parts of readline and history.
I suppose you do this in a command line, not in bash script
the quickest way is to write cp and PRESS Alt+., press space, and Press Alt+. again and repair the path
Alt+. gives you last parameter you used
Ctrl+R gives you reverse search
UP arrow gives you latest commands
press the up arrow to get the last command, then change whatever you want to have different.
or if you want to address an older command hit 'Ctrl-r' then start typing what you want and if the correct old command appears hit right arrow to make it the current. Then change whatever you want to change.

Resources