Let's sit back and see what happens...

----------  Forwarded Message  ----------

Subject: Prepending filename and line number to a match?
Date: Wed, 24 Jul 2002 16:49:25 +0100
From: Angus Leeming <[EMAIL PROTECTED]>
To: Sed Users <[EMAIL PROTECTED] >

I have (many) text files in which I want to find some special entries.

An example file would be lib/ui/default.ui:
-----------
    Menu "file"
        Item "New|N" "buffer-new"
        Item "New from Template...|T" "buffer-new-template"
        Item "Open...|O" "file-open"
    End
-----------

I am interested here in the first argument of Item, because I want to
translate them. Assume I have a regexp to find one of these entries

For example this extracts the relevant string:
sed -e 's/.*[iI]tem[^"]*\("[^"]*"\).*/msgid \1/p;d' < ${INPUT} > ${OUTPUT}

However, rather than end up with a file containing:
msgid "New|N"
msgid "New from Template...|T"
msgid "Open...|O"

I would like to prepend each extracted string with the filename and line
number it came from. Ie, output something like
--------------
#: lib/ui/default.ui:2
msgid "New|N"
msgstr ""

#: lib/ui/default.ui:3
msgid "New from Template...|T"
msgstr ""

#: lib/ui/default.ui:4
msgid "Open...|O"
msgstr ""
-----------------

So, the question is:
how do I get hold of this filename and line number info?
Is sed the right tool?

Best regards,
Angus

PS, needless to say, this has to work with vanilla sed because it's to be
used by an OS project and should work out-of-the-box...

A.

-------------------------------------------------------

-- 
Dr Angus Leeming
Dept of Bioengineering
Imperial College
London SW7 2BX
+44 (0)207 594 5186

Reply via email to