12

I noticed many files in my directory, called "sedAbCdEf" or such.

  • Why does it create these files?
  • Do these have any value after a script has run?
  • Can I send these files to another location , e.g. /tmp/?

Update:

I checked the scripts until I found one which makes the files. Here is some sample code:

#!/bin/bash
a=1
b=`wc -l < ./file1.txt`
while [ $a -le $b ]; do
    for i in `sed -n "$a"p ./file1.txt`; do
        for j in `sed -n "$a"p ./file2.txt`; do
            sed -i "s/$i/\nZZ$jZZ\n/g" ./file3.txt
            c=`grep -c $j file3.txt`
            if [ "$c" -ge 1 ]
            then
                echo $j >> file4.txt
                echo "Replaced "$i" with "$j" "$c" times ("$a"/"$b")."
            fi
                echo $i" not found ("$a"/"$b")."
            a=`expr $a + 1`
        done
    done
done
Village
  • 22,513
  • 46
  • 122
  • 163
  • 1
    why do you think it is related to sed? except for the filename of course, – cctan Mar 23 '12 at 02:57
  • I suppose it could be made by something else, however, I find them after running BASH scripts and the BASH scripts contain `sed`. All of the filenames begin with "sed", followed by some random letters. – Village Mar 23 '12 at 03:01
  • 1
    can you post the bash script? I am looking for a part that says `blabla > sedAbCdEf`? – cctan Mar 23 '12 at 03:02
  • 1
    @NiklasB.: `sed -i` creates files, see [my answer](http://stackoverflow.com/a/9834294/4279) – jfs Mar 23 '12 at 05:10

3 Answers3

12
  • Why does it create these files?

sed -i "s/$i/\nZZ$jZZ\n/g" ./file3.txt

the -i option makes sed stores the stdout output into a temporary file.
After sed is done, it will rename this temp file to replace your original file3.txt.
If something is wrong when sed is running, these sedAbCdE temp files will be left there.

  • Do these have any value after a script has run?

Your old file is untouched. Usually no.

  • Can I send these files to another location , e.g. /tmp/?

Yes you can, see above.

Edit: see this for further reading.

Community
  • 1
  • 1
cctan
  • 2,015
  • 3
  • 19
  • 29
  • [The source code for GNU sed-4.2](http://stackoverflow.com/a/9834294/4279) as I understand it says that it is impossible to send these files to another location – jfs Mar 23 '12 at 09:22
  • @J.F.Sebastian I think Village meant after all the script has stopped running, can confirm OP? – cctan Mar 23 '12 at 10:35
5

If you use -i option (it means make changes inplace) sed writes to a temporary file and then renames it to your file. Thus if operation is aborted your file is left unchanged.

You can see which files are opened, renamed with strace:

$ strace -e open,rename sed -i 's/a/b/g' somefile

Note: somefile is opened as readonly.

It seems there is no way to override the backup directory. GNU sed always writes in the file's directory (±symlinks). From sed/execute.c:

if (follow_symlinks)
  input->in_file_name = follow_symlink (name);
else
  input->in_file_name = name;

/* get the base name */
tmpdir = ck_strdup(input->in_file_name);
if ((p = strrchr(tmpdir, '/')))
  *(p + 1) = 0;
else
  strcpy(tmpdir, ".");

Prefix sed is hardcoded:

output_file.fp = ck_mkstemp (&input->out_file_name, tmpdir, "sed");
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • The temporary file has to be created in the same directory as the file it'll eventually be renamed over, otherwise you can't be sure the rename will work. `sed` ought to be deleting its temporary files if it doesn't use them to replace the old files, though. – zwol May 03 '13 at 13:46
2

This may be that, since you have used too much sed actions, and in a looped pattern, sed may be making tmp files which are not removed properly.

Sed creates un-deleteable files in Windows

Take a look at this post, sed have such an issue to be reported before. The better way is to make a script that removes the files, or create a function that remove all files that deletes all files with name starting with sed, (^sed* )like thing.

Community
  • 1
  • 1
Xander
  • 1,652
  • 2
  • 15
  • 20