1

I want to grep and print disabled string from below output

$ grep "s_icsmstatus" $CONTEXT_FILE
<oa_service_status oa_var="s_icsmstatus">disabled</oa_service_status>

I can use something like

$ grep "s_icsmstatus" $CONTEXT_FILE | awk -F ">" '{print $(NF-1)}' | awk -F "</" '{print $(NF-1)}'
disabled

But is there any alternative simple or straigt way to do this?

3 Answers 3

1

Use grep -o:

grep "s_icsmstatus" $CONTEXT_FILE | grep -o 'disabled' 

-o provides as output only the exact match for the provided pattern, rather than the entire line in which the match is made.

1

grep is not the best tool for parsing html tags, but with a little help of Perl syntax one can do the following:

$ grep -Po 's_icsmstatus">\K[^<]*' $CONTEXT_FILE
disabled

the essential part is \K which cuts away everything matched so far, and we print only rest, i.e. everything till the next <.

0

You could use the following, which is a bit more universal as it can grab any word between the XML tags... not just the word 'disabled':

grep "s_icsmstatus" test.txt | awk -F">" '{print $2}' | awk -F"<" '{print $1}'

Reference: http://code.scottshipp.com/2013/06/27/easily-extract-data-from-xml-using-grep-and-awk/

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .