Monday, November 15, 2010

sed -i option

We can use the -i option in sed to edit the file within the same command rather than directing it to another file and then renaming/moving it...

Code:
[ahamed@ahamed ~]$ cat textfile
# Check this line
The quick brown ajith jumps over the lazy dog

[ahamed@ahamed ~]$ sed -i '2 i \# Wow! I am the new guy' textfile

[ahamed@ahamed ~]$ cat textfile
# Check this line
# Wow! I am the new guy
The quick brown ajith jumps over the lazy dog

As you can see, after the -i option, the number is the line number to be inserted. The rest is the usual sed syntax. I'll break it up

sed - The command
i - For insertion
2 - Line Number
\#... - The line to be inserted
textfile - file being edited

If you don't use the option "-i", the file will not be edited. The changes will be displayed on the standard display.
Check this...

Code:
[ahamed@ahamed ~]$ cat textfile
# Check this line
# Wow! I am the new guy
The quick brown ajith jumps over the lazy dog

[ahamed@ahamed ~]$ sed '2 i # Wow! I am the NEXT new guy' textfile
# Check this line
# Wow! I am the NEXT new guy
# Wow! I am the new guy
The quick brown ajith jumps over the lazy dog

[ahamed@ahamed ~]$ cat textfile
# Check this line
# Wow! I am the new guy
The quick brown ajith jumps over the lazy dog

Good Day...

No comments:

Post a Comment