chrise
chrise

Reputation: 4253

dynamic editing in linux

I am fairly new to linux. What I am looking for is a way to edit a file without opening it, but by just editing grep output.

So if I do

grep find_me allMyfiles

and get the output

file_1: this is find_me line in file_1
file_2: this is find_me line in file_2

I would like to edit a certain line without having to open the file, so e.g. changing the first hit to

file_1: this is find_me line in file_1 including my edits

and having this change being saved in file_1

Upvotes: 0

Views: 96

Answers (2)

You did not define what "opening a file" means to you.

But something needs to do the open(2) syscall. (There is not way to read inside a file without using that open(2) syscall).

If you want to edit some file programmatically (inside your shell script), consider using the GNU ed, or GNU sed or GNU emacs (with emacs --batch or emacs --eval ....)

Perhaps GNU awk could be useful.

Reading Advanced Linux Programming and Advanced Bash Scripting should be helpful.

Upvotes: 1

Barmar
Barmar

Reputation: 782693

Use sed:

sed -i.bak '/find_me/s/$/ including my edits/' filename

Upvotes: 1

Related Questions