[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
sed: getting at the current file name
From: |
Ralf Wildenhues |
Subject: |
sed: getting at the current file name |
Date: |
Sat, 14 Nov 2009 13:23:01 +0100 |
User-agent: |
Mutt/1.5.20 (2009-08-09) |
Hi Paolo, bug-gnu-utils,
for operating on several files, GNU sed provides --separate which is
great; but sed is missing a way to get at the file name of the currently
processed file. This would be very convenient for things such as:
# find some bug, transform the found places to a vim quickfix file
for f in $files; do
sed -n "/$pattern_or_complex_script/"' {
=
p
}' $f | sed "N; s/^/$f:/; s/\n/:/"
done > quickfix-file
vim -c 'cf quickfix-file'
where the script is too complex to be emulated by grep, or git grep.
With --separate and a way to print the current file name we could
replace the shell loop with two sed invocations. For example, if a
command `F' would print the file name of the currently open file or `-',
plus a newline, then one could use:
sed -ns "/$pattern_or_complex_script/"' {
F
=
p
}' $files | sed "N; N; s/\n/:/g" > quickfix-file
saving lots of processes.
Alternatively, F could operate on pattern space, say, append the file
name. Not sure if it should prepend a newline in this case.
Would you accept such an extension? Or tell me to use awk?
Currently, the only way I see to do something like this efficiently in
sed is with real weird shell hackery that in-band-signals the file name
into the sed script and also on the command line.
FWIW, this would help for example
<http://thread.gmane.org/gmane.comp.lib.gnulib.bugs/19448/focus=19451>.
Thanks,
Ralf
- sed: getting at the current file name,
Ralf Wildenhues <=