Linux Sed Command
Does Linux Have a Search & Replace Feature?
You can use theSubstitution sed 's/<oldstring>/<newstri ng>/g' <file>
Deletion
sed '<start>,<end>d' < file>
Let's start with a substitution example. If you want to change all occurrences of lamb to ham in the poem.txt file in the grep example, enter this:
sed 's/lamb/ham/g' poem.txt
Mary had a little ham
Mary fried a lot of spam
Jack ate a Spam sandwich
Jill had a ham
spamwich
In the quoted string, the "s" means substitute, and the "g" means make a global change. You can also leave off the "g" (to change only the first occurrence on each line) or specify a number instead (to change the first n occurrences on each line).
Now let's try an example involving deletion of lines. The values for start and end can be either a line number or a pattern to match. All lines from the start line to the end line are removed from the output. This example will delete starting at line 2, up to and including line 3:
sed '2,3d' poem.txt
Mary had a little lamb
Jill had a lamb spamwich
This example will delete starting at line 1, up to and including the next line containing Jack:
sed '1,/Jack/d' poem.txt
Jill had a lamb spamwich
The most common use of sed is to change one string of text to another string of text. But I should mention that the strings that sed uses for search and delete are actually regular expressions. This means you can use pattern matching, just as with grep. Although you'll probably never need to do anything like this, here's an example anyway. To change any occurrences of lamb at the end of a line to ham, and save the results in a new file, enter this:
sed 's/lamb$/ham/g' poem.txt > new.file
Since we directed output to a file, sed didn't print anything on the screen. If you look at the contents of new.file it will show these lines:
Mary had a little ham
Mary fried a lot of spam
Jack ate a Spam sandwich
Jill had a lamb spamwich
Use the man sed command for more information on using sed.
For more information on the sed command, see the sed manual.
Previous Lesson: Selecting Records
Next Lesson: Crunching Data
Comments - most recent first
(Please feel free to answer questions posted by others!)
So... my question is:
How do I take text at the beginning of a line and move it to the end?
example
make this line:
abiword</Program>:<Program icon="abiword_48.png" label=" ">exec
look like this:
<Program icon="abiword_48.png" label=" ">exec abiword</Program>
I think I should be able to get everything before the : and a(append) to the $(End of Line).
sed 's/mohit//g' try.txt try1.txt
sed -e 's/\/\*pattern string line\*\//\/\*pattern string line/g' <filename>
grep -i '*/' file1 | cut -d'/' -f2 | sed 's/*/ /g'
I'd like to change line in file.txt
/*pattern string line*/
to
pattern string line
command
sed -i "s/\/*patern.*/pattern string line/" file.txt
replace line
/*pattern string line*/
to
/*pattern string line
Any idea?
I found the mistake.
I forgot the complete path when I assigned the content of the Rul.inf File to Variable Rul.
Here the solution:
Rul=`/bin/cat /usr/mtcl/Scripts/Rul.inf`
String="/bin/sed "s/Normal/$Rul/g" /usr/mtcl/Scripts/template.mgr"
echo $String > /usr/mtcl/Scripts/sed.log
$String > /usr/mtcl/Scripts/RulName.mgr
Rul=`cat Rul.inf`
String="/bin/sed "s/Normal/$Rul/g" /usr/mtcl/Scripts/template.mgr"
echo $String > /usr/mtcl/Scripts/sed.log
$String > /usr/mtcl/Scripts/RulName.mgr
When I run the script manually it works
but when I run it with the crontab the sed comand dosen't use the content of $Rul
sed.log: after manually:
/bin/sed s/Normal/820/g /usr2/mtcl/Scripts/Vorlage.mgr
sed.log: after crontab:
/bin/sed s/Normal//g /usr2/mtcl/Scripts/Vorlage.mgr
Any idea what is wrong?
I have many rows, I'd like to delete the only zero values, example :
108.00
0.00
687809.00
0.00
0.00
6890.00
I only erase row number 2,4,5 using linux command
Thanks a lot
Sugeng
many thanks!
dan
So, it looks like sed could be very fast, but can be used only on fix pattern. a dynamic pattern search would be too complex for it.
find /home/marie/Fotos -name "*.jpg" -ls
or without the -name parameter:
find /home/marie/Fotos -ls
This will lisdt all files with relative paths included.
find . -exec find /home/marie/Fotos -name '{}' \;
But then I get the message, that Unix filenames do not contain slashes etc... and I see that the replaced names all begin with ./ as {} seems to give out the path and not only the file name.
I tried with grep but I do not seem to understand the syntax. Could you help?
Thanks
Marie
With that setup it fails to match anything.
sed -i "s/$user = '.*';/$user = '$DBUSER';/" configfile
I basicly need to be able to change what is inside of the single quotes. What happens is that every set of single quotes gets replaced through out the entire file. What am I missing here?
sed -i s/default/600dpi/ /etc/cups/ppd/prt1.ppd
Great site, keep up the fantastic work!
ex: sed -i "s\SRC\DEST\g" tools.txt
I want to replace a string having '/' to a string having '/'. Is there any possibility of using sed command?
For example,
sed -i "s/SRC/DEST/g" tools.txt
where SRC= D:/tools
DEST= D/tools/plain/test
I have Another Problem can anyone help me out...
I have two Files in this output is Coming like this-
1
2
3
4
5
6
And Also Second file have output like this-
10
9
8
7
6
We need to do sum taken from coloum to coloum with both sheet...
Like from first sheet we take 1 and from second sheet we take 10
and we need output like-
11
.
.
.
.
.
.and so on !!!!
i was trying with command line arguments in sed..
wanted to get the file, patterns as input string and wanted to replace the patterns in the file so my script was...
echo "enter file"
read file
echo "enter old pattern"
read old
echo "enter new pattern"
read new
f=$file
n=$new
o=$old
sed 's/$o/$n/g' $f
kindly help me out...
What is the output of "date -u +%W`uname`|sha256sum|sed 's/\W//g'"?
" <string>9999</string> " to " <string>8888</string> "
and also it will be great if I get option to get option to use a wild match like
" <string>*</string> " to " <string>8888</string> "
I want to add
2011 10 2356
to 48 txt files in linux without opening them
help me please
this should be really easy but I don't know what I am doing wrong
I have a file like this:
#PBS -N pore2_5_nc3_10
#PBS -l nodes=1:ppn=1
#PBS -l walltime=168:00:00
#PBS -S /bin/bash
I want to change the firs line as
#PBS -N pore2_5_nc3_20
can anyone use a variable string=20 and then use sed to get the job done
I have tried sed but I have two problems
1- sed print the result on STDOUT but doesnot change the file!
2-it replace 10 with $string instead of 20(the value of string)
Thanks in advance
Fatima
i need to do a simple task.
i have 2 files file1.txt and file2.txt
file1.txt has follwing content:
a11
b....
replace
d...
e...
replace
f....
g....
replace
hello
i woh
and file2.txt has:
1
2
3
i need the output as:
a11
b...
1
d...
e...
2
f...
g...
3
hello
i woh
i need to replace the 'replace' string with another file2.txt content with that order
thanks in advance..!!
Gedit treats a paragraph of many sentences as one line. Finding the capital letter at the first of every sentence is difficult when you have almost 130,000 words.
You suggested: sed 's/\. \([a-z]\)/. \u\1/g' 'test of novel.txt' >nov1sed.txt
This searched for a period at the end of a sentence and then the space.
I put apostrophes around a pseudo file and had to use > to direct sed to make a new file.
I found thirty such errors.
Thanks again Bob.
1) Delete tag <html> </html>
2) Replace xyz to empty (blank)
3) This should done at once
I have try lots of thing with sed but not succeed
E.g. If the a string HOME_PATH is not there in a config file, I want to add it. If it's there, based on need, I want to change the value.
Is there a way to achieve this using sed?
sed 's/^[a-z]/\u&/g' test.txt
This tells sed to look for lines that start with a lowercase letter and capitalize it.
I use gedit to write a novel. I'd like to find the occasional lower case letter starting a sentence and convert it to upper case. I believe gedit treats a paragraph of many sentences as one line that wraps. Thanks
Can you tell me how to search and delete lines using sed?
Thanx Bob it did the trick =)
have a good week
rgds
Marcus
TODAY=`date +%Y%m%d`
TODAY=$TODAY"01"
sed -i "s/2010.*/$TODAY; /g" pri.domain
$TTL 86400
@ IN SOA dns.domain.se. admin.domain.se. (
2010120502 ; serial, todays date + todays serial #
28800 ; refresh, seconds
7200 ; retry, seconds
i whant to change "20101205" to todays date and add 01 on the end of the date.
i try sed -i 's/2010*/"$TODAY; "/g' pri.*
but it dident work.
Best regards
Marcus
I have a file that have several colomn unsefully that there are several colomn usefull between
them.I want to delete them with useing
sed command but one time.
thank you
I wanted help on how to search for the end of a function and need to place two lines before the function ends. Is it possible using the sed command? Thanks
Input file has text like this.
A, B, C, D, (and so on).
Output file has to be like this.
A, B
A, C
A, D
How can I do this? I know I can use sed command for this but don't know how to implement. Need help. Thanks in advance.
Any idea what is wrong?
eg: Rollback, ROLLBACK, rollback
I have 200 files I want to modify in a batch
Source is html file
Need to create a new file for the changes
Want to delete the firat part of the file up to the first </tr> tag (This is 20 or so lines but can vary slightly)
Then insert the contents of a reference file (the same for all files) into the new target file. The number of lines does not match the number that are deleted though.
Hope you can help
Paul
framework/Keyboard.java: Color backGround = new Color (82, 113, 115); //106,131,147
framework/Keyboard.java: private Color fgc = new Color (57, 78, 79);
framework/Keyboard.java: this.setBackground ( new Color(106,131,147));
framework/UIConstants.java: = new Color (107, 134, 148);
framework/UIConstants.java: = new Color (90,97,115);
u should try this cmd:
cut -c 1-10 filname
this will cut first ten characters which r ur dates
jacklyn u should change ur stment vth above one
I am trying to search for a string in a file and replace it with a new value. The original string is input by the user and then replaced and saved to a new file:
echo "please enter the file name"
read sinname
echo "Please enter your fruit"
read fruit
NewFruit=banana
sed -e '/$fruit/s/$fruit/$NewFruit/g' $sinname > try.SIN
This does not change the old fruit value to the new fruit value!! please help!
I want to add '' in a file
Say : 9823,2,5,2010-10-15
want to change in
9823,2,5,'2010-10-15'
Pls help
Consider a file like
... <lots of veggies here>
cucumber apple carrot
banana apple tomato
potato apple banana
...<lots of veggies here too>
I want to change the "apple" in the line starting with banana to "onion", but not in the other lines.
sed -i '/21\/06/d' filename
with -i you can replace the content of your file ar else you can redirect the output of the command in a new filename (sed '/21\/06/d' filename > newfilename)
We have a large file it contqains more no of lines all the lines are starting with date.like below
21/06/2009,asdfasd,asfas,
21/06/2009,fdasfa,asfsda,
22/06/2009,sdfadsdsa,sfasd,
how to delete the line based on the dates which are appearing lines.
Any body can pull out from this...
thanks in advance
res=`sed -n '/string1/,/string2/p' file.xml`
sed -i "s/$res/newstring/g" file.xml
sed 's/\//\\/g'
Can you show me how to do it change // and / to \\ and \ together.
A bit late mabye, but I think your problem can be solved with the "dos2unix" command. I sugest you check it out. It replaces the "\r\n" (car return & new line) line terminators (used by windows programs) with "\n" (new line).
File V have the value Karunanithi
>cat T.sh
###---------------------
d=`date +%d%m%y`
echo $d
cat V | sed 's/nithi/'$d'/g'
cat V | sed 's:nithi:'$d':g'
###--------------------------
>sh T.sh
050710
Karuna050710
Karuna050710
Use
sed s:searchstring:"$var":g
The "/" in the var are causing problems. The idea is any single character after "s" in sed, ":" here, will be used as separator. I have changed the separator from "/" to ":".
var=31/03/2010
sed s/searchstring/"$var"/g
But didn't work.
I tried various combinations by trial and error, bit didn't get it.
Please suggest any suitable solution.
Its working.
you can use a '?' character as the separator between IS and BECOMEs on a substitution:
echo foo/bar | sed 's?/?BBB?'
fooBBBbar
Change the character / between foo and bar to 3 upper case 'B's... use the ? instead of / in your sed expression. (Same works in vi/vim etc)
sed 's/\//xyz'
P.S.
I'm the real Dennis not the one from above :)))
"C:\\", "E:\\", "VSS ASR DISK:\\",
"VSS SYSTEM FILESET:\\", "VSS OTHER:\\",
"VSS SYSTEM SERVICES:\\", "VSS SYSTEM BOOT:\\";
to continuous data with only one <return> at the end of each line... example
"C:\\", "E:\\", "VSS ASR DISK:\\", "VSS SYSTEM FILESET:\\", "VSS OTHER:\\","VSS SYSTEM SERVICES:\\", "VSS SYSTEM BOOT:\\";
I welcome your comments. However... I am puzzled by many people who say "Please send me the Linux tutorial." This website *is* your Linux Tutorial! Read everything here, learn all you can, ask questions if you like. But
don't ask me to send what you already have. :-) NO SPAM! If you post garbage, it will be deleted, and you will be banned.
Copyright © by -
All rights reserved - Redistribution is allowed only with permission.

