Skip to main content
added 171 characters in body
Source Link
Sundeep
  • 12.2k
  • 3
  • 28
  • 73
$ s='-0.005 Tc 0.005 Tw [(T)-8.5(o)-3.2(p)-15.3(ik)]TJ'

$ # if you want to delete []TJ
$ echo "$s" | awk '{print gensub(/\[([^]]+)]TJ/, "\\1", "g")}'
-0.005 Tc 0.005 Tw (T)-8.5(o)-3.2(p)-15.3(ik)

$ # if you just want the portion inside []TJ
$ # GNU awk supports third argument for 'match' method
$ # which makes it easy to extract capture groups
$ echo "$s" | awk 'match($0, /\[([^]]+)]TJ/, a){s = a[1]; print s}'
(T)-8.5(o)-3.2(p)-15.3(ik)

GNU awk supports third argument for match method, which makes it easy to extract capture groups. The first element of array will have the entire match. Second element will contain portion matched by first group, third element will contain portion matched by second group and so on.

$ s='-0.005 Tc 0.005 Tw [(T)-8.5(o)-3.2(p)-15.3(ik)]TJ'

$ # if you want to delete []TJ
$ echo "$s" | awk '{print gensub(/\[([^]]+)]TJ/, "\\1", "g")}'
-0.005 Tc 0.005 Tw (T)-8.5(o)-3.2(p)-15.3(ik)

$ # if you just want the portion inside []TJ
$ # GNU awk supports third argument for 'match' method
$ # which makes it easy to extract capture groups
$ echo "$s" | awk 'match($0, /\[([^]]+)]TJ/, a){s = a[1]; print s}'
(T)-8.5(o)-3.2(p)-15.3(ik)
$ s='-0.005 Tc 0.005 Tw [(T)-8.5(o)-3.2(p)-15.3(ik)]TJ'

$ # if you want to delete []TJ
$ echo "$s" | awk '{print gensub(/\[([^]]+)]TJ/, "\\1", "g")}'
-0.005 Tc 0.005 Tw (T)-8.5(o)-3.2(p)-15.3(ik)

$ # if you just want the portion inside []TJ
$ echo "$s" | awk 'match($0, /\[([^]]+)]TJ/, a){s = a[1]; print s}'
(T)-8.5(o)-3.2(p)-15.3(ik)

GNU awk supports third argument for match method, which makes it easy to extract capture groups. The first element of array will have the entire match. Second element will contain portion matched by first group, third element will contain portion matched by second group and so on.

Source Link
Sundeep
  • 12.2k
  • 3
  • 28
  • 73

$ s='-0.005 Tc 0.005 Tw [(T)-8.5(o)-3.2(p)-15.3(ik)]TJ'

$ # if you want to delete []TJ
$ echo "$s" | awk '{print gensub(/\[([^]]+)]TJ/, "\\1", "g")}'
-0.005 Tc 0.005 Tw (T)-8.5(o)-3.2(p)-15.3(ik)

$ # if you just want the portion inside []TJ
$ # GNU awk supports third argument for 'match' method
$ # which makes it easy to extract capture groups
$ echo "$s" | awk 'match($0, /\[([^]]+)]TJ/, a){s = a[1]; print s}'
(T)-8.5(o)-3.2(p)-15.3(ik)