This question already has answers here:
How to delete from a text file, all lines that contain a specific string?
(21 answers)
Closed 6 years ago.
For example I have a caf.txt file and I want to delete a "donut" word in the document without entering the document on linux .How can I do it?
To delete just the word "donut"
sed -i 's/donut//g' caf.txt
To delete lines that contain the word "donut"
sed -i '/donut/d' caf.txt
What I do is:
sed '/text_to_delete/d' filename | sponge filename
This will make the change to the source file.
Related
This question already has answers here:
Rename multiple files while keeping the same extension on Linux
(4 answers)
Closed 3 years ago.
I have a scenario where I want to remove date from filename
Lets take an example 1 :
ABC_2019_06_12.txt
Lets take an example 2 :
ABCDEF_202012040120456.txt
using cut I cannot delete required text
how to cut to get the required below output like below
ABC.txt
ABCDEF.txt
One command which should work for all scenario which ever filename it is
My solution which I worked is to read the number of position and cut that part but I don't find it effective any other solution will be appreciated
In bash you can cut off the part starting with underscore:
$ filename=ABC_2019_06_12.txt
$ filename=${filename%%_*}
$ echo $filename
ABC
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).
This question already has answers here:
how to remove the first two columns in a file using shell (awk, sed, whatever)
(9 answers)
Closed 4 years ago.
From a text file, I want to remove the first 6 columns. I tried sed as follows, but I have to do it six times (one for each column). Is there any efficient way to do it (or pass the 6 columns at once for sed)?
sed -i -r 's/(\s+)?\S+//1' file
Thanks!
You could do this within regex and quantifying braces if a column consists of non-whitespace characters with optional leading spaces:
sed -i -r 's/^((\s+)?\S+){6} *//' file
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
This question already has answers here:
How do I use a new-line replacement in a BSD sed?
(4 answers)
Closed 7 years ago.
sed '/^;date.timezone =/!b;:a;n;//ba;i\date.timezone = Europe/London' /etc/php.ini
You can probably guess Im creating a script for setting up LAMP servers.
In the above example the text is not replaced but instead the changes are displayed on the console.
I my goal was to insert date.timezone = Europe/London the last occurence of ;date.timezone =
Etan Reisner:
You aren't using the -i flag to tell sed to modify in place.