0

I'm trying to replace all my spaces with commas to use my file as a CSV input, here is a sample input:

[Royal Gauntlets of Silvermoon] (1) Senhna 2500g
[Chestguard of the Vanquished Hero] (1) Neithia 3000g
[Chestguard of the Vanquished Hero] (1) Buddafly 3000g

and here is my expected output:

[Royal Gauntlets of Silvermoon],(1),Senhna,2500g,
[Chestguard of the Vanquished Hero],(1),Neithia,3000g,
[Chestguard of the Vanquished Hero],(1),Buddafly,3000g,

tr ' ' ',' <input >output works but replaces my spaces in the brackets as well

I know I can do this with awk but I'm unsure as to how exactly to do it. Thanks!

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
n0n0man
  • 13
  • 3
  • Check this out https://stackoverflow.com/questions/1271222/replace-whitespace-with-a-comma-in-a-text-file-in-linux/1271270 – XraySensei Jan 12 '22 at 17:07
  • 1
    Thanks @XraySensei however the specific part of not including the text in brackets is what I'm interested in solving. For instance `tr ' ' ',' output` works but replaces my spaces in the brackets as well – n0n0man Jan 12 '22 at 17:12
  • Always show your attempted code (which is required in a question) and any other relevant information in your question, not in a comment where it can't be formatted and could be missed. I copy/pasted your code and problem statement from your comment into your question this time. – Ed Morton Jan 12 '22 at 18:01

3 Answers3

2

Using GNU awk for FPAT:

$ awk -v FPAT='[^ ]*|[[][^]]+]' -v OFS=',' '{$1=$1}1' file
[Royal Gauntlets of Silvermoon],(1),Senhna,2500g
[Chestguard of the Vanquished Hero],(1),Neithia,3000g
[Chestguard of the Vanquished Hero],(1),Buddafly,3000g
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • 1
    In `[[][^]]+]` the first `[` is not a character to be matched and also not closed by the last `]`. First is has the character `]` inside `[]` resulting in `[]]`. Next you have a class without `]`, given by `[^]]`. At he end a " normal" character `]`. Difficult to read, but nice. – Walter A Jan 12 '22 at 19:51
  • `sed -r 's/([^ ]*|[[][^]]+]) /\1,/g' file` is another (worse?) method using your pattern. – Walter A Jan 12 '22 at 20:13
  • The solution has a problem with input like `arr[1 2 3]` or `FPAT='[^ ]*|[[][^]]+]'`. I don't care. – Walter A Jan 12 '22 at 22:18
  • @WalterA was that last comment addressed to me? It sounds like you're replying angrily to someone about something so I wasn't sure. If it's to me - sure, there's many inputs other than what the OP shows that any of the solutions won't work for. – Ed Morton Jan 13 '22 at 01:45
  • I tried to take away any angry sound with "I don't care". I was supprised that it took me a while to understand the regex, so I wanted to understand your solution better. Greedy matching the largest substring makes `arr[1 2 3]` fail, but that was not a requirement. I constructed an ugly `sed` solution for this (replace all spaces beteen the brackets with `\r`, replace all remaining spaces by `,` and replacing the `\r` to spaces again), but I did not post that solution: Your solution is clearly better and I had upvated it since it gave me new inspiration. Thanks. – Walter A Jan 13 '22 at 10:31
  • 1
    Ah, it was the "I don't care" that made me think it sounded angry, like you'd been having a conversation with someone and eventually gave up. Sorry, I just had an idiot from here follow me onto another platform and post insane, aggressive rantings there after they asked why their question here had been dowonvoted and closed and I explained to them it was off-topic so I'm probably a bit overly sensitive to tone right now! – Ed Morton Jan 13 '22 at 13:17
  • 1
    Thanks for the discussion and the solutions, both of them work great ! Sorry for replying a bit late, had gotten busy. – n0n0man Jan 20 '22 at 12:31
2

if the first field is the only square brackets, another solution

$ awk -F']' '{gsub(" ",",",$2); print $1 FS $2}' file

[Royal Gauntlets of Silvermoon],(1),Senhna,2500g
[Chestguard of the Vanquished Hero],(1),Neithia,3000g
[Chestguard of the Vanquished Hero],(1),Buddafly,3000g

separate the line at the close square bracket, replace single spaces with comma in the second part and join back.

karakfa
  • 66,216
  • 7
  • 41
  • 56
  • 1
    Thanks ! That was what I was looking to do initially, but I really get lost with awk syntax. Sorry for the late reply, thanks again for the solution ! – n0n0man Jan 20 '22 at 14:47
0

If sed is an option

sed 's/\(\[[^]]*]\|([^)]*)\|[a-z]*\) \|$/\1,/g' file
[Royal Gauntlets of Silvermoon],(1),Senhna,2500g,
[Chestguard of the Vanquished Hero],(1),Neithia,3000g,
[Chestguard of the Vanquished Hero],(1),Buddafly,3000g,
HatLess
  • 10,622
  • 5
  • 14
  • 32