3 ms·
Does cut(1) work for you?
by bbrazil 10y ago
Does cut(1) work for you?
- peterburkimsher 10y agocut only supports single ASCII characters as delimiters. Reading a value from "<td>2017-01-09 <b>08:30</b></td>" is harder than it should be. cut needs single-character delimiters, so only splitting on "<" or ">" won't work. sed trips up on the "/", ":", and "-" without proper escaping. This is before even mentioning Unicode. High-level scripting languages handle all of these just fine. I'd much rather have a standard tool and library for this purpose though.
- protomikron 10y agoMaybe use awk? $ echo '<td>2017-01-09 <b>08:30</b></td>' | awk -F'[<>]' '{print $5}' This will give you '08:30'. Unicode seems to be also supported: $ echo 'λf. (λx. f (x x)) (λx. f (x x))' | awk -F'[λ.]' '{print $4}' This prints 'x'. For clarity some other matches: $1: '' $2: 'f' $3: ' (' $4: 'x' ...
- peterburkimsher 10y agoFine, until the next row doesn't have <b></b> tags. So the $5 no longer matches where it should. I'm just suggesting easier syntax, and a custom tool. Like dd is for disk copying, instead of cp/gzip/etc. A simple tool that will just work. This is the syntax I'm going for: textBetween("<td>", "</td>") | replace ("<b>", "") | replace ("</b>", "") Maybe with some magic strings, e.g. textBetween(":", "end")
- protomikron 10y agoYeah you are right, but if the input is more sophisticated I would probably use a scripting language and its full-fledged XML parser. Your replace part is already covered by sed (although I agree with you that the escaping can be awkward).
- lucb1e 10y agoThere's loads of things you can easily do with tools and I'm sure someone will come up with something, but sometimes it's just easier to use Python: cat dates | python -c 'import sys; for line in sys.stdin: print(line.split("<td>"))' Or PHP php -r 'foreach (explode("\n", file_get_contents("dates")) as $line) { print_r(explode("<td>", $line)); }' Or with Perl or Ruby or whatever floats your boat. While writing this I did think of something though, depending on what you're actually trying to do it might work: cat dates | tr "<>" " " | awk '{print $yourColumnOfInterest}'
- peterburkimsher 10y agoAs I say, "High-level scripting languages handle all of these just fine". Back to the article. dd is not a disk writing tool. Everything on UNIX is a disk writing tool. dd has one job, and does it right. Again, I propose a single-purpose text delimiter cutting program. It doesn't need to be a whole scripting language, e.g. awk, perl, python, etc. Just get the text between two strings, and have sensible syntax.