-1

Ok, so I know there is a similar post that I referred to already but does not fit the exact issue I am having.

For reference: shell sed - substitute an unknown string between a known string and a generic delimiter replace a unknown string between two known strings with sed

file unknown-string,unknown-string,unknown-string

i need to add . to be like that:

file unknown-string,.unknown-string,.unknown-string

i tried sed -i 's/file.*,/file ,./g' file.txt not working!

NOTE: there are many , in the file so i can't use sed -i 's/,/,./'

Thanks.

Inian
  • 80,270
  • 14
  • 142
  • 161
test test
  • 77
  • 1
  • 11
  • 2
    `sed 's/,/,./g'`?? – David C. Rankin Aug 18 '20 at 03:18
  • @DavidC.Rankin, sorry i forget something, I added NOTE to the question, that NOTE: there are many , in the file so i can't use sed -i 's/,/,./g' – test test Aug 18 '20 at 03:21
  • Okay, that presents a problem. If you don't know what the text is and you can't key off the delimiters because you only want append a `'.'` after some of the delimiters -- that's not much to go on. Do you only want to append the `'.'` after the first and second commas? – David C. Rankin Aug 18 '20 at 03:23
  • @DavidC.Rankin, Yes – test test Aug 18 '20 at 03:24
  • `sed 's/,/,./; s/,/,./'` will work for you then? – Sundeep Aug 18 '20 at 03:28
  • Or `sed -e 's/,/,./1' -e 's/,/,./2'` or just `sed 's/,/,./1;s/,/,./2'` (same thing) – David C. Rankin Aug 18 '20 at 03:29
  • @Sundeep @DavidC.Rankin, As i said there are many of `,` in the file, if i tried to use your command, that will add dot to all `,` in the file, i want in this line and others like it when the `file` is known string and `unknown-string,` , i hope you understand me. – test test Aug 18 '20 at 03:34
  • 2
    `sed '/^file/s/,/,./1;s/,/,./2'` That will only place a `'.'` after the first two commas in lines that begin with `"file ..."` – David C. Rankin Aug 18 '20 at 03:43
  • @DavidC.Rankin, it's working, but the result is `file unknown-string,unknown-string,.unknown-string` you miss `.` after the first `,` – test test Aug 18 '20 at 03:47
  • 2
    I'm a bit confused, testing with `"file unknown-string,unknown-string,unknown-string,unknown-string"` results in `"file unknown-string,.unknown-string,.unknown-string,unknown-string"` -- I thought you only wanted the `'.'` after the first two commas? (make sure you are using the revised version with `.../1.../2` not `.../2 .../3`) – David C. Rankin Aug 18 '20 at 03:49
  • @DavidC.Rankin, Yes,i want `.` after the first two commas, but you miss the first comma! – test test Aug 18 '20 at 03:50
  • When i run your command the result will be: ```file unknown-string,unknown-string,.unknown-string``` – test test Aug 18 '20 at 03:52
  • there are no `.` after the first comma! – test test Aug 18 '20 at 03:53
  • @DavidC.Rankin, Sorry i missing something, now it's working, thank you very much for your help :) – test test Aug 18 '20 at 03:55
  • 1
    Glad it helped. Good luck with your scripting. – David C. Rankin Aug 18 '20 at 03:55
  • @DavidC.Rankin, If you can please post an answer so i can accept it and close this question? – test test Aug 18 '20 at 04:01

1 Answers1

2

After some difficulty understanding the format you were trying to match, to match a line format of:

file a,b,c,d....

and append a period '.' after the first two commas only for line beginning with file, you can use a sed expression of:

sed '/^file/s/,/,./1;s/,/,./2'

Explanation

  • /^file/ matches only lines beginning with the word "file",
  • s/,/,./1 is the general substitution form where the find expression matches a comma ',' and replaces it with ",." with the 1 ending the substitution specifying that the first-occurrence of the match is to be substituted,
  • s/,/,./2 is the same but with 2 specifying that the second-occurrence of the match be replaced.

Example Use/Output

Continuing with the format above, you could do:

$ echo "file a,b,c,d...." | sed '/^file/s/,/,./1;s/,/,./2'
file a,.b,.c,d....

Let me know if you have any further questions.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85