How to find an entire sentences in a csv file and replace it with sentences from another file using bash?











up vote
-2
down vote

favorite












So I have two files file1 and file2:



file1:
my name is xyz.
my name is abc.
I am a doctor.
I am an engineer.
I like dogs.
I like cats.


I want to replace some of these sentences by shorter sentences. So I have created another file named file2.csv



file2.csv:
"my name is xyz.","name xyz"
"my name is abc.","name abc"
"I am a doctor.","doctor"
"I like dogs.","dogs"


I have used sed so far and if input all these lines individually in the sed command they work perfectly however the the contents of file1 and file2 may change according to my needs and i want a solution that doesn't need changing the script or the code.
Something like creating a 2 dimensional array and and then checking if the value in the first column of file2 exists in file1 and then replacing it with the corresponding entry in the second column of file2.csv



So after I run the shell script file 1 should look like:



name xyz.
name abc.
doctor.
I am an engineer.
dogs.
I like cats.


Note that the contents in file1 and file 2 can change or new entries can be added and hence using something like



sed -i 's/I like dogs/dogs/' file1.csv


is not feasible.










share|improve this question
























  • Why not creating a simple script to do the task?
    – NiVeR
    Nov 7 at 18:36










  • yes I am trying to do that however I am stuck . I am finding it difficult to assign entire sentences to a variable or an array.When using awk it treats each word as a new column,that is undesirable,I want the entire sentence to be treated as one column and the next sentence as the second column.
    – Anuj Kulkarni
    Nov 7 at 18:40












  • Sounds like a homework question... How do I ask homework questions on Stack Overflow. You are expected to make an effort.
    – jww
    Nov 8 at 4:32










  • Consider converting file 2 to a set of sed commands and then run that
    – Thorbjørn Ravn Andersen
    Nov 9 at 12:34










  • Consider using TAB-separated files instead. They are much simpler to parse.
    – Thorbjørn Ravn Andersen
    Nov 9 at 12:50















up vote
-2
down vote

favorite












So I have two files file1 and file2:



file1:
my name is xyz.
my name is abc.
I am a doctor.
I am an engineer.
I like dogs.
I like cats.


I want to replace some of these sentences by shorter sentences. So I have created another file named file2.csv



file2.csv:
"my name is xyz.","name xyz"
"my name is abc.","name abc"
"I am a doctor.","doctor"
"I like dogs.","dogs"


I have used sed so far and if input all these lines individually in the sed command they work perfectly however the the contents of file1 and file2 may change according to my needs and i want a solution that doesn't need changing the script or the code.
Something like creating a 2 dimensional array and and then checking if the value in the first column of file2 exists in file1 and then replacing it with the corresponding entry in the second column of file2.csv



So after I run the shell script file 1 should look like:



name xyz.
name abc.
doctor.
I am an engineer.
dogs.
I like cats.


Note that the contents in file1 and file 2 can change or new entries can be added and hence using something like



sed -i 's/I like dogs/dogs/' file1.csv


is not feasible.










share|improve this question
























  • Why not creating a simple script to do the task?
    – NiVeR
    Nov 7 at 18:36










  • yes I am trying to do that however I am stuck . I am finding it difficult to assign entire sentences to a variable or an array.When using awk it treats each word as a new column,that is undesirable,I want the entire sentence to be treated as one column and the next sentence as the second column.
    – Anuj Kulkarni
    Nov 7 at 18:40












  • Sounds like a homework question... How do I ask homework questions on Stack Overflow. You are expected to make an effort.
    – jww
    Nov 8 at 4:32










  • Consider converting file 2 to a set of sed commands and then run that
    – Thorbjørn Ravn Andersen
    Nov 9 at 12:34










  • Consider using TAB-separated files instead. They are much simpler to parse.
    – Thorbjørn Ravn Andersen
    Nov 9 at 12:50













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











So I have two files file1 and file2:



file1:
my name is xyz.
my name is abc.
I am a doctor.
I am an engineer.
I like dogs.
I like cats.


I want to replace some of these sentences by shorter sentences. So I have created another file named file2.csv



file2.csv:
"my name is xyz.","name xyz"
"my name is abc.","name abc"
"I am a doctor.","doctor"
"I like dogs.","dogs"


I have used sed so far and if input all these lines individually in the sed command they work perfectly however the the contents of file1 and file2 may change according to my needs and i want a solution that doesn't need changing the script or the code.
Something like creating a 2 dimensional array and and then checking if the value in the first column of file2 exists in file1 and then replacing it with the corresponding entry in the second column of file2.csv



So after I run the shell script file 1 should look like:



name xyz.
name abc.
doctor.
I am an engineer.
dogs.
I like cats.


Note that the contents in file1 and file 2 can change or new entries can be added and hence using something like



sed -i 's/I like dogs/dogs/' file1.csv


is not feasible.










share|improve this question















So I have two files file1 and file2:



file1:
my name is xyz.
my name is abc.
I am a doctor.
I am an engineer.
I like dogs.
I like cats.


I want to replace some of these sentences by shorter sentences. So I have created another file named file2.csv



file2.csv:
"my name is xyz.","name xyz"
"my name is abc.","name abc"
"I am a doctor.","doctor"
"I like dogs.","dogs"


I have used sed so far and if input all these lines individually in the sed command they work perfectly however the the contents of file1 and file2 may change according to my needs and i want a solution that doesn't need changing the script or the code.
Something like creating a 2 dimensional array and and then checking if the value in the first column of file2 exists in file1 and then replacing it with the corresponding entry in the second column of file2.csv



So after I run the shell script file 1 should look like:



name xyz.
name abc.
doctor.
I am an engineer.
dogs.
I like cats.


Note that the contents in file1 and file 2 can change or new entries can be added and hence using something like



sed -i 's/I like dogs/dogs/' file1.csv


is not feasible.







linux bash shell awk sed






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 at 19:03









Cyrus

44.4k43375




44.4k43375










asked Nov 7 at 18:35









Anuj Kulkarni

145




145












  • Why not creating a simple script to do the task?
    – NiVeR
    Nov 7 at 18:36










  • yes I am trying to do that however I am stuck . I am finding it difficult to assign entire sentences to a variable or an array.When using awk it treats each word as a new column,that is undesirable,I want the entire sentence to be treated as one column and the next sentence as the second column.
    – Anuj Kulkarni
    Nov 7 at 18:40












  • Sounds like a homework question... How do I ask homework questions on Stack Overflow. You are expected to make an effort.
    – jww
    Nov 8 at 4:32










  • Consider converting file 2 to a set of sed commands and then run that
    – Thorbjørn Ravn Andersen
    Nov 9 at 12:34










  • Consider using TAB-separated files instead. They are much simpler to parse.
    – Thorbjørn Ravn Andersen
    Nov 9 at 12:50


















  • Why not creating a simple script to do the task?
    – NiVeR
    Nov 7 at 18:36










  • yes I am trying to do that however I am stuck . I am finding it difficult to assign entire sentences to a variable or an array.When using awk it treats each word as a new column,that is undesirable,I want the entire sentence to be treated as one column and the next sentence as the second column.
    – Anuj Kulkarni
    Nov 7 at 18:40












  • Sounds like a homework question... How do I ask homework questions on Stack Overflow. You are expected to make an effort.
    – jww
    Nov 8 at 4:32










  • Consider converting file 2 to a set of sed commands and then run that
    – Thorbjørn Ravn Andersen
    Nov 9 at 12:34










  • Consider using TAB-separated files instead. They are much simpler to parse.
    – Thorbjørn Ravn Andersen
    Nov 9 at 12:50
















Why not creating a simple script to do the task?
– NiVeR
Nov 7 at 18:36




Why not creating a simple script to do the task?
– NiVeR
Nov 7 at 18:36












yes I am trying to do that however I am stuck . I am finding it difficult to assign entire sentences to a variable or an array.When using awk it treats each word as a new column,that is undesirable,I want the entire sentence to be treated as one column and the next sentence as the second column.
– Anuj Kulkarni
Nov 7 at 18:40






