How to replace second pattern(dot) after pattern(comma) in bash

awk '(gsub)
awk replace string in file
sed replace regex
sed replace last occurrence in file
sed insert text after match
sed replace character
how can i replace text after a specific word using sed?
sed replace character in string

How do i replace second dot after comma.

this is the closest i could go

echo '0.592922148,0.821504176,1.174.129.731' | xargs -d ',' -n1 echo | sed 's/\([^\.]*\.[^\.]*\)\./\1/' | sed 's/\([^\.]*\.[^\.]*\)\./\1/'

Output :

0.592922148
0.821504176
1.174129731

Expected output :

0.592922148,0.821504176,1.174129731

50 `sed` Command Examples – Linux Hint, How do i replace second dot after comma. this is the closest i could go echo ' 0.592922148,0.821504176,1.174.129.731' | xargs -d ',' -n1 echo | sed 's/\([^\.]*\.[^\ .]� 11. Add string before and after the matching pattern using ‘\1’ The sequence of matching patterns of `sed` command is denoted by ‘\1’, ‘\2’ and so on. The following `sed` command will search the pattern, ‘Bash’ and if the pattern matches then it will be accessed by ‘\1′ in the part of replacing text.

While I think the sed solution is your best choice, since you have tagged your question with both sed and awk, an awk solution is fairly straight forward as well using split() and basic string concatenation. (just not nearly as short) For example you could do:

awk -v OFS=, -F, '{
    for (i=1; i<=NF; i++) {
        n=split ($i, a,".")
        if (n > 2) {
            s=a[1] "." a[2]
            for (j=3; j<=n; j++)
                s = s a[j]
            $i=s
        }
    }
}1'

Where you define the field separator and output field separators as ','. Then looping over each field, check the return of split(), splitting the field into an array on '.' into array a. If the resulting number of elements is greater than 2, then put your first two elements back together restoring the first '.' in the number, and then simply concatenate the remaining fields. The 1 at the end is the default "print record" to print the updated record.

Example Use/Output

$ echo '0.592922148,0.821504176,1.174.129.731' |
> awk -v OFS=, -F, '{
>     for (i=1; i<=NF; i++) {
>         n=split ($i, a,".")
>         if (n > 2) {
>             s=a[1] "." a[2]
>             for(j=3;j<=n;j++)
>                 s = s a[j]
>             $i=s
>         }
>     }
> }1'
0.592922148,0.821504176,1.174129731

Awk - replace one character only in a certain column, Replace the second occurrence only of a match on each line The following ` sed` command will search the pattern, 'Bash' and if the pattern matches then The following `sed` command will replace each newline by a comma in the file os .txt. Pattern to Replace – \=submatch(0) + 1 – gets the matched pattern and adds 1 to it. Flag – as there is no flag, by default it substitutes only the first occurrence. After executing the substitute statement the file will become like this, where you can add the 3rd Article.

Could you please try following.

echo '0.592922148,0.821504176,1.174.129.731' | 
awk '
BEGIN{
  FS=OFS=","
}
{
  for(i=1;i<=NF;i++){
    ind=index($i,".")
    if(ind){
      val1=substr($i,1,ind)
      val2=substr($i,ind+1)
      gsub(/\./,"",val2)
      $i=val1 val2
    }
  }
  val1=val2=""
}
1' 


Explanation: Adding explanation for above code.

echo '0.592922148,0.821504176,1.174.129.731' |   ##Printing values as per OP mentioned and using pipe to send its output as standard input for awk command.
awk '                                            ##Starting awk program from here.
BEGIN{                                           ##Starting BEGIN section of this program here.
  FS=OFS=","                                     ##Setting FS and OFS as comma for each line of Input_file here.
}                                                ##Closing BEGIN BLOCK here.
{
  for(i=1;i<=NF;i++){                            ##Starting a for loop to traverse through fields of line..
    ind=index($i,".")                            ##Checking index of DOT in current field and saving it into ind variable.
    if(ind){                                     ##Checking condition if variable  ind is NOT NULL.
      val1=substr($i,1,ind)                      ##Creating variable val1 from sub-string in current field from 1 to ind value.
      val2=substr($i,ind+1)                      ##Creating variable val2 from sub-string in current field from ind+1 value to till complete length of current field.
      gsub(/\./,"",val2)                         ##Globally substituting DOTs with NULL in val2 variable.
      $i=val1 val2                               ##Re-crearing current field with value of val1 val2.
    }                                            ##Closing BLOCK for if condition.
  }                                              ##Closing BLOCK for for loop.
  val1=val2=""                                   ##Nullifying val1 and val2 variables here.
}                                                ##Closing main code BLOCK here.
1'                                               ##Mentioning 1 will print edited/non-edited line.

