0

i've a issue that i mentioned at the title.

There are lines in my file like:

<Hi>
<Test>
<Test2>
</Test2>
</Test>
</Hi>

So i need to get "Hi" tag's whole content. But i don't have a gnu grep and can't install it i'm not root on the machine. I can use only :

Usage: grep -hblcnsviw pattern file . . .

So can't use -A and -B commands or other gnu grep commands, can you please show me a way out..

Thanks in advance..

Jotne
  • 40,548
  • 12
  • 51
  • 55
Sercan Ozdemir
  • 4,641
  • 3
  • 34
  • 64
  • This has been discussed already. Please see this: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – tvm Nov 21 '13 at 07:00
  • actually this's not the same..And also this post that you sent is very old 4 years changes everything... – Sercan Ozdemir Nov 21 '13 at 07:03

3 Answers3

1

Does your system have awk, if so, try:

awk '/<Hi>/,/<\/Hi>/' file
<Hi>
<Test>
<Test2>
</Test2>
</Test>
</Hi>

Or you could use:

awk '/<Hi>/{f=1} /<\/Hi>/ {f=0;print} f' file
Jotne
  • 40,548
  • 12
  • 51
  • 55
  • It gave me that: awk: record `00:00:00,022 DEBUG c...' too long record number 9 – Sercan Ozdemir Nov 21 '13 at 07:05
  • Test the second option I added. Both should work since you have tag `ubuntu`. What version of `ubuntu` are you on. – Jotne Nov 21 '13 at 07:08
  • Sorry that gave me error : awk: syntax error near line 1 awk: bailing out near line 1 – Sercan Ozdemir Nov 21 '13 at 07:09
  • Cam you give me the output of `cat /etc/*-release`. I am testing this on `Ubuntu 12.04.3 LTS` – Jotne Nov 21 '13 at 07:13
  • cat: cannot open /etc/*-release , i don't know exact version of Linux but i just learned that i'm not on a UBuntu computer it's a SunOS "uname -mrs" command says. I Checked here: http://www.cyberciti.biz/faq/find-linux-distribution-name-version-number/ to learn version and got that.. Sorry for wrong information.. Its a SUNOS – Sercan Ozdemir Nov 21 '13 at 07:18
1

What about the venerable sed?

sed -n '/<Hi>/,/<\/Hi>/p' file
chthonicdaemon
  • 19,180
  • 2
  • 52
  • 66
1

Using sed:

sed -n '/<Hi>/,/<\/Hi>/p' file

Perl one-liner..

perl -ne 'print if /<Hi>/ .. /<\/Hi>/' file
hwnd
  • 69,796
  • 4
  • 95
  • 132