简体   繁体   中英

Grep Syntax Dilemna

I'm trying to use grep to copy out lines in a text file that match a certain pattern but, I'm running into some issues... I would like to grab the values in the "title=" container.

Code:

get_tmax=`grep '[0-9][0-9]°C' K0G7_ec_tmp`
echo "${get_tmax}" > K0G7_ec_tmp2

Text File Contents:

<p class="one" title="19&deg;C">19</p>
<p class="two" title="26&deg;C">26</p>

You can use grep -P with match reset \\K :

grep -ioP 'title="\K[^"]+' K0G7_ec_tmp
19&deg;C
26&deg;C

However take caution while parsing HTML file using shell utilities grep/awk/sed etc. Better to use dedicated HTML parser for this job.

grep is shorthand for g/re/p which is not exactly what you're trying to do so I'd look at sed for this:

$ sed 's/.*title="\([^"]*\).*/\1/' file
19&deg;C
26&deg;C

That will work with any sed version on any OS.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM