Home > Blockchain >  Extract all words with a similar pattern from the same line and display them in a list
Extract all words with a similar pattern from the same line and display them in a list

Time:01-04

I have got an xml file that I have displayed using cat, and I have also piped through the grep command to extract the one line I am looking for.

The command looks like:

cat file_name.xml | grep -i pattern1

The output is one line long enclosed within xml brackets:

<list_of_names>ABC987599.com,ABC419192.com,ABC456321.com</list_of_names>

How do I manipulate this output so that I get the list of ABC addresses like so:

ABC987599.com

ABC419192.com

ABC456321.com

I have tried

cat file_name.xml | grep -i pattern1 | grep -Po 'ABC*com'

But all that gives me is

ABC

ABC

ABC

CodePudding user response:

What about a simple translation from comma to newline, like in this example:

echo "a,b,c" | tr "," "\n"
a
b
c

Have fun :-)

Edit:
Obviously, you need to remove your XML tags first, I propose sed for this:

echo "<list_of_names>ABC987599.com,ABC419192.com,ABC456321.com</list_of_names>" 
  | sed 's/<list_of_names>//g' 
  | sed 's/<\/list_of_names>//g' 
  | tr "," "\n"

(The whole command with the pipes is to be put on one line.)

For your information: when working with a / character in sed, it must be preceded by a backslash, hence <\/list....

  • Related