-1

I have a text file that contain a lot of mess text.

I used grep to get all the text that contains the string prod like this

cat textfile | grep "<host>prod*"

The result

<host>prod-reverse-proxy01</host>
<host>prod-reverse-proxy01</host>
<host>prod-reverse-proxy01</host>

Continually, i used sed with the intention to remove all the "host" part

cat textfile | grep "<host>prod*" | sed "s/<host>//g"; "s/</host>//g"

But only the first "host" was removed.

prod-reverse-proxy01</host>
prod-reverse-proxy01</host>
prod-reverse-proxy01</host>

How can i remove the other "/host" part?

The One
  • 2,261
  • 6
  • 22
  • 38
  • 2
    Possible duplicate of [How to replace strings containing slashes with sed?](https://stackoverflow.com/q/16790793/608639), [What characters do I need to escape when using sed in a sh script?](https://unix.stackexchange.com/q/32907/56041), [Escape a string for a sed replace pattern](https://stackoverflow.com/q/407523/608639), [Which characters need to be escaped when using Bash?](https://stackoverflow.com/q/15783701/608639), etc. – jww Aug 23 '18 at 04:56
  • Thanks man. I tried using the \ before special character but it didn't work. – The One Aug 23 '18 at 04:58
  • This looks wrong: `sed "s///g"; "s///g"`. Pipe to another invocation of sed. Also see [Multiple replacements with one sed command](https://stackoverflow.com/q/19590980/608639), [Join multiple sed commands in one script for processing CSV file](https://unix.stackexchange.com/q/229234/56041), etc. – jww Aug 23 '18 at 05:00
  • 1
    Possible duplicate of [How to use different delimiters for sed substitute command?](https://stackoverflow.com/questions/5864146/how-to-use-different-delimiters-for-sed-substitute-command) – Sundeep Aug 23 '18 at 05:18
  • 1
    you could use single command... `sed -E '/prod/ s|?host>||g' ip.txt` or `sed -nE '/prod/ s|?host>||gp' ip.txt` depending on what you need... also, don't use double quotes unless you need it – Sundeep Aug 23 '18 at 05:21

2 Answers2

2
sed -n -e "s/^<host>\(.*\)<\/host>/\1/p" textfile

sed can process your file directly. No need to grep or cat.

-n is there to suppress any lines that do not match. Last 'p' in the script will print all matching files.

Script dissection:

s/.../.../... 

is the search/replace form. The bit between the first and the second '/' is what you search for. The bit between the second and third is what you replace it with. The last part is any commands you want to apply to the replacement.

Search:

^<host>\(.*\)<\/host>

finds all lines beginning with <host> followed by any text (.*) followed by </host>. Any text between <host> and </host> is stored into internal variable '1' using '(' and ')'. Note that (, ) and / (in </host>) have to be escaped.

Replace:

\1

Replace found text with contents of variable 1 (1 has to be escaped, otherwise, everything is replaced by character '1'.

Commands:

p

Print resulting line (after replacement).

Note: Your search involves removing two similar but not identical strings (<host> and </host>).

Dejan D.
  • 81
  • 5
  • 1
    Using '/' to delimit various parts of find/replace string is not mandatory. You can use any other character. E.g. `sed -n -e "s-^\(.*\)-\1-p" textfile` In this case you do not need to escape the '/' inside `` – Dejan D. Aug 23 '18 at 05:47
0

I think this sed is enough

sed 's/<[/]*host>//g' infile
ctac_
  • 2,413
  • 2
  • 7
  • 17