1

Ten files located in a directory. Each file content has "apple" word. write a bash shell script to replace "apple" with "banana" in all ten files and Print the number of replacements for each file. Have tried in this way but dont know how to get number of replacement. can anyone help in this ? and sed is only modifying in display actual file not getting modified

#!/bin/bash 
for f in *.txt 
do sed 's/apple/banana/g' $f 
done
Bubbles
  • 97
  • 1
  • 1
  • 7
  • 3
    When you say a "shell script", do you mean bash, ksh, anything else? Perhaps add relevant tags. Also, what is the real problem you're trying to solve? Some context might be nice as this sounds like homework. – wmorrison365 Jun 22 '15 at 08:41
  • Do not use code in comments. Instead, [edit] your question showing it. – fedorqui Jun 22 '15 at 08:47
  • See also question [how-to-check-if-the-sed-command-replaced-some-string](http://stackoverflow.com/questions/15433058/how-to-check-if-the-sed-command-replaced-some-string) – wmorrison365 Jun 22 '15 at 10:01
  • 1
    Your question is not very clear. When you say 'Each file has "apple" word.' do you mean in the file name or in its contents? My gut tells me it's probably the content, but we need to be sure as each interpretation requires very different solutions – Frank Jun 22 '15 at 12:12
  • File content not file name – Bubbles Jun 22 '15 at 13:02

1 Answers1

1

Assuming you want to replace the word 'apple' with 'banana' (exact match) in the contents of the files and not on the names of the files (see my comment above) and that you are using the bash shell:

#!/bin/bash

COUNTER=0

    for file in *.txt ; do
        COUNTER=$(grep -o "\<apple\>" $file | wc -l)
        sed -i 's/\<apple\>/banana/g' $file
        echo "RESULT: $COUNTER replacements in file $file"
        let COUNTER=0
    done

exit 0;

Explanation:

  • To count the replacements for each file you can use grep with the -o flag to print all matches with the word you want replaced (each in a new line) and pipe the result to wc -l (which counts newlines). The result is stored in a variable which is printed on screen and then reset after each file is processed.
  • You must specify the -i flag if you want sed to actually preform the substitution in the files, otherwise the substitution will be performed on the standard output.
  • This script will only replace exact matches for the word 'apple'. Thus, the word 'apples', for instance, would not be replaced with 'bananas'. If you want a different behavior just remove the word delimiters around the word 'apple' (word delimiters are \< and \>).
fedorqui
  • 275,237
  • 103
  • 548
  • 598
Frank
  • 250
  • 3
  • 10
  • Any links suggestible for practising on "sed" and "awk" commands please post the links – Bubbles Jun 22 '15 at 14:25
  • 1
    @Bubbles If this worked for you, please upvote it and accept it by clicking the tick mark on the left. As for practising "sed" and "awk" commands, these are the best tutorials and best reference I could find aside from the documentation itself: http://www.grymoire.com/Unix/Sed.html http://www.grymoire.com/Unix/Awk.html – Frank Jun 22 '15 at 15:40