sed, I want to replace all dots . in the second column with a comma , as I would with sed 's/\./\,/g' file how can I use sed or preferably awk to only apply this for the� So the substitution pattern just adds the last comma. Once the comma is inserted, the ta tells sed to go back to the beginning (label :a) and do it again. The second time, the comma that was just inserted is now a word boundary, so the pattern matches the three digits before the comma, 215, and inserts another comma before them. Let's make sure:

An awk verison:

echo '0.592922148,0.821504176,1.174.129.731' | awk  -F, '{for (i=1;i<=NF;i++) {sub(/\./,"#",$i);gsub(/\./,"",$i);sub(/#/,".",$i);print $i}}'
0.592922148
0.821504176
1.174129731

It splits the line inn to multiple fields by ,. Then replace first . to #. Then replace rest of . to nothing. Last replace # back to . and print it.

Edit

awk -F, '{for (i=1;i<=NF;i++) {sub(/\./,"#",$i);gsub(/\./,"",$i);sub(/#/,".",$i);a=a (i==1?"":",")$i}print a}' file
0.592922148,0.821504176,1.174129731

sed, a stream editor, If the comma is always at the end of the second to last line: Using awk and bash Negative lookahead asserts that there wouldn't be a comma after that is s DOTALL modifier which makes dot to match even newline characters also. time the pattern is seen. tac reverses the order of the lines in the file,� So I know how to replace a particular instance (say 3rd one) of a word in a line using sed based on the sed one-liners. However I would like to replace a particular instance of a word in the entire file. I tried searching on the Internet but did not find anything useful. For example, here is a file:

Unix Sed Tutorial: Advanced Sed Substitution Examples, For example, to replace all occurrences of ' hello ' to ' world ' in the file input.txt : By default, sed prints out the pattern space at the end of each cycle through the script Example: Add ' hello ' after the second line (-| indicates printed output lines): The following command prints lines in /etc/passwd which end with ' bash ':. how to remove the last string after "." character include the "." character itself. implementation can be with sed under Linux/Solaris operation system. example of IP address before change. 192.9.200.1 ( need to remove the .1 ) expected results. 192.9.200 other example. 100.2.2.101FFF expected results. 100.2.2

Inserting commas into numbers with sed (Shallow Thoughts), Linux Sed Examples - Advanced Find and Replace Operation As we discussed in our previous post, we can use the different delimiters II. Sed '&' Get Matched String. The precise part of an input line on replacement part will replace with / usr/bin which is matched pattern and Bash 101 Hacks eBook T he $ character is used for parameter expansion, arithmetic expansion and command substitution. You can use it for manipulating and expanding variables on demands without using external commands such as perl, python, sed or awk.

Replacing dot for comma - UNIX and Linux Forums, It would be easy enough to insert commas after every third number, but that doesn't I was familiar with sed's basic substitute command, s/from/to/. Well, the to pattern is easy: & matches whatever we just substituted (from), The second time, the comma that was just inserted is now a word boundary,� I tried to acheive this, but everytime I either delete only dots "." but not the chars after "." or I could only delete the last ".gz". I am not sure how to delete chars after "." dot from each of the string separated by a space. Also I don't want to use a loop instead I am looking for a single command. Can this be done through a single command?

Comments
  • By seeing your profile came to know that you have never selected an answer as correct one. IMHO whenever you post a question on SO wait for sometime and you see some answers try to select anyone of them as correct answer.
  • You can shorten to a single expression sed ':a; s/\(\.[^.,]*\)\./\1/; ta'
  • @DavidC.Rankin I always posted answers with ;s before, I decided to post one answer with -es, can't I? This is too preferential IMHO.
  • I loved this answer and UV'ed it. Looking at the separate expressions it just caught me that you could do it all in one. Both ways are fine.
  • @David afaik BSD sed doesn't let you separate labels and branching ops from other expressions with semicolons
  • @oguzismail thank you. That's good info. I don't have a BSD sed to test with, but I was unaware of that limitation.
  • Thank you for the answer, but i believe this is not my expected output.