Find the answer to your Linux question:
Results 1 to 3 of 3
Hello, I'm trying to make a script file that has an if statement that would be true if the line that is being read starts with the word "crush". Then ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2013
    Posts
    4

    Bash: Trying to make an if statement


    Hello, I'm trying to make a script file that has an if statement that would be true if the line that is being read starts with the word "crush". Then if so, inside the if statement crush would be deleted with the sed function and the word apple would change to orange. I used find here because I'm trying to read in all files and read in all lines of each file.


    #here's some of the code I was thinking of:

    find . -type f -exec \

    sed -i -e 's/^crush//g' \
    -e 's/apple/orange/g' {} \;

  2. #2
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    211
    hello !

    Code:
    find -type f -exec sed -i '/^crush/{s/^crush//;s/apple/orange/g}' {} \;
    my bad: I was not totally awake, and so didn't understand correctly.
    sorry.

    block of commands between curly braces only applies to the lines matching the pattern (/^crush/).
    there is always only one beginning, so g is not needed.
    there may be many apple, so g is required.

  3. #3
    Just Joined!
    Join Date
    May 2013
    Posts
    4
    Oh wow, I tried this format with the semicolons and inside the brackets and it worked better than my format as it wasn't picking up the duplicates (even with g), but this works perfectly. Thanks so much!


    Quote Originally Posted by watael View Post
    hello !

    Code:
    find -type f -exec sed -i '/^crush/{s/^crush//;s/apple/orange/g}' {} \;
    my bad: I was not totally awake, and so didn't understand correctly.
    sorry.

    block of commands between curly braces only applies to the lines matching the pattern (/^crush/).
    there is always only one beginning, so g is not needed.
    there may be many apple, so g is required.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •