why questionmark comes in the end of filename when i create .txt file through shell script? [duplicate] - linux

This question already has answers here:
Shell Scripting unwanted '?' character at the end of file name
(2 answers)
Closed 6 years ago.
I am writing one shell script in which I am supposed to create 1 text file. When I do this, a question mark comes at the end of file name. what is the reason?
I am trying below methods in bash script.
1) grep ERROR a1* > text.txt
2) touch text.txt
In both the methods, instead of text.txt, there is a file generated as text.txt?
what should I do to overcome this?

Sounds like you script uses \r\n as line endings, this is typical DOS style line endings. Unix like systems uses \n. You should try to change the line feeds, eg with your favorite text editor:
vim +'set ff=unix | x' my_script
Or with dos2unix:
dos2unix my_script
Or with GNU sed:
sed -i 's/\r$//' my_script

Related

How to use sed to replace text with a file path? [duplicate]

This question already has answers here:
How to pass a variable containing slashes to sed
(7 answers)
Closed 2 years ago.
I'm writing a bash script where I need to replace text in a file with a specific file path, but my understanding is that sed does not work with specific characters such as /. I'm wondering if there is some way around this?
Here is my script currently:
currentdir="$PWD"
filepathvar="${currentdir}/settings.ini"
sed -i -e "s/filepath/$filepathvar/g" aimparmstest
When I print out filepathvar everything is as I expect it to be, but it seems the fact that filepathvar contains special characters, it gives me the following error:
sed: -e expression #1, char 13: unknown option to `s'
Is there any way around this? Or perhaps another command I can use? I haven't had any success with changing around the parameters. Any help is greatly appreciated.
You can use any character as the separator (the first character). For example:
echo "a/b/c" | sed -e 's|/|_|g'
In your case:
sed -i -e "s|filepath|$filepathvar|g" aimparmstest

How to source a key value paired file in bash escaping whitespace? [duplicate]

This question already has answers here:
Use key/value data from a file in a shell script
(1 answer)
Reading key/value parameters from a file into a shell script
(1 answer)
Closed 3 years ago.
$ cat foo.txt
a=1one
b=2two
c=3 three
d=4four
$ source foo.txt
bash: three: command not found...
Need to set all the variable listed in foo.txt, how to source this file by escaping the space character? foo.txt comes from other application, which I cannot control, or is there an alternative to source ?
If the output is so regular, you could try to preprocess the file using sed like this:
$ sed -e "s/=/='/;s/$/'/" < foo.txt >sourced.env
and then source sourced.env. This will add a ' just after the = and add an ending '.

How to replace a line with special characters of a file with Linux Bash instructions? [duplicate]

This question already has answers here:
Escape a string for a sed replace pattern
(17 answers)
Closed 4 years ago.
I am trying to modify the line of a phpmyadmin configuration file, precisely, the file /etc/phpmyadmin/config.inc.php, in the line on the database server, where I need to change it with a bash instruction. I tried to do it with the sed and awk commands, but I still have no results.
The line that I want to modify is the following:
$cfg['Servers'][$i]['host'] = $dbserver;
For the following value:
$cfg['Servers'][$i]['host'] = '192.168.0.10';
cat ini
"$cfg['Servers'][$i]['host'] = $dbserver;"
cat ini | sed "s/\$dbserver/'192.168.0.10'/"
"$cfg['Servers'][$i]['host'] = '192.168.0.10';"
In other words, a
sed -i.bak-e "s/\$cfg\['Servers'\]\[\$i\]\['host'\] = \$dbserver;/\$cfg['Servers'][\$i]['host'] = '192.168.0.10';/" /etc/phpmyadmin/config.inc.php
should do the job. (with a .bak copy as a precaution).

bash: initiate command inside of a string. - using sed [duplicate]

This question already has answers here:
How to replace a value with the output of a command in a text file?
(2 answers)
Closed 5 years ago.
my sed input is as follows:
sed 's/ListenAddress=.*/ListenAddress= $hostname/' nodemanager.properties
I am trying to run this against a Linux server and replace ListenAddress={current_value} with ListenAddress={hostname_of_server}
I need to know how to run the hostname command and have that output be reflected at the end of ListenAddress=
Thanks
If you wish your bash variables to reflect inside the sed script use, double quotes. The same is valid for command substitution. So you should be doing
sed "s/ListenAddress=.*/ListenAddress= $(hostname)/" nodemanager.properties
Since thevariable expansion takes place, you need to be careful about certain situations where $ appear as a sed attribute. For example if you're applying the above command only to the last line of the file, then do
sed "\$s/ListenAddress=.*/ListenAddress= $(hostname)/" nodemanager.properties
Note the $ before s command is escaped meaning that it is literal $ supplied to the script.

how to use variables in SED [duplicate]

This question already has answers here:
Replace a string in shell script using a variable
(12 answers)
Closed 8 years ago.
I have an xml file with line below
<config-root>/temp</config-root>
Using 'sed' is bash shell script I want to replace the line,
the 'sed' script is below
sed -i 's/<config-root>\(.*\)<\/config-root>/<config-root>\"${dirPath}"<\/config-root>/' Plan.xml
The 'sed' is resulting in
<config-root>"${dirPath}"</config-root>
I am expecting the line to be replaced as /opt/shared
Can anyone let me know what is wrong in my script? Basically I want to use variable in ‘sed’
Thanks in advance,
Babu
You can use bash to place the variable in the sed script: End the sed script using the single quote ', place the variable in double quotes " and continue the sed program with another single quote ':
sed 's~<config-root>[^<]*</config-root>~<config-root>'"$variable"'</config-root>~' Plan.xml
I would encourage you to use delimiter different from / because the / is part of the pattern (and of the variable) and would need to get escaped otherwise. I used ~ as the delimiter.

Resources