pattern matching

hi ,

i am trying to search for a pattern in the file and then replace the pattern.

please guide me

Thanks,
Mahi

man grep
man sed

hi pludi,

i tried with sed cmd

sed 's/ABC/abc/g' $file1

but i can see the change only on cmd prompt but the in the file ABC is not replaced with abc.

u need to redirect it to anoter file, u cannot directly make changes in original file. U can use replace command in VI editor if you want...

Ex: sed s/ABC/abc/g $file >$file_tmp

U can use replace command in VI editor if you want...

syntax: :%s/ABC/abc/g (in vi editor)

sed tries to be non-destructive, so it will keep your input untouched and print any changes to STDOUT. If you want to change a file inplace, try

cp ${file1} ${file1}.orig
sed -e 's/ABC/abc/g' ${file1}.orig > ${file1}
rm ${file1}.orig

thanks for the help ..
but i have a small problem here
actually i need to create a new file with a name similar to the old file.

i.e. if the old file is abc.txt , the new file should be abc_1.txt
i should get the substring of the file name and then name the new one
please let me know how to do it

Thanks in Advance

So you don't want to change the content of the file, but the filename. I can give you 2 options for that:

  1. If all files have the same extension use basename ${file} <ext> (e.g. basename ${file} '.txt'), which will give you the filename without the extension
  2. If they use different extensions use echo ${file} | sed -e 's/\(.*\)\.\(.*$\)/\1_1.\2/', which will append '_1' to the filename

i tried with ur solutions but i am unable to get it

if my file name is in $file variable and i need the file name with out the extension in one variable and the extension in another variable.
how do i do it

please help me ..