Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed command creates randomly named files

Tags:

shell

sed

I recently wrote a script that does a sed command, to replace all the occurrences of "string1" with "string2" in a file named "test.txt".

It looks like this:

sed -i 's/string1/string2/g' test.txt

The catch is, "string1" does not necessarily exist in test.txt.

I notice after executing a bunch of these sed commands, I get a number of empty files, left behind in the directory, with names that look like this:

"sed4l4DpD"

Does anyone know why this might be, and how I can correct it?

like image 553
Toast Avatar asked Oct 12 '11 00:10

Toast


People also ask

Does sed create a new file?

Sed provides “w” command to write the pattern space data to a new file. Sed creates or truncates the given filename before reads the first input line and it writes all the matches to a file without closing and re-opening the file.

What is the sed command designed for explain and provide an example?

The SED command in Linux stands for Stream EDitor and is helpful for a myriad of frequently needed operations in text files and streams. Sed helps in operations like selecting the text, substituting text, modifying an original file, adding lines to text, or deleting lines from the text.

What is $P in sed?

In sed, p prints the addressed line(s), while P prints only the first part (up to a newline character \n ) of the addressed line. If you have only one line in the buffer, p and P are the same thing, but logically p should be used.


2 Answers

-i is the suffix given to the new/output file. Also, you need -e for the command.
Here's how you use it:

sed -i '2' -e 's/string1/string2/g' test.txt

This will create a file called test.txt2 that is the backup of test.txt

To replace the file (instead of creating a new copy - called an "in-place" substitution), change the -i value to '' (ie blank):

sed -i '' -e 's/string1/string2/g' test.txt

EDIT II

Here's actual command line output from a Mac (Snow Leopard) that show that my modified answer (removed space from between the -i and the suffix) is correct.
NOTE: On a linux server, there must be no space between it -i and the suffix.

> echo "this is a test" > test.txt
> cat test.txt
this is a test
> sed -i '2' -e 's/a/a good/' test.txt 
> ls test*
test.txt    test.txt2
> cat test.txt
this is a good test
> cat test.txt2
this is a test
> sed -i '' -e 's/a/a really/' test.txt 
> ls test*
test.txt    test.txt2
> cat test.txt
this is a really good test
like image 88
Bohemian Avatar answered Nov 03 '22 22:11

Bohemian


I wasn't able to reproduce this with a quick test (using GNU sed 4.2.1) -- but strace did show sed creating a file called sedJd9Cuy and then renaming it to tmp (the file named on the command line).

It looks like something is going wrong after sed creates the temporary file and before it's able to rename it.

My best guess is that you've run out of room in the filesystem; you're able to create a new empty file, but unable to write to it.

What does df . say?

EDIT:

I still don't know what's causing the problem, but it shouldn't be too difficult to work around it.

Rather than

sed -i 's/string1/string2/g' test.txt

try something like this:

sed 's/string1/string2/g' test.txt > test.txt.$$ && mv -f test.txt.$$ test.txt

Something is going wrong with the way sed creates and then renames a text file to replace your original file. The above command uses sed as a simple input-output filter and creates and renames the temporary file separately.

like image 44
Keith Thompson Avatar answered Nov 03 '22 22:11

Keith Thompson