add line to a file ONLY if it is not in file already [duplicate] - linux

This question already has answers here:
Appending a line to a file only if it does not already exist
(25 answers)
Closed 1 year ago.
I want to add the following line:
nohup java -jar /mnt/fusion/nfs/labStats/LabInfoAutoLog.jar > /dev/null &
to the end of the file /etc/rc.d/rc.local if it does not already exist.
How can I do that from linux command line? I assume grep or sed would work, but I am not familiar enough with either to get it to work. Right now I use echo, but that just keeps adding it over and over again.

Assuming you want it at the end of the file:
LINE="nohup java -jar /mnt/fusion/nfs/labStats/LabInfoAutoLog.jar > /dev/null &"
FILE=/etc/rc.d/rc.local
grep -q "$LINE" "$FILE" || echo "$LINE" >> "$FILE"

one option is two steps:
grep -q "yourline" /path/file||sed -i '/..place../ a \the line' file
also possible to do with awk,
save all lines in array, during saving if the line was found, exit. otherwise, add the line in END{} block to the right place.
P.S. You didn't tell in the file, where to add that line.

Related

In Linux shell scripting if we want remove dupicate line, how can i do that except sort -u command [duplicate]

This question already has answers here:
How to delete duplicate lines in a file without sorting it in Unix
(9 answers)
Closed last month.
write a script name to read file name from the end user and remove duplicate line from in that file.
#! /bin/bash
read -p "Enter any file name to remove duplicate line:" $fname
sort -u $fname > tmp.txt
mv tmp.txt > $fname
here duplicate line will be remove but my content will be sorted but i don't that what should i do.
i want another method to remove duplicate line in shell scripting.
You can remove duplicate lines by the 'uniq' command.

I need a script to replace old libraries with newer library in all files [duplicate]

This question already has answers here:
How to replace a string in multiple files in linux command line
(28 answers)
How can I use a file in a command and redirect output to the same file without truncating it?
(14 answers)
Looping through the content of a file in Bash
(16 answers)
How to loop over files in directory and change path and add suffix to filename
(6 answers)
Closed 3 years ago.
I have numerous files in a directory and want to replace one of the libraries used in all of these files with a newer library because the old library no longer works.
I have used ls > names.txt to list all filenames into a txt document. I then attempted to write a bash script which would loop through all files, catch the old library, and replace it with the new library.
for entry in names.txt
do
sed 's/<libOld>/<libNew>/g' $entry > $entry
done
I expect the loop to go through each file name, find the old library used, and replace it with the new one. Running this script however doesn't appear to do anything.
You're bumping into a few common issues; I've closed as a duplicate, but here are the collected fixes for your specific case:
Editing a file in-place with sed
With GNU sed:
sed -i 's/<libOld>/<libNew>/g' filename
with any sed:
sed 's/<libOld>/<libNew>/g' filename > filename.tmp && mv filename.tmp filename
Looping over a file line by line
for entry in names.txt only ever sets entry to names.txt, it doesn't read its contents. This is also BashFAQ/001.
while IFS= read -r entry; do
printf '%s\n' "$entry"
done < names.txt
Looping over all files in a directory
You don't need a separate file, and you shouldn't use ls but globs:
for fname in ./*; do
printf '%s\n' "$fname"
done
Combined for your case
Notice the double quotes around $entry.
for entry in ./*; do
sed -i 's/<libOld>/<libNew>/g' "$entry"
done
which can be simplified to no loop at all:
sed -i 's/<libOld>/<libNew>/g' ./*

How to echo this entire code to .bashrc without leaving out characters/strings? [duplicate]

This question already has answers here:
How do I properly quote this bash pipeline for watch?
(2 answers)
Closed 3 years ago.
How can I echo this entire piece of code to .bashrc without leaving out a single character?
# automatic logging of terminal input/output
test "$(ps -ocommand= -p $PPID | awk '{print $1}')" == 'script' || (script -f -q /home/user/.logs/terminal/manjaro/$(date +"%Y-%m- %d_%H:%M:%S")_terminal.log)
When I attempt to enter the following into terminal:
echo "the above code" >> ~/.bashrc
I get the following appended to .bashrc which is nothing like "the above code", its short about 45 or so characters.
# automatic logging of terminal input/output
test script == 'script' || (script -f -q /home/user/.logs/terminal/manjaro/2019-05- 08_09:09:19_terminal.log)
As you can see, it's leaving out A LOT of the original code. I understand this has a lot to do with the number of different quotations and placement, but without altering my code much, or at least to the point where it can still function as its intended, how can I go about getting this to echo to the file properly?
Thank you for every nanosecond of your time.
Wrap your echo'd string with single quotes ' instead of double "

How to echo command output to end of existing line in Linux?

I am trying to automate the building of a config file and most of what I need can do done via a straight echo 'text here' >> myfile.conf
What I am having trouble with is appending the output of a command to the last line in the file.
My last working command is echo 'masternodeprivatekey=' >> myfile.conf
My next command is ./wallet-cli masternode genkey &>> myfile.conf
I need to have the masternode key generated to be included on the same line as masternodeprivatekey= so that the line will end up as this -
masternodeprivatekey=abcd13def456ghi789
Can this be done?
I have seen ppl suggest adding /c to the end of the echo command or -n to the end of the line however these don't work, they just add those characters to the end and still post the output of ./wallet-cli masternode genkey &>> myfile.conf on the next line.
Is it possible to do what I want? I'm pretty sure I could echo the output to two different temp files and then combine them, but is there an easier way?
You can use echo with option -n , it do not output the trailing newline
echo -n 'masternodeprivatekey=' >> myfile.conf
In sed, you can adress the last line with $
echo 'masternodeprivatekey=' >> myfile.conf
sed -i -r "$ s/(.*)/\1$(($RANDOM%100000))/" myfile.con
Check, whether your sed has -i and -r parameters. This worked with GNU-sed.
Since I don't know ./wallet-cli, I used something else in $(...) instead, to perform a command which generates output. So for you, it might be
sed -i -r "$ s/(.*)/\1$(./wallet-cli masternode genkey)" myfile.con
Since you seem to know before, that you will add something after the equal sign, why don't you do:
echo "masternodeprivatekey=$(./wallet-cli masternode genkey)" >> myfile.con
As per Mark Plotnick's suggestion, the use of printf instead of echo works perfectly.

sed changes are lost (while running cat command on txt file) [duplicate]

This question already has answers here:
Find and replace in file and overwrite file doesn't work, it empties the file
(12 answers)
sed edit file in place
(15 answers)
Closed 6 years ago.
I need to insert a command "new file" in a test.txt file at line number 4.
Tried sed; I can see the changed file output, but when I again do cat test.txt, the changes are gone.
sed "4i new file" /test.txt
How can I save the changes?
Use in place edit option sed -i "4i new file" test.txt
Without the -i option sed will not make any changes to the file. It will only print the result.
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if SUFFIX supplied)
sed '4i new file' test.txt > tmp && mv tmp test.txt

Resources