Skip to main content
extended solution
Source Link
RomanPerekhrest
  • 30.9k
  • 5
  • 47
  • 68

grep + awk solution:

grep -Eo '<[^<>]+>' input.xml | awk '{ gsub(/[<>]/,""); printf "%-3s - %s\n", NR, $0 }' 

The output:

1 - note 2 - to 3 - /to 4 - from 5 - /from 6 - heading 7 - /heading 8 - body 9 - /body 10 - /note 

Or with single GNU awk command:

awk -v FPAT='</?[^<>]+>' '{ for(i=1;i<=NF;i++) printf "%-3s - %s\n", ++c, $i }' input.xml 

grep + awk solution:

grep -Eo '<[^<>]+>' input.xml | awk '{ gsub(/[<>]/,""); printf "%-3s - %s\n", NR, $0 }' 

The output:

1 - note 2 - to 3 - /to 4 - from 5 - /from 6 - heading 7 - /heading 8 - body 9 - /body 10 - /note 

grep + awk solution:

grep -Eo '<[^<>]+>' input.xml | awk '{ gsub(/[<>]/,""); printf "%-3s - %s\n", NR, $0 }' 

The output:

1 - note 2 - to 3 - /to 4 - from 5 - /from 6 - heading 7 - /heading 8 - body 9 - /body 10 - /note 

Or with single GNU awk command:

awk -v FPAT='</?[^<>]+>' '{ for(i=1;i<=NF;i++) printf "%-3s - %s\n", ++c, $i }' input.xml 
Source Link
RomanPerekhrest
  • 30.9k
  • 5
  • 47
  • 68

grep + awk solution:

grep -Eo '<[^<>]+>' input.xml | awk '{ gsub(/[<>]/,""); printf "%-3s - %s\n", NR, $0 }' 

The output:

1 - note 2 - to 3 - /to 4 - from 5 - /from 6 - heading 7 - /heading 8 - body 9 - /body 10 - /note