Wednesday, May 11, 2011

vi or sed - Delete till end of line from pattern

Here's the regex that will match everything from the pattern until a newline is found.

Say you want to remove everything after "here" from this sample file.

code here    where?
code here   right here!
code here         look every where.
Used regex
s/here.*//
Explanation:
.*  
dot matches any character including spaces, numbers, alphabets but not new line.
asterisk matches any number of occurrences of a character

s/here.*/meow/ will result in

code meow
code meow
code meow

Monday, May 9, 2011

Shell script for multiple SED edits

Following shell script
1.    Reads the names of the files to be edited from the file "filelist".
2.    Executes two sed statements on each of those files and
3.    Overwrites the original file. 


#!/usr/bin
while read line
do
sed 's/0512/0505/g' $line > temp
sed 's/05\/13/05\/06/g' temp > $line
done < filelist