yes I am trying to do that however I am stuck . I am finding it difficult to assign entire sentences to a variable or an array.When using awk it treats each word as a new column,that is undesirable,I want the entire sentence to be treated as one column and the next sentence as the second column.
– Anuj Kulkarni
Nov 7 at 18:40














Sounds like a homework question... How do I ask homework questions on Stack Overflow. You are expected to make an effort.
– jww
Nov 8 at 4:32




Sounds like a homework question... How do I ask homework questions on Stack Overflow. You are expected to make an effort.
– jww
Nov 8 at 4:32












Consider converting file 2 to a set of sed commands and then run that
– Thorbjørn Ravn Andersen
Nov 9 at 12:34




Consider converting file 2 to a set of sed commands and then run that
– Thorbjørn Ravn Andersen
Nov 9 at 12:34












Consider using TAB-separated files instead. They are much simpler to parse.
– Thorbjørn Ravn Andersen
Nov 9 at 12:50




Consider using TAB-separated files instead. They are much simpler to parse.
– Thorbjørn Ravn Andersen
Nov 9 at 12:50












4 Answers
4






active

oldest

votes

















up vote
0
down vote



accepted










Using awk



awk -F'"(,")?' '
NR==FNR { r[$2] = $3; next }
{ for (n in r) gsub(n, r[n]) } 1' file2.csv file1




  • -F'"(,")?' is the field separator, matches a " or ",", so that we don't need to remove double quotes from fields,


  • NR==FNR { r[$2] = $3; next } populates an array with content of file2.csv using the full sentence as key and replacement string as value,


  • { for (n in r) gsub(n, r[n]) } 1 searches for each full sentence in each input record and replaces it with the replacement string.






share|improve this answer



















  • 1




    Hey thanks this is working perfectly..can you please explain me how this works exactly? I am new to shell scripting and still getting the hang of things thanks again
    – Anuj Kulkarni
    Nov 7 at 19:32










  • See man gawk for understanding how it works, my English is not good enough :/
    – oguzismail
    Nov 7 at 19:44






  • 1




    Sure thanks a lot anyway
    – Anuj Kulkarni
    Nov 7 at 19:45










  • You're welcome, glad it helped you
    – oguzismail
    Nov 7 at 19:45






  • 1




    @AnujKulkarni, there's lots of good information on the awk tag info page: stackoverflow.com/tags/awk/info
    – glenn jackman
    Nov 7 at 20:11


















up vote
2
down vote













With bash and sed:



sed -f <(sed 's|","|/|; s|"|/|g; s|^|s|' file2.csv) file1


Output:




name xyz
name abc
doctor
I am an engineer.
dogs
I like cats.


The dot may be a problem because it is a special character in regex.






