Home > Net >  How to increase speed string processing in bash script?
How to increase speed string processing in bash script?

Time:03-01

I have string content as TXT file = 70 kb. I want to find and cut string with pattern.

Sample content.

"<div jsname="Rfh2Tc"  id="i3" role="alert"></div></div></div></div><div  role="listitem"><div jsmodel="CP1oW" data-params="%.@.[1696921474,&quot;พื้นที่&quot;,null,2,[[TARGET_STRING,[[&quot;BKK"

I use this code in bash script.

DATA=`cat content.txt`
DATA=${DATA##*พื้นที่&quot}
DATA=${DATA%\,\[\[*}
echo $DATA

It's work. But! When it run, It very slow for 60 KB text file. and high CPU usage. How can I fix it, Please help me.

CodePudding user response:

You could use sed:

DATA=$( sed 's/.*FrontPattern:\[\(.*\?\)\]:BackPattern.*/\1/' <<< "$DATA" )

For a 70 KiB text file, this takes less than 20 ms on my machine.

  • Related