linux - How to use sed to grep specifc values from a file -
i new using sed command , question might basic one.
i having log file , trying grep errors , extract required values error lines.
i facing problem when there more 9 pattern groups in sed. tried using sed -e have multiple sed not seem work.
commands tried
command #1:
grep error file | sed -n 's_.*key1=\([0-9]*\), key2=\([0-9]*\),.* key3=(.*\), key4=\([a-za-z0-9_]*\),.* key5=\([a-z0-9]*\),.*key6=\([a-za-z0-9]*\),.*key7=\([0-9]*\), key8=\(.*\), key9=\(.*\), key10=\(.*\)}.*_\1,\2,\3,\4,\5,\6,\7,\8,\9,\10_p'
in above output, number 10 being printed instead of value matching 10th pattern
command #2:
this awk command, did wanted though not able format proper comma separated values.
awk -f "\n" '/error/ && match($0,/key1=([0-9]*), key2=([0-9]*),.*key3=(.*),key4=([a-za-z0-9_]*),.*key5=([a-z]*).*key6=([a-z]*),.*key7=([a-z0-9]*),.*key8=([0-9]*).*key9=([a-za-z0-9]*)/,a) { (i=1; in a; i++) { printf "%s%s", a[i], (i<length(a) ? ofs : ors) } printf "\n" }' testfile
just use awk. should considering awk since you're using grep+sed. gnu awk 3rd arg match():
awk -v ofs=',' '/error/ && match($0,/key1=([0-9]*), key2=([0-9]*),.* key3=(.*), key4=([a-za-z0-9_]*),.* key5=([a-z0-9]*),.*key6=([a-za-z0-9]*),.*key7=([0-9]*), key8=(.*), key9=(.*), key10=(.*)}/,a) { (i=1; in a; i++) { printf "%s%s", a[i], (i<length(a) ? ofs : ors) } }' file
but there's simpler solution if posted concise, testable sample input , expected output.
Comments
Post a Comment