0

How do you break the regex for instructions into multiple lines with a script file?

I currently have more than 9 regex groups and want to still use sed in a script file. How does one break up the instructions?

Thanks

I would like to use sed -E -f thiscode.txt file.txt

s/([^ ]*) ([^ ]*) ([^,]*), *([^,]*), *([^,]*), *([^.]*), *([^,]*) *([^,]), ([^.]*), *([^,]*) *([^,]), ([^ ]*) *([^,]*) *([^.]*). ([^ ]*)./\2, \1, \3, ``\4," \6} \8,{\10} \6 \12/

Per a suggestion, I deleted /5 /7 and /9

sed -E 's/([^ ]*) ([^ ]*) ([^,]*), ([^,]), ([^.]), ([^,]), *([^,]) ([^,]), ([^ ]) ([^,]) ([^.]). ([^ ]*)./\2, \1, \3, ``\4," \5 \6, \7 \5 \9/'
userJoe
  • 73
  • 2
  • 12
  • I still want to use sed. This only tells me to use python or a different method. – userJoe May 01 '16 at 21:55
  • 1
    Share what you currently have. – Rudie May 01 '16 at 21:56
  • @Rudie I just did. And I know that it works when I break it apart. It's just combining them that's tricking me – userJoe May 01 '16 at 21:58
  • @user: you waste three backreferences (`\5`, `\7` and `\9`). – Cyrus May 01 '16 at 22:01
  • @Cyrus so does that mean I should just delete the grouping and decrement the back references appropriately? – userJoe May 01 '16 at 22:08
  • @userJoe: Correct. – Cyrus May 01 '16 at 22:12
  • @Cyrus why do you say that I'm wasting \5 \7\ 9 ? I deleted them and decremented and now it no longer works. What was wrong with those groupings? – userJoe May 01 '16 at 22:24
  • @userJoe: The important part was "appropriately". – Cyrus May 01 '16 at 22:33
  • @Cyprus, yeah my apologies- I'm kind of lost and trying to understand. I deleted the 5th, 7th, and 9th groupings and then decremented down to what I believe matches everything back up. sed -E 's/([^ ]*) ([^ ]*) ([^,]*), *([^,]*), *([^.]*), *([^,]), *([^,]*) *([^,]), ([^ ]*) *([^,]*) *([^.]*). ([^ ]*)./\2, \1, \3, ``\4," \5 \6, \7 \5 \9/' – userJoe May 01 '16 at 22:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/110776/discussion-between-userjoe-and-cyrus). – userJoe May 01 '16 at 22:45
  • If they are similiar regexes you could use labeled jumps to loop around and perform additional processing, look at the `b`, `:`, `t` commands. Otherwise, you can use multiple substitution commands. –  May 02 '16 at 00:59
  • 1
    The question seems unclear. Is the goal to make the code to take up less screen space, or column space, or something else? Must the resulting code run the same speed? What would typical input & output data look like? – agc May 02 '16 at 01:18
  • If you describe how you want your elements rearranged and what separates them, I'm pretty there will be a way to split up the command into multiple substitutions with less than 9 capture groups each, but it's very hard to tell from the question as it is now. – Benjamin W. May 02 '16 at 03:36

1 Answers1

1
sed '/very-long-pattern/ {
    H
    s/.......(ten)(eleven).../\1 \2 .../
    x
    s/(one)(two)...(nine).*/\1 \2 .../
    G
    s/\n//
}'

Basically, you capture the last 9 (<=) groups, and store them in the hold buffer, then capture the first 9 groups, and put them together, at last, remove the newline character introduced by G.

You can extend this script to support as many sed groups as you want.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
pdg
  • 103
  • 8