2

I have recently begun learning ed, however I've come to the point where I need to search for text. My first attempt was ed's / command, which will show occurrences, but doesn't include line numbers. Is this achievable in vanilla ed?

0

2 Answers 2

3

You can use the g + n (global + number commands):

$ ed -p 'ed> ' file ed> ,p tree apple lemon fruit not here pear apricot end ed> g/a/n 2 apple 7 pear 8 apricot 

Always check the documentation to see all commands available: POSIX Ed and GNU Ed.

0
2

You may use = to output the line number of a line in the editing buffer. To search with some expression RE and output the line number of the first hit, use

/RE/ = 

The n command outputs the current line prefixed by its line number. Applying it to some other line or range of lines is also possible:

/RE/ n 

To get the line numbers of all matching lines, combine = or n with the g command. Note however that since the default address of the = command is the last line of the editing buffer, not the current line, using = would output the wrong result with g unless we explicitly give it the . address. So use

g/RE/ .= 

to get only the line numbers of all lines matching RE.

Use

g/RE/ n 

to output all lines matching RE together with their line numbers (similar to grep -n 'RE' although formatted differently).

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.