Live data from Hacker News

Sed(1) one-liners

sed.sourceforge.net

1–10 of 14 posts

Re: Sed(1) one-liners

#3
I don't see any use of the 'in-place' -i option.

My most useful one-liner:

        sed -i "s/pat/rep/g"   file
 
It means replace in-place all matches of pat with rep in file.

To make a backup of old file use -i.bak instead of -i where .bak would be the suffix of the backup file.

Re: Sed(1) one-liners

#4
post #3

I don't see any use of the 'in-place' -i option. My most useful one-liner: sed -i "s/pat/rep/g" file It means replace in-place all matches of pat with rep in file. To make a backup of old file use -i.bak instead of -i where .bak would be the suffix of the backup file.

Option -i is not defined in POSIX sed. That's probably the reason author didn't include it in his list.

I have heard that it's GNU sed option only. (But can't currently say if it's 100% true.)

Re: Sed(1) one-liners

#5
post #4
post #3

I don't see any use of the 'in-place' -i option. My most useful one-liner: sed -i "s/pat/rep/g" file It means replace in-place all matches of pat with rep in file. To make a backup of old file use -i.bak instead of -i where .bak would be the suffix of the backup file.

Option -i is not defined in POSIX sed. That's probably the reason author didn't include it in his list. I have heard that it's GNU sed option only. (But can't currently say if it's 100% true.)

There's no reason why this functionality should be in sed when it can be implemented on its own and re-used. IIRC there's an an example script to for that in _The Unix Programming Environment_. Better to use sudo than have everything implement its own sudo-like functionality, etc.

See "UNIX Style, or cat -v Considered Harmful" by Rob Pike (http://harmful.cat-v.org/cat-v/) for a longer discussion of this.

Re: Sed(1) one-liners

#6
post #4

Earlier quoted context omitted.

Option -i is not defined in POSIX sed. That's probably the reason author didn't include it in his list. I have heard that it's GNU sed option only. (But can't currently say if it's 100% true.)

There's no reason why this functionality should be in sed when it can be implemented on its own and re-used. IIRC there's an an example script to for that in _The Unix Programming Environment_. Better to use sudo than have everything implement its own sudo-like functionality, etc. See "UNIX Style, or cat -v Considered Harmful" by Rob Pike ( http://harmful.cat-v.org/cat-v/ ) for a longer discussion of this.

In place operations have been implemented as a standalone utility: sponge from moreutils.

    sed 's/blah/foo/' file | sponge file

Re: Sed(1) one-liners

#7
post #4
post #3

I don't see any use of the 'in-place' -i option. My most useful one-liner: sed -i "s/pat/rep/g" file It means replace in-place all matches of pat with rep in file. To make a backup of old file use -i.bak instead of -i where .bak would be the suffix of the backup file.

Option -i is not defined in POSIX sed. That's probably the reason author didn't include it in his list. I have heard that it's GNU sed option only. (But can't currently say if it's 100% true.)

Yes, thank you. Forgot to qualify that -i is only in GNU sed.

But I think it is in GNU sed for a reason -- it is a practical shortcut. The only thing I use sed for is to do search and replaces. If the command spanned 2 lines or had pipes and redirects a lot of people just wouldn't remember it.

Re: Sed(1) one-liners

#8
post #3

I don't see any use of the 'in-place' -i option. My most useful one-liner: sed -i "s/pat/rep/g" file It means replace in-place all matches of pat with rep in file. To make a backup of old file use -i.bak instead of -i where .bak would be the suffix of the backup file.

Equivalent perl one-liner (Perl regex may be more convenient):

   perl -e 's/pat/rep/g' -p -i file
-i.bak instead of -i works the same to create backups.

Re: Sed(1) one-liners

#9
post #4

Earlier quoted context omitted.

Option -i is not defined in POSIX sed. That's probably the reason author didn't include it in his list. I have heard that it's GNU sed option only. (But can't currently say if it's 100% true.)

There's no reason why this functionality should be in sed when it can be implemented on its own and re-used. IIRC there's an an example script to for that in _The Unix Programming Environment_. Better to use sudo than have everything implement its own sudo-like functionality, etc. See "UNIX Style, or cat -v Considered Harmful" by Rob Pike ( http://harmful.cat-v.org/cat-v/ ) for a longer discussion of this.

I checked - it's pgs. 152-156. They call their utility "overwrite".

"...Many other commands could also use a -o command. For example, sed could edit a file in place: ... It would be impractical to modify all such commands to add the option. Furthermore, it would be bad design: it is better to centralize functions, as the shell does with the > operator."

Re: Sed(1) one-liners

#10
post #7
post #4

Earlier quoted context omitted.

Option -i is not defined in POSIX sed. That's probably the reason author didn't include it in his list. I have heard that it's GNU sed option only. (But can't currently say if it's 100% true.)

Yes, thank you. Forgot to qualify that -i is only in GNU sed. But I think it is in GNU sed for a reason -- it is a practical shortcut. The only thing I use sed for is to do search and replaces. If the command spanned 2 lines or had pipes and redirects a lot of people just wouldn't remember it.

I agree. It's very practical.
Post reply on HN