bash script sed command unexpected EOF while looking for matching `"' [duplicate] - bash

This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 6 years ago.
I've been attempting to write a Bash script that automates everything needed to add a new piece of equipment to our MRTG graphs. Part of that requires me to edit a cfg file which I've read can be done with the sed command. The lines pasted below are where the error occurs when running the script giving me a "unexpected EOF while looking for matching `"' " error. town, tower, equipment, and direction are declared above. Any help in narrowing down what the problem might be would be a huge help!
newpattern="WorkDir: /var/www/html/mrtg/$town/$tower/$equipment$direction"
pattern="WorkDir: "
sudo sed -e "s/$pattern/$newpattern/" ~/MRTGconfigs/mrtg-BeatriceBSWT2960.cfg

You need to use something other than slashes in the s/// command because there are slashes galore in the replacement text:
newpattern="WorkDir: /var/www/html/mrtg/$town/$tower/$equipment$direction"
pattern="WorkDir: "
sudo sed -e "s%$pattern%$newpattern%" ~/MRTGconfigs/mrtg-BeatriceBSWT2960.cfg
I used % symbols instead; you can use any other character that appears neither in $pattern nor $newpattern. If need so be, you can use a control character such as Control-A; that works fine too.

Related

OSX paste command not pasting whole line [duplicate]

This question already has answers here:
Are shell scripts sensitive to encoding and line endings?
(14 answers)
Closed 1 year ago.
I am trying to paste two files together
file ip.txt
10.32.216.15
10.23.134.8
10.33.2.37
10.33.84.20
10.33.17.38
file obj.txt
obj-10.32.216.15
obj-10.23.134.8
obj-10.33.2.37
obj-10.33.84.20
obj-10.33.17.38
and I use the command like this:
paste ip.txt obj.txt
However I get this truncated output:
10.32.21obj-10.32.216.15
10.23.13obj-10.23.134.8
10.33.2.obj-10.33.2.37
10.33.84obj-10.33.84.20
10.33.17obj-10.33.17.38
The two files are created by grep command in my script earlier. This behavior is also present when I used variables with the command.
Also when I try to specify a delimiter only the second file gets pasted.
Does anyone know what might be the issue? Thanks
The issue was with line endings, for some reason Windows were set up but Unix were needed.

BASH echo showing some unusual behaviour [duplicate]

This question already has answers here:
Strange "echo" behavior in shell script
(3 answers)
Variables overwriting text problem with "echo" in Bash
(2 answers)
Closed 3 years ago.
This is the simple command that is giving me problems -
echo hafsda sfsdfdsfs $ymn $ymx $range
The output of this command is coming -
2.568 sfsdfdsfs 86.72
Where ymn = 86.72 ymx = 89.28 and range = 2.56. This only happens when I am using variables. The following command works fine -
echo hafsda sfsdfdsfs 1 2 $range
Also, the same command (the first one) works fine if I try running it directly in the terminal. This is only happening is a script. I also tried to use printf but encountered similar results.
I don't even understand what to google for to resolve this. I am unable to understand what is happening at all. So, what is happening here? Is this reproducible or is this just some error on my system, and if it is, what might be the problem?
Your script probably has DOS-style CRLF line endings. I suspect you actually have ymn="86.72\r" ymx="89.28\r" and range="2.56\r". You can test this in your script with
echo hafsda sfsdfdsfs $ymn $ymx $range | od -c
You can fix your script with dos2unix or sed -i 's/\r$// script.sh`.
Make sure you change the settings of your text editor do use unix line endings.

Strip last line in shell [duplicate]

This question already has answers here:
Remove the last line from a file in Bash
(16 answers)
Closed 5 years ago.
How can I strip the last line off a text file with Unix tools? Action can take place in place, so probably a version of truncate would be appreciated.
I guess there should be a solution using grep, sed, or head or similar tools, but I couldn't figure out a concise version.
Input:
one
two
three
Output:
one
two
sed is your friend. This prints everything but not the last line of a given file.
$ sed \$d file

Replace with sed appears to do nothing [duplicate]

This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed 8 years ago.
I've this code in my script:
no_forward="#net.ipv4.ip_forward=1"
forward="net.ipv4.ip_forward=1"
sed -i 's/$no_forward/$forward/' /etc/sysctl.conf
Based on the man page, -i suffix is not neccesary, but this this use I'll only have the modified file instead both of them (the file modified and the "backup", the previous one).
I need to use that vars, because I needing them after that command so It's useful to have them like that. I guess I should be wrong with the pattern string, but right now I can't find why. Or maybe the problem are the var's strings or their symbols?
Could You help me? I accept other solutions non-sed based if they use bash and don't need special commands, since I'll need to use the script in another computer without installing anymore.
Thanks for reading
If you want to be able to use vars in sed, use double quotes, so :
sed -i "s/$no_forward/$forward/" /etc/sysctl.conf

"Event not found" error for shell command in unix [duplicate]

This question already has answers here:
echo "#!" fails -- "event not found"
(5 answers)
Closed 5 years ago.
when i am trying to remove consecutive duplicate lines with
awk "!x[$0]++" file
its reporting x[: Event not found.
even the same case with
sed -i -e "$!N; /^\(.*\)\n\1$/!P;D" file as well reporting
N: Event not found.
i tried with single quotes too, it didn't help
Any idea to fix those
You're invoking the shell's history substitution. Surround the exclamation point with single quotes.

Resources