Search This Blog

Wednesday, February 18, 2009

Insert, append and replace lines or text with SED

A file vik

sed '/keyword/ a\your-text-here' vik #insert your-text-here after every occurance of keyword
sed '5 a\your-text-here' vik #insert your-text-here at line 6
sed '5 i\your-text-here' vik #insert your-text-here at line 5
sed '/^keyword/ i\your-text-here' vik #insert your-text-here before keyword
sed '/^keyword/ c\your-text' vik #replace your keyword with your-text
sed '4 i\text1\ntext2' vik # this will insert two lines

1 comment:

  1. sed and awk inserting new line

    [root@vikas Desktop]# echo "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z" | awk 'BEGIN{n=1}{while(substr($0,n,16)){print substr($0,n,16);n+=16}}'
    A B C D E F G H
    I J K L M N O P
    Q R S T U V W X
    Y Z
    [root@vikas Desktop]# echo "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z" | sed -e "s/.\{16\}/&\n/g"
    A B C D E F G H
    I J K L M N O P
    Q R S T U V W X
    Y Z
    [root@vikas Desktop]#

    ReplyDelete