share|improve this answer























  • thanks but i get the following error: sed: file /dev/fd/63 line 1: unknown option to `s'
    – Anuj Kulkarni
    Nov 7 at 19:10












  • I had successfully tested it with bash 4.3.48 and GNU sed 4.2.2.
    – Cyrus
    Nov 7 at 19:13










  • I am trying it on bash 4.4.23 and GNU sed 4.5 but getting that error.
    – Anuj Kulkarni
    Nov 7 at 19:17










  • You might want to add s|/|\/|g to the sed script.
    – glenn jackman
    Nov 7 at 19:25


















up vote
0
down vote













A concise ruby script:



ruby -rcsv -e '
sentences = CSV.read(ARGV.shift).to_h
File.foreach(ARGV.shift, chomp: true) {|line| puts sentences[line] || line}
' file2.csv file1





share|improve this answer




























    up vote
    0
    down vote













    Using Perl One liner.



    $ cat file1
    my name is xyz.
    my name is abc.
    I am a doctor.
    I am an engineer.
    I like dogs.
    I like cats.

    $ cat file2.csv
    "my name is xyz.","name xyz"
    "my name is abc.","name abc"
    "I am a doctor.","doctor"
    "I like dogs.","dogs"

    $ perl -ne ' BEGIN {%kvp=map{chomp;s/"//g;split "," } qx(cat file2.csv)} { chomp;print $kvp{$_}?"$kvp{$_}.n":"$_n"; } ' file1
    name xyz.
    name abc.
    doctor.
    I am an engineer.
    dogs.
    I like cats.

    $





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














       

      draft saved


      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53195694%2fhow-to-find-an-entire-sentences-in-a-csv-file-and-replace-it-with-sentences-from%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote



      accepted










      Using awk



      awk -F'"(,")?' '
      NR==FNR { r[$2] = $3; next }
      { for (n in r) gsub(n, r[n]) } 1' file2.csv file1




      • -F'"(,")?' is the field separator, matches a " or ",", so that we don't need to remove double quotes from fields,


      • NR==FNR { r[$2] = $3; next } populates an array with content of file2.csv using the full sentence as key and replacement string as value,


      • { for (n in r) gsub(n, r[n]) } 1 searches for each full sentence in each input record and replaces it with the replacement string.






      share|improve this answer



















      • 1




        Hey thanks this is working perfectly..can you please explain me how this works exactly? I am new to shell scripting and still getting the hang of things thanks again
        – Anuj Kulkarni
        Nov 7 at 19:32










      • See man gawk for understanding how it works, my English is not good enough :/
        – oguzismail
        Nov 7 at 19:44






      • 1




        Sure thanks a lot anyway
        – Anuj Kulkarni
        Nov 7 at 19:45










      • You're welcome, glad it helped you
        – oguzismail
        Nov 7 at 19:45






      • 1




        @AnujKulkarni, there's lots of good information on the awk tag info page: stackoverflow.com/tags/awk/info
        – glenn jackman
        Nov 7 at 20:11















      up vote
      0
      down vote



      accepted










      Using awk



      awk -F'"(,")?' '
      NR==FNR { r[$2] = $3; next }
      { for (n in r) gsub(n, r[n]) } 1' file2.csv file1




      • -F'"(,")?' is the field separator, matches a " or ",", so that we don't need to remove double quotes from fields,


      • NR==FNR { r[$2] = $3; next } populates an array with content of file2.csv using the full sentence as key and replacement string as value,


      • { for (n in r) gsub(n, r[n]) } 1 searches for each full sentence in each input record and replaces it with the replacement string.






      share|improve this answer



















      • 1




        Hey thanks this is working perfectly..can you please explain me how this works exactly? I am new to shell scripting and still getting the hang of things thanks again
        – Anuj Kulkarni
        Nov 7 at 19:32










      • See man gawk for understanding how it works, my English is not good enough :/
        – oguzismail
        Nov 7 at 19:44






      • 1




        Sure thanks a lot anyway
        – Anuj Kulkarni
        Nov 7 at 19:45










      • You're welcome, glad it helped you
        – oguzismail
        Nov 7 at 19:45






      • 1




        @AnujKulkarni, there's lots of good information on the awk tag info page: stackoverflow.com/tags/awk/info
        – glenn jackman
        Nov 7 at 20:11













      up vote
      0
      down vote



      accepted







      up vote
      0
      down vote



      accepted






      Using awk



      awk -F'"(,")?' '
      NR==FNR { r[$2] = $3; next }
      { for (n in r) gsub(n, r[n]) } 1' file2.csv file1




      • -F'"(,")?' is the field separator, matches a " or ",", so that we don't need to remove double quotes from fields,


      • NR==FNR { r[$2] = $3; next } populates an array with content of file2.csv using the full sentence as key and replacement string as value,


      • { for (n in r) gsub(n, r[n]) } 1 searches for each full sentence in each input record and replaces it with the replacement string.






      share|improve this answer














      Using awk



      awk -F'"(,")?' '
      NR==FNR { r[$2] = $3; next }
      { for (n in r) gsub(n, r[n]) } 1' file2.csv file1




      • -F'"(,")?' is the field separator, matches a " or ",", so that we don't need to remove double quotes from fields,


      • NR==FNR { r[$2] = $3; next } populates an array with content of file2.csv using the full sentence as key and replacement string as value,


      • { for (n in r) gsub(n, r[n]) } 1 searches for each full sentence in each input record and replaces it with the replacement string.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 7 at 19:23

























      answered Nov 7 at 19:14









      oguzismail

      2,259518




      2,259518








      • 1




        Hey thanks this is working perfectly..can you please explain me how this works exactly? I am new to shell scripting and still getting the hang of things thanks again
        – Anuj Kulkarni
        Nov 7 at 19:32










      • See man gawk for understanding how it works, my English is not good enough :/
        – oguzismail
        Nov 7 at 19:44






      • 1




        Sure thanks a lot anyway
        – Anuj Kulkarni
        Nov 7 at 19:45










      • You're welcome, glad it helped you
        – oguzismail
        Nov 7 at 19:45






      • 1




        @AnujKulkarni, there's lots of good information on the awk tag info page: stackoverflow.com/tags/awk/info
        – glenn jackman
        Nov 7 at 20:11














      • 1




        Hey thanks this is working perfectly..can you please explain me how this works exactly? I am new to shell scripting and still getting the hang of things thanks again
        – Anuj Kulkarni
        Nov 7 at 19:32










      • See man gawk for understanding how it works, my English is not good enough :/
        – oguzismail
        Nov 7 at 19:44






      • 1




        Sure thanks a lot anyway
        – Anuj Kulkarni
        Nov 7 at 19:45










      • You're welcome, glad it helped you
        – oguzismail
        Nov 7 at 19:45






      • 1




        @AnujKulkarni, there's lots of good information on the awk tag info page: stackoverflow.com/tags/awk/info
        – glenn jackman
        Nov 7 at 20:11








      1




      1




      Hey thanks this is working perfectly..can you please explain me how this works exactly? I am new to shell scripting and still getting the hang of things thanks again
      – Anuj Kulkarni
      Nov 7 at 19:32




      Hey thanks this is working perfectly..can you please explain me how this works exactly? I am new to shell scripting and still getting the hang of things thanks again
      – Anuj Kulkarni
      Nov 7 at 19:32












      See man gawk for understanding how it works, my English is not good enough :/
      – oguzismail
      Nov 7 at 19:44




      See man gawk for understanding how it works, my English is not good enough :/
      – oguzismail
      Nov 7 at 19:44




      1




      1




      Sure thanks a lot anyway
      – Anuj Kulkarni
      Nov 7 at 19:45




      Sure thanks a lot anyway
      – Anuj Kulkarni
      Nov 7 at 19:45












      You're welcome, glad it helped you
      – oguzismail
      Nov 7 at 19:45




      You're welcome, glad it helped you
      – oguzismail
      Nov 7 at 19:45




      1




      1




      @AnujKulkarni, there's lots of good information on the awk tag info page: stackoverflow.com/tags/awk/info
      – glenn jackman
      Nov 7 at 20:11




      @AnujKulkarni, there's lots of good information on the awk tag info page: stackoverflow.com/tags/awk/info
      – glenn jackman
      Nov 7 at 20:11












      up vote
      2
      down vote













      With bash and sed:



      sed -f <(sed 's|","|/|; s|"|/|g; s|^|s|' file2.csv) file1


      Output:




      name xyz
      name abc
      doctor
      I am an engineer.
      dogs
      I like cats.


      The dot may be a problem because it is a special character in regex.






      share|improve this answer























      • thanks but i get the following error: sed: file /dev/fd/63 line 1: unknown option to `s'
        – Anuj Kulkarni
        Nov 7 at 19:10












      • I had successfully tested it with bash 4.3.48 and GNU sed 4.2.2.
        – Cyrus
        Nov 7 at 19:13










      • I am trying it on bash 4.4.23 and GNU sed 4.5 but getting that error.
        – Anuj Kulkarni
        Nov 7 at 19:17










      • You might want to add s|/|\/|g to the sed script.
        – glenn jackman
        Nov 7 at 19:25















      up vote
      2
      down vote













      With bash and sed:



      sed -f <(sed 's|","|/|; s|"|/|g; s|^|s|' file2.csv) file1


      Output:




      name xyz
      name abc
      doctor
      I am an engineer.
      dogs
      I like cats.


      The dot may be a problem because it is a special character in regex.






      share|improve this answer























      • thanks but i get the following error: sed: file /dev/fd/63 line 1: unknown option to `s'
        – Anuj Kulkarni
        Nov 7 at 19:10












      • I had successfully tested it with bash 4.3.48 and GNU sed 4.2.2.
        – Cyrus
        Nov 7 at 19:13










      • I am trying it on bash 4.4.23 and GNU sed 4.5 but getting that error.
        – Anuj Kulkarni
        Nov 7 at 19:17










      • You might want to add s|/|\/|g to the sed script.
        – glenn jackman
        Nov 7 at 19:25













      up vote
      2
      down vote










      up vote
      2
      down vote









      With bash and sed:



      sed -f <(sed 's|","|/|; s|"|/|g; s|^|s|' file2.csv) file1


      Output:




      name xyz
      name abc
      doctor
      I am an engineer.
      dogs
      I like cats.


      The dot may be a problem because it is a special character in regex.






      share|improve this answer














      With bash and sed:



      sed -f <(sed 's|","|/|; s|"|/|g; s|^|s|' file2.csv) file1


      Output:




      name xyz
      name abc
      doctor
      I am an engineer.
      dogs
      I like cats.


      The dot may be a problem because it is a special character in regex.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 7 at 19:09

























      answered Nov 7 at 19:02









      Cyrus

      44.4k43375




      44.4k43375












      • thanks but i get the following error: sed: file /dev/fd/63 line 1: unknown option to `s'
        – Anuj Kulkarni
        Nov 7 at 19:10












      • I had successfully tested it with bash 4.3.48 and GNU sed 4.2.2.
        – Cyrus
        Nov 7 at 19:13










      • I am trying it on bash 4.4.23 and GNU sed 4.5 but getting that error.
        – Anuj Kulkarni
        Nov 7 at 19:17










      • You might want to add s|/|\/|g to the sed script.
        – glenn jackman
        Nov 7 at 19:25


















      • thanks but i get the following error: sed: file /dev/fd/63 line 1: unknown option to `s'
        – Anuj Kulkarni
        Nov 7 at 19:10












      • I had successfully tested it with bash 4.3.48 and GNU sed 4.2.2.
        – Cyrus
        Nov 7 at 19:13










      • I am trying it on bash 4.4.23 and GNU sed 4.5 but getting that error.
        – Anuj Kulkarni
        Nov 7 at 19:17










      • You might want to add s|/|\/|g to the sed script.
        – glenn jackman
        Nov 7 at 19:25
















      thanks but i get the following error: sed: file /dev/fd/63 line 1: unknown option to `s'
      – Anuj Kulkarni
      Nov 7 at 19:10






      thanks but i get the following error: sed: file /dev/fd/63 line 1: unknown option to `s'
      – Anuj Kulkarni
      Nov 7 at 19:10














      I had successfully tested it with bash 4.3.48 and GNU sed 4.2.2.
      – Cyrus
      Nov 7 at 19:13




      I had successfully tested it with bash 4.3.48 and GNU sed 4.2.2.
      – Cyrus
      Nov 7 at 19:13












      I am trying it on bash 4.4.23 and GNU sed 4.5 but getting that error.
      – Anuj Kulkarni
      Nov 7 at 19:17




      I am trying it on bash 4.4.23 and GNU sed 4.5 but getting that error.
      – Anuj Kulkarni
      Nov 7 at 19:17












      You might want to add s|/|\/|g to the sed script.
      – glenn jackman
      Nov 7 at 19:25




      You might want to add s|/|\/|g to the sed script.
      – glenn jackman
      Nov 7 at 19:25










      up vote
      0
      down vote













      A concise ruby script:



      ruby -rcsv -e '
      sentences = CSV.read(ARGV.shift).to_h
      File.foreach(ARGV.shift, chomp: true) {|line| puts sentences[line] || line}
      ' file2.csv file1





      share|improve this answer

























        up vote
        0
        down vote













        A concise ruby script:



        ruby -rcsv -e '
        sentences = CSV.read(ARGV.shift).to_h
        File.foreach(ARGV.shift, chomp: true) {|line| puts sentences[line] || line}
        ' file2.csv file1





        share|improve this answer























          up vote
          0
          down vote










          up vote
          0
          down vote









          A concise ruby script:



          ruby -rcsv -e '
          sentences = CSV.read(ARGV.shift).to_h
          File.foreach(ARGV.shift, chomp: true) {|line| puts sentences[line] || line}
          ' file2.csv file1





          share|improve this answer












          A concise ruby script:



          ruby -rcsv -e '
          sentences = CSV.read(ARGV.shift).to_h
          File.foreach(ARGV.shift, chomp: true) {|line| puts sentences[line] || line}
          ' file2.csv file1






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 7 at 19:21









          glenn jackman

          164k26138232




          164k26138232






















              up vote
              0
              down vote













              Using Perl One liner.



              $ cat file1
              my name is xyz.
              my name is abc.
              I am a doctor.
              I am an engineer.
              I like dogs.
              I like cats.

              $ cat file2.csv
              "my name is xyz.","name xyz"
              "my name is abc.","name abc"
              "I am a doctor.","doctor"
              "I like dogs.","dogs"

              $ perl -ne ' BEGIN {%kvp=map{chomp;s/"//g;split "," } qx(cat file2.csv)} { chomp;print $kvp{$_}?"$kvp{$_}.n":"$_n"; } ' file1
              name xyz.
              name abc.
              doctor.
              I am an engineer.
              dogs.
              I like cats.

              $





              share|improve this answer



























                up vote
                0
                down vote













                Using Perl One liner.



                $ cat file1
                my name is xyz.
                my name is abc.
                I am a doctor.
                I am an engineer.
                I like dogs.
                I like cats.

                $ cat file2.csv
                "my name is xyz.","name xyz"
                "my name is abc.","name abc"
                "I am a doctor.","doctor"
                "I like dogs.","dogs"

                $ perl -ne ' BEGIN {%kvp=map{chomp;s/"//g;split "," } qx(cat file2.csv)} { chomp;print $kvp{$_}?"$kvp{$_}.n":"$_n"; } ' file1
                name xyz.
                name abc.
                doctor.
                I am an engineer.
                dogs.
                I like cats.

                $





                share|improve this answer

























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Using Perl One liner.



                  $ cat file1
                  my name is xyz.
                  my name is abc.
                  I am a doctor.
                  I am an engineer.
                  I like dogs.
                  I like cats.

                  $ cat file2.csv
                  "my name is xyz.","name xyz"
                  "my name is abc.","name abc"
                  "I am a doctor.","doctor"
                  "I like dogs.","dogs"

                  $ perl -ne ' BEGIN {%kvp=map{chomp;s/"//g;split "," } qx(cat file2.csv)} { chomp;print $kvp{$_}?"$kvp{$_}.n":"$_n"; } ' file1
                  name xyz.
                  name abc.
                  doctor.
                  I am an engineer.
                  dogs.
                  I like cats.

                  $





                  share|improve this answer














                  Using Perl One liner.



                  $ cat file1
                  my name is xyz.
                  my name is abc.
                  I am a doctor.
                  I am an engineer.
                  I like dogs.
                  I like cats.

                  $ cat file2.csv
                  "my name is xyz.","name xyz"
                  "my name is abc.","name abc"
                  "I am a doctor.","doctor"
                  "I like dogs.","dogs"

                  $ perl -ne ' BEGIN {%kvp=map{chomp;s/"//g;split "," } qx(cat file2.csv)} { chomp;print $kvp{$_}?"$kvp{$_}.n":"$_n"; } ' file1
                  name xyz.
                  name abc.
                  doctor.
                  I am an engineer.
                  dogs.
                  I like cats.

                  $






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 9 at 12:32

























                  answered Nov 8 at 11:43









                  stack0114106

                  1,3831416




                  1,3831416






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53195694%2fhow-to-find-an-entire-sentences-in-a-csv-file-and-replace-it-with-sentences-from%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      Xamarin.form Move up view when keyboard appear

                      Post-Redirect-Get with Spring WebFlux and Thymeleaf

                      Anylogic : not able to use stopDelay()