UNIX question on grep

Dear Sir,

I need to remove the word /klp/ in all the files present in the directories

can you tell me how to remove the word globally.

grep "/klp/" * -exec ls -l

Once the above command is exectubed I could see lot of files displayed.

This has a few issues already.

  • I do not believe that grep has a -exec option. This looks like the end of a find command.
  • Using the * to specify all files might hit problems if:-
    [list]
  • There are too many files and your expanded command exceeds the limit
  • There are special files, such as directories etc.
    [/list]
  • If the string you are looking for is /klp/ then you may need to consider that the / is a special character. You might need to search for \/klp\/ to get what you want.

I would use find to generate the list to work with and then for each file you find, you could use grep, sed or perhaps awk to make the edit depending how you want the edit to affect the file.

I would suggest initially that you should use this to select the files to work on:-

find $dir -type f -exec grep -l "/klp/" {} /dev/null \;

I would avoid running the ls -l as you will only have to extract the file again information later on.

The use of /dev/null causes grep to output the filename and when combined with the -l flag does not show you the actual matching lines.

From this list, you can then decide what to do next. Do you want to:-

  • Delete the whole record where the string is matched
  • Delete just the actual string matched
  • Overwrite the actual string matched (in case you have fixed width data)
  • Something else?

Can you elaborate?

Thanks, in advance,
Robin

thanks for your detailed explanation.

I would need to delete that particular word in all the files present in the one particual directories.

The particular word

 /klp/ 

is present in the directory

 /us/hub/ 

my request is to delete the particular matched string alone.

So if your input file contains the records:-

abc/def/hij/klp/001
zyx/wvu/tsr/klm/002

.... does that mean you output should be:-

  1. text abc/def/hij//001 zyx/wvu/tsr/klm/002
  2. text abc/def/hij001 zyx/wvu/tsr/klm/002
  3. text abc/def/hij 001 zyx/wvu/tsr/klm/002
  4. Something else?

I just want to be sure that I am/we are working on the right idea.

Robin

 string ="/klp//" 

everything is perfect. from your example only the

 / / 

slash also need to be removed.

only the particular string need to be deleted in all the files.

Can do this using for loop in directory

for file in `ls *files.txt`
do
newfile=$file"_new"
cat $file | sed 's/klp//g' >$newfile
done

To ramkumar15,

Which option is it then? Leaving blanks or not?

To kidSandy,

There is again the issue over the number of files that your command will be expanded to.

Robin

the sting

 /flp/ 

need to be deleted in all the files present in given directory.

 /us/hub/ 

---------- Post updated at 12:14 PM ---------- Previous update was at 09:30 AM ----------

only the particual string need to be deleted in all the files available in one particular directory.

Another proposal.
The idea is to avoid unnecessary copy-back.

cd /us/hub || exit
for f in *
do
 if [ -f "$f" ]
 then
  perl -pe '$r=1 if s|/klp/||g; END {exit 1 unless defined($r)}' "$f" >"$f.new" &&
  cp "$f.new" "$f"
  rm -f "$f.new"
 fi
done