-1

I'm pretty terrible with regex and searching for patterns, so I don't really know where to start.

I need to find <td id="latest_button"> in test.txt and return the next 50 characters after it. It's for bash script, so other languages will just be a pain.

  • Are you trying to parse HTML with regexes? – choroba Apr 9 at 7:37
  • Does that potentially include linebreaks? (Technically, there's also lots of things that may cause you to not find matches, like whitespace or extra attributes in your td, which is what @choroba is hinting at.) – Ulrich Schwarz Apr 9 at 7:46
  • I said I don't really know where to start, but yes it is HTML – Dragon8oy Apr 9 at 7:47
  • Are you really interested in 50 characters or in 50 lines? – Panki Apr 9 at 10:05
  • Characters, it's one long string of tags and im interested in the tag after it, around 50 chars – Dragon8oy Apr 9 at 10:11
0

I dont know if it's the best idea to parse html like you are trying to, but anyways, this works, if you have linebreaks, if you dont, you can remove the tr part :

cat yourFile.html | tr -d '\n' | awk -F'<td id="latest_button">' '{print $2}' | cut -c -50

So, tr will get rid of the linebreaks, to treat everything as one line string.

awk -F sets the delimiter to the tag, print $2 , prints the part after the delimiter

and cut -c -50 prints only the first 50 chars

1

I typically use grep -o for this:

grep -o '<td id="latest_button">..................................................'

Or using extended mode:

grep -E -o '<td id="latest_button">.{50}'

Of course you also get the tag itself.

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.