-1

I have a text file, the content is made of custom tags :

<description>text</description><shortdescription>text for short description</shortdescription>

I want to extract in Linux command line the content of , which is "text for short description" and output its length if it is possible. Thank you.

Yvon Huynh
  • 453
  • 3
  • 16

1 Answers1

1

Assuming your file is a flat file and NOT an XML you can find the string within the tags using GNU grep which supports perl style regEx match with a the -P flag. The flags -o and -m1 are for returning only the matching part and return 1 instance of the match only.

grep -oPm1 "(?<=<shortdescription>)[^<]+" input-file
text for short description 

and for length of the string, store it in a variable the output of above command

stringLength="$(grep -oPm1 "(?<=<shortdescription>)[^<]+" file)"
echo "${#stringLength}"
26
Inian
  • 80,270
  • 14
  • 142
  • 161