Simple find and replace with sed

后端 未结 3 698
独厮守ぢ
独厮守ぢ 2020-12-10 04:46

I\'m trying to replace a number that is in a couple different strings in a text file. Basically it would take the form of

tableNameNUMBER
carNUMBER
         


        
3条回答
  •  猫巷女王i
    2020-12-10 05:26

    Since you've stated you're new to this, I'm going to first answer some problems that you haven't asked.

    Your shebang is wrong

    The first bytes of your script should be #! (a hash and an exclamation mark; referred to as a shebang). You have #1 (hash-one), which is gibberish. This shebang line tells the system to use bash to interpret your file (i.e. your script) when executed. (More or less. Technically speaking, it's actually given to env, which finds bash before handing it over.)

    Once you've fixed up the shebang, set the permissions on the script to let the system know it's executable:

    $ chmod a+x test.sh
    

    Then run it like so:

    $ ./test.sh 123456
    

    As @gokcehan also commented, running your script with sh ... when you have a shebang is redundant, and isn't preferable for other reasons.


    As for what you were asking, you can easily test your regex replacement:

    $ echo tableNameNUMBER | sed "s/NUMBER/123456/"
    tableName123456
    

    And it seems to work just fine.

    Note: The preceding $ merely denotes that I typed it into my console and isn't part of the actual command.

提交回复
热议问题