-1

I am looking for logic where in I am trying to extract words from multiple lines using AWK command in Linux Tried in multiple ways with AWK and SED but no results.

Attached enter image description hereis the text file and expected output

  • 1
    Please, don't post images of text. Since it's a short text file, just copy and paste the text. – Kusalananda Jun 28 '18 at 12:14
  • I tried doing so, but it is skipping lot of characters in the file – user297460 Jun 28 '18 at 12:21
  • Well, that just means that we can't actually test our solutions against the data that you actually have, which is pretty bad. – Kusalananda Jun 28 '18 at 12:26
0

You can use this to get the desired output

$ echo "DEVICE-NAME STATUS" ; grep -e NAME -e STATUS <filename> | sed 's#NAME##g ; s#STATUS##g ;  s#<>## ; s#</>##' | xargs -L2 | column -t

Output:

DEVICE-NAME STATUS
METASYNC Online
DELTA OFFLINE
  • Thank you for your response, but I am getting below error awk: warning: escape sequence \*' treated as plain *' updated my query, please go through it once. – user297460 Jun 28 '18 at 11:41
  • I have updated my answer , please check if it works now – Arushix Jun 28 '18 at 12:09
  • Sorry, it is giving only DEVICE-NAME STATUS in output – user297460 Jun 28 '18 at 12:19
  • You are using your filename in $ echo "DEVICE-NAME STATUS" ; grep -e NAME -e STATUS <filename> | awk -F "\\\\*\\\\*" '{print $2}' | xargs -L2 echo ? right ?? if yes , please give output of $ echo "DEVICE-NAME STATUS" ; grep -e NAME -e STATUS <filename> – Arushix Jun 28 '18 at 12:21
  • yes I am using devicelog.txt output: <NAME>METASYNC</NAME> <STATUS>Online</STATUS> <NAME>DELTA</NAME> <STATUS>Online</STATUS> – user297460 Jun 28 '18 at 12:26
1

Using XMLStarlet:

xmlstarlet sel -t -m '//APPLIANCE' -v 'concat(NAME/text()," ",STATUS/text())' -nl file.xml

For the given data, this would produce

METASYNC Online
DELTA Offline

It does this by finding all APPLIANCE nodes in the XML document and concatenating the values of their NAME and STATUS sub-nodes with a space in-between. The -nl at the end inserts a newline after outputting the result for each APPLIANCE.

If you want column headers and nicer tabular format:

( echo 'Device_name Status'
  xmlstarlet ...as above... ) | column -t

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.