0

Please, if you could incorporate your answer into a working sed line.

Solution:

sed -r "s/([^]].*)\[/\1\r[/g" testfile.txt

Solution that handles both sides of the bracket, and removes tabs

sed -r "s/\t//g;s/([^]].*)\[/\1\r[/g;s/\]([^[].*)$/]\r\1/g" testfile.txt

I have this gnuwin32 (working) sed line:

sed -r "s/(.*)\[/\1\r[/g" testfile.txt

What it does is

s/
(.*)\[  - matches anyString[
/
\1\r[   - splits anyString<INSERTSCARRIAGERETURN>[
/g

What I'd like to do is use a ^ (NOT Operand) to exclude anyString from being a "]"

so...

before is pretty much this

*[

becomes

*
[

however...

if * == ], I'd like to not perform the operation.

It was suggested I do something like ^\[

However I don't know where/how to implement the ^ operator on a variable stringMask such as (.*). Does .* being a string matter when I'm trying to text if * is/ends with ]?

I'm thinking either

(.*^\[)

or

(.*)^\[

any help would be appreciated.

Here's my sample data set

item_gloves

[OBJECT:ITEM]

###test###
    [ITEM_GLOVES:ITEM_GLOVES_GAUNTLETS]###test###
[NAME:gauntlet:gauntlets]
###test###[ARMORLEVEL:2]
[UPSTEP:1]
###test###[SHAPED]
[LAYER:ARMOR]###test######test###
[COVERAGE:100]
[LAYER_SIZE:20]
[LAYER_PERMIT:15]
[MATERIAL_SIZE:2]
[SCALED]
[BARRED]
[METAL]
[LEATHER]
[HARD]
thistleknot
  • 1,098
  • 16
  • 38

1 Answers1

2
sed -r "s/^([^]]*)\[/\1\r[/g" testfile.txt
Wrikken
  • 69,272
  • 8
  • 97
  • 136
  • that works. I don't suppose you could recommend how I would do the same with ]*? Something like sed -r "s/\]^([^[]*)/\]\r\n\1/g" testfile.txt – thistleknot Aug 18 '14 at 18:00
  • `"s/^([^[]*)\]/\1\r]/g"`? – Wrikken Aug 18 '14 at 18:04
  • alas, that was not it. However, I will try to analyze your solution, and see if I can figure it out. I just don't understand how the ^ operator works. I see it listed twice in the sed statement, something like \]^(*[^[])....sed -r "s/\]^(*[^[])/\]\r\n\1/g" testfile.txt – thistleknot Aug 18 '14 at 18:08
  • ]* to be split as ]NEWLINE*; but not if * == [, same problem, but with ]* vs original problem which was reversed – thistleknot Aug 18 '14 at 18:15
  • So, swap around: `s/^\]([^[]+)$/]\r\1/g` – Wrikken Aug 18 '14 at 18:17
  • You _do_ know BTW that `^` means _start of line_ anywhere outside `[]` character ranges? – Wrikken Aug 18 '14 at 18:18
  • idkw, but with the same data, I have some lines as ###comments###, that are to the right of ] that is not getting separated. – thistleknot Aug 18 '14 at 18:18
  • That's because I anchored it to start of line as per your first example (see? to little simple in- & output). Could you try around a bit on http://regex101.com/ & link an example of what you really want of in- & output, and your best effort in making that happen? – Wrikken Aug 18 '14 at 18:19
  • thanks that worked!: sed -r "s/\]([^[]+)$/]\r\n\1/g" testfile.txt... I updated the original post with source data. – thistleknot Aug 18 '14 at 18:20