Jump to content
php.lv forumi

bash - regex string - result array


Recommended Posts

Posted

Hi,

 

man ir fails kura ir json strings.

 

Man vajaga izgrepot ara no {...},{...}... visus objektus kuros ir kaut kada veida teksts kuru mekleju, piemeram "Govs"

 

so, ja man teksts ir:

 

 

[{"a":123,"b":"riepa"},{"a":124,"b":"govs"},{"a":125,"b":"zivs"},{"a":126,"b":"lampa"},{"a":127,"b":"govs"},{"a":128,"b":"galds"}]

 

 

man rezultata jasanjem

0={"a":124,"b":"govs"}
1={"a":127,"b":"govs"}

 

kads ir kautko tadu darijis? centos visadi, ar awk, sed, grep .. laikam nemau tik labi.

 

Ideja ir skaidra +- 

 

\{(.*)govs(.*)\}
\{(.*)(\bGovs\b)(.*)\}
.. bet ka lai pareizi visu izdara ta lai izsaucot bash skriptu: script.sh MySearch es rezultata sanjemu pliku stringu ar visiem rezultatiem kuri matcoja
 
Paldies, ja kadam izddoas izdomat
Posted

Kautkā tā? Strings nāk no faila data

 

 

#!/bin/bash

m="govs"
p="\{[^\{]*$m[^\{\}]*\}"
c=0

for l in `cat data | grep -o "$p"`; do
        echo $c=$l
        c=$((c+1))
done

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...