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.
linux bash shell awk sed
add a comment |
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.
linux bash shell awk sed
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
add a comment |
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.
linux bash shell awk sed
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
linux bash shell awk sed
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
add a comment |
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
add a comment |
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 offile2.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.
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
Seeman 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
add a comment |
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.
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 adds|/|\/|g
to the sed script.
– glenn jackman
Nov 7 at 19:25
add a comment |
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
add a comment |
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.
$
add a comment |
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 offile2.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.
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
Seeman 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
add a comment |
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 offile2.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.
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
Seeman 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
add a comment |
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 offile2.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.
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 offile2.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.
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
Seeman 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
add a comment |
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
Seeman 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
add a comment |
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.
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 adds|/|\/|g
to the sed script.
– glenn jackman
Nov 7 at 19:25
add a comment |
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.
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 adds|/|\/|g
to the sed script.
– glenn jackman
Nov 7 at 19:25
add a comment |
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.
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.
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 adds|/|\/|g
to the sed script.
– glenn jackman
Nov 7 at 19:25
add a comment |
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 adds|/|\/|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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 7 at 19:21
glenn jackman
164k26138232
164k26138232
add a comment |
add a comment |
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.
$
add a comment |
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.
$
add a comment |
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.
$
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.
$
edited Nov 9 at 12:32
answered Nov 8 at 11:43
stack0114106
1,3831416
1,3831416
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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