Remove/replace html tags in bash

后端 未结 2 1589
梦毁少年i
梦毁少年i 2021-01-02 08:30

I have a file with lines that contain:

  • Some Text: More Text
  • I want to remove the html tags and r

    2条回答
    •  旧时难觅i
      2021-01-02 09:23

      If you strictly want to strip all HTML tags, but at the same time only replace the tag with a -, you can chain two simple sed commands with a pipe:

      cat your_file | sed 's||-|g' | sed 's|<[^>]*>||g' > stripped_file
      

      This will pass all the file's contents to the first sed command that will handle replacing the to a -. Then, the output of that will be piped to a sed that will replace all HTML tags with empty strings. The final output will be saved into the new file stripped_file.

      Using a similar method as the other answer from @Steve, you could also use sed's -e option to chain expressions into a single (non-piped command); by adding -i, you can also read-in and replace the contents of your original file without the need for cat, or a new file:

      sed -i -e 's||-|g' -e 's|<[^>]*>||g' your_file
      

      This will do the replacement just as the chained-command above, however this time it will directly replace the contents in the input file. To save to a new file instead, remove the -i and add > stripped_file to the end (or whatever file-name you choose).

    提交回复
    热议问题