Why does sed leave many files around?
- 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.
Change multiple files
Better yet:
for i in xa*; do
sed -i 's/asd/dfg/g' $i
done
because nobody knows how many files are there, and it's easy to break command line limits.
Here's what happens when there are too many files:
# grep -c aaa *
-bash: /bin/grep: Argument list too long
# for i in *; do grep -c aaa $i; done
0
... (output skipped)
#
Sed creates un-deleteable files in Windows
I have replicated your setup and I have the following observations.
- I dont think there is a problem in the loop. The simple command "C:\Program Files\gnuwin32\bin\sed.exe" -i "s/bad/good/g" . - creates the same set of temporary files.
- The files are indeed created by sed. sed creates these temporary files when the "in place" (-i) option is turned on. In the normal course, sed actually deletes the files (that is what happens in cygwin) using a call to the 'unlink' library. In case of gnuwin32, it looks like the 'unlink' fails. I have not been able to figure out why. I took a guess that maybe the unlink call is dependent on the gnuwin32 'coreutils' library and tried to download and install the coreutils library - no dice.
- If you remove the 'read-only' restriction in the parent folder before executing the sed command, you can delete the temporary files from windows command prompt. So that should give you some temporary respite.
I think we now have enough information to raise a bug report. If you agree, I think it may be a good idea to bring it to the notice of the good folks responsible for gnuwin32 and ask them for help.
Meanwhile, the following version cleans up its temporary file:
- https://github.com/mbuilov/sed-windows
Changing last line in many files in sed - sed only change in first
You might need to ensure there is a newline character at the end of the file. sed
implementations are typically picky about the meaning of $
. Consider e.g.
sed -i.bak -e '$a\' file
or even better if your shell allows it:
[ -n "$(tail -c1 file)" ] && printf '\n' >>file
Make single instance of `sed` with multiple filenames skip to next file
See if your version supports the -s
option:
$ seq 3 > three ; seq 10 1 13 > thirteen
$ sed -n '$p' three thirteen
13
$ sed -n '2p' three thirteen
2
$ sed -sn '$p' three thirteen
3
13
$ sed -sn '2p' three thirteen
2
11
From man sed
:
-s, --separate
consider files as separate rather than as a single continuous long stream.
When using the -i
option, GNU sed
uses -s
by default.
In case the -s
option is not available, here's an alternative with perl
:
$ perl -ne 'print if eof' three thirteen
3
13
sed edit file in place
The -i
option streams the edited content into a new file and then renames it behind the scenes, anyway.
Example:
sed -i 's/STRING_TO_REPLACE/STRING_TO_REPLACE_IT/g' filename
while on macOS you need:
sed -i '' 's/STRING_TO_REPLACE/STRING_TO_REPLACE_IT/g' filename
How can write the multiple files with sed from pattern matched
Here's one way, using GNU sed
:
sed -r 's/(\w+) (.*)/echo "\2" >> \1.txt/e' file
But why can't you use awk
. It really trivializes the problem:
awk '{ print $2, $3, $4 > $1 ".txt" }' file
Or if you have many columns:
awk '{ r=$1; sub($1 FS, ""); print > r ".txt" }' file
sed rewrite leaving file blank
This is normal as sed reads as it writes. The first write will truncate the file. This ends reading and leaves an empty file.
In some condition when a tool reads a buffer before writing it would work for small inputs. But this is nothing you dont want to depend on. so if the tool does not have a in-place overwrite option don't use it.
You can write to a temp outfile and rename it over the infile or rename the in-file (or open the in-file, then delete it) and then write to the expected location. Otherwise you have to ensure to read everything into memory.
sed -i
with and without extension does work the same. See for example https://robots.thoughtbot.com/sed-102-replace-in-place which describes the -i
variations.
Related Topics
How to Put All Command Arguments in One Variable
Selinux Prevented Httpd(Usr/Sbin/Httpd) Write Access to /Var/Www/HTML/Bookings/Templates_C
Start Tomcat from Eclipse in Port 80 in Ubuntu with Authbind
Splitting a File Using Awk on MAC Os X
Bash Concurrent Jobs Gets Stuck
Using Awk to Get a Specific String in Line
Make Uses "Cc" Instead of "Arm-None-Eabi-As"
Bash Script to Install Postgresql - Not Working
Spawn_Id: Spawn Id Exp6 Not Open
Host Multiple ASP.NET Core Web Application Under a Single Linux Server
Ssh Environment Variable for Sudo Access
Run Meteor as a Daemon Process
How to Add a Ppa Repository Using Ansible
./Configure-With-Boost No Such File or Directory
Using Sftp to Transfer Images from HTML Form to Remote Linux Server Using Perl/Cgi.Pm