How to add a character to the end of each line in a file in shell script [duplicate] - linux

This question already has answers here:
sed edit file in place
(15 answers)
Closed 8 years ago.
I need a script that add a particular character to the end of each line . I am using the command
sed 's/$/ foo/' r.txt
It adds foo to end of each line in the file r.txt and displays in my terminal .
What do i need to do if i want to save this existing file with this new record appended after the end of each line .

To save to a new file:
sed 's/$/ foo/' r.txt > newfile.txt
To edit in place
sed -i 's/$/ foo/' r.txt

Related

Removing new lines from text file in order for text to appear on a single line using awk,tr or sed [duplicate]

This question already has answers here:
How can I replace each newline (\n) with a space using sed?
(43 answers)
Closed 2 years ago.
How do we remove newlines in a test.txt file so that the text now appears on a single line using tr,awk or sed?
E.g
My name is mo
Learning linux
live in CAD
If I want that text to appear on one line and save it to a new file called passed.txt. What command should I run?
With awk:
awk '{ printf "%s",$0 }' file > passed.txt # print each line ($0) of file with no new lines
With tr:
tr -d '\n' < file > passed.txt # Use -d to delete new lines (\n)
xargs and printf is also an option:
xarg printf "%s" < file > passed.txt # Redirect file into printf and print each default space variable without new lines.
Output is redirected to passed.txt with each example

How to delete 1 or more matching line(s) while reading a file in bash script [duplicate]

This question already has answers here:
How to pass a variable containing slashes to sed
(7 answers)
Combining two sed commands
(2 answers)
Linux, find replace on a folder of files using a list of items for replacement?
(1 answer)
Closed 4 years ago.
I want to read file using a bash script and delete line(s) which are matching with my specific scenario (line(s) starting with 'z').
my code works fine if the 'inputFile' contains only alphabetic characters.
but, if a line with 'specific characters of sed' (line eg : z-2.10.3.2 x/y/z F (&)[]+* ) then i got an error,(error : sed: -e expression #1, char 29: unterminated `y' command).
#!/bin/bash
inputFile="test.txt"
while IFS= read -r line
do
echo "$line"
if [[ $line == z* ]];
then
sed -i "/$line/d" $inputFile
fi
done < "$inputFile"
i want to delete 'z-2.10.3.2 x/y/z F (&)[]+*' kind of lines, how can i do this...?
As you mentioned you don't need line which has z*
Simply use grep -v
grep -vE "^[[:blank:]]*z" file
I have created one scenario where I have a file which contains
root#ubuntu:~/T/e/s/t# cat file
hello world
sample line 1
sample line 2 world
sample line 3
sample line 4
In my case, I want to remove the line contains "world"
root#ubuntu:~/T/e/s/t# grep -v "world" file
sample line 1
sample line 3
sample line 4
If you want you can redirect your output in another file.

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).

Read a file line by line from last [duplicate]

This question already has answers here:
How can I reverse the order of lines in a file?
(24 answers)
Closed 6 years ago.
I have a requirement to read the file line by line from last line until the first line. Right now I am able to read the file line by line from start with the below piece of code.
while IFS= read line
do
#Logic here
done <"$Input_File"
Kindly help me out with a solution to read the file line by line from last line.
You can use tac to read the file from the last line until the first. Using your example you could do:
while IFS= read line
do
#Logic here
done <<<(tac "$Input_File")
See the manual page for tac (this may not be installed by default in your distribution but should be available using the package manager).
file="path/to/your/file.txt"
awk '{print NR ":" $0}' $file | sort -t: -k 1nr,1 | sed 's/^[0-9][0-9]*://'

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