Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run the sed command with input and output as the same file?

Tags:

sed

I'm trying to do use the sed command in a shell script where I want to remove lines that read STARTremoveThisComment and lines that read removeThisCommentEND.

I'm able to do it when I copy it to a new file using

sed 's/STARTremoveThisComment//' > test

But how do I do this by using the same file as input and output?

like image 944
Arvind Sridharan Avatar asked May 31 '12 07:05

Arvind Sridharan


People also ask

How do you save the output of sed in a file?

It sends all lines to standard ouput (typically the screen)—the lines that were modified as well as the lines that are unchanged. You have to capture this output in a new file if you want to save it. The redirection symbol “>” directs the output from sed to the file newfile.

How do I run multiple sed commands?

3.8 Multiple commands syntax There are several methods to specify multiple commands in a sed program. Using newlines is most natural when running a sed script from a file (using the -f option). The { , } , b , t , T , : commands can be separated with a semicolon (this is a non-portable GNU sed extension).

Can you use sed on multiple files?

Here is a quick command that you can use to search and replace a piece of text across multiple files using find, xargs, and sed.


3 Answers

sed -i (or the extended version, --in-place) will automate the process normally done with less advanced implementations, that of sending output to temporary file, then renaming that back to the original.

The -i is for in-place editing, and you can also provide a backup suffix for keeping a copy of the original:

sed -i.bak fileToChange
sed --in-place=.bak fileToChange

Both of those will keep the original file in fileToChange.bak.

Keep in mind that in-place editing may not be available in all sed implementations but it is in GNU sed which should be available on all variants of Linux, as per your tags.

If you're using a more primitive implementation, you can use something like:

cp oldfile oldfile.bak && sed 'whatever' oldfile >newfile && mv newfile oldfile
like image 199
paxdiablo Avatar answered Oct 16 '22 20:10

paxdiablo


You can use the flag -i for in-place editing and the -e for specifying normal script expression:

sed -i -e 's/pattern_to_search/text_to_replace/' file.txt

To delete lines that match a certain pattern you can use the simpler syntax. Notice the d flag:

sed -i '/pattern_to_search/d' file.txt
like image 13
Tiago Avatar answered Oct 16 '22 20:10

Tiago


You really should not use sed for that. This question seems to come up ridiculously often, and it seems very strange that it does since the general solution is so trivial. It seems bizarre that people want to know how to do it in sed, and in python, and in ruby, etc. If you want to have a filter operate on an input and overwrite it, use the following simple script:

#!/bin/sh -e
in=${1?No input file specified}
mv $in ${bak=.$in.bak}
shift
"$@" < $bak > $in

Put that in your path in an executable file name inline, and then the problem is solved in general. For example:

inline input-file sed -e s/foo/bar/g

Now, if you want to add logic to keep multiple backups, or if you have some options to change the backup naming scheme, or whatever, you fix it in one place. What's the command line option to get 1-up counters on the backup file when processing a file in-place with perl? What about with ruby? Is the option different for gnu-sed? How does awk handle it? The whole friggin' point of unix is that tools do one thing only. Handling logic for backup files is a second thing, and needs to be factored out. If you are implementing a tool, do not add logic to create backup files. Tell your users to use a 2nd tool for that. Integration is bad. Modularity is good. That is the unix way.

Notice that this script has several problems. The permissions/mode of the input file may be changed, for example. I'm sure there are innumerable other issues. However, by putting the backup logic in a wrapper script, you localize all of these issues and don't have to worry that sed overwrites the files and changes mode, while python keeps the file in place and does not change the inode (I made up those two cases, the point being that not all tools will use the same logic, while the wrapper script will.)

like image 4
William Pursell Avatar answered Oct 16 '22 20:10

William Pursell