4

I use ed in a bash script to search a file; the / command will display the content, which I don't want. I tried to redirect >/dev/null 2>&1 but that didn't work for me.

text file foo.txt:

a
b
c

bash script bar.sh:

ed -s foo.txt << EOF
/b/
EOF
> /dev/null 2>&1

result:

$ ./bar.sh
b

How can I stop ed printing the matched line b?

Community
  • 1
  • 1
redmark
  • 131
  • 8
  • 1
    What is it that you are actually trying to do? I mean, you can find a `b` using `grep`? – Mark Setchell Jun 01 '16 at 14:22
  • @lutz horn , search command of ed will print out what it found, i don't want it to print , i only need the address to do other things. – redmark Jun 01 '16 at 14:31
  • That was my question - what other things? There is probably a better tool. – Mark Setchell Jun 01 '16 at 15:11
  • yes , grep is better for search. in fact, i not only need search , but also edit the file , e.g. firstly locate the some specified string in file , then delete few lines after that, add some lines new ,finally save these changes back to the file. i know ed can do the job. do u have better suggestion, i appreciate – redmark Jun 02 '16 at 08:51
  • 1
    `awk` or `sed` will do that very easily. Questions are free. I suggest you ask another question, tag it with `awk` and `sed`, and say 1) what you want to find, b) how many lines you want to delete afterwards, and c) what you want to add. I am sure you will get good answers within 10 minutes... – Mark Setchell Jun 02 '16 at 08:54

2 Answers2

4

I think you mean this:

ed -s foo.txt > /dev/null <<eof
/b/
eof
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
1

You can use the ed commenting command /b/;# to move to a matched line without printing the result.

According to ed documentation

'(.,.)#'

Begins a comment; the rest of the line, up to a newline, is ignored. If a line address followed by a semicolon is given, then the current address is set to that address. Otherwise, the current address is unchanged.

Adam
  • 17,838
  • 32
  • 54