Change a list of strings to lowercase
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
I have two files, one file contains a list of strings.
+stringa +Dog +Cat
+cat +Tux +elephant
and the second file (csv) contains something like:
"123456 Abc","+Stringx +123","something"
"23456 dEf","+cat +Tux +elephant","Other something"
"34524 xyz","+stringa +Dog +Cat","third something"
the result should be:
"123456 Abc","+Stringx +123","something"
"23456 dEf","+cat +tux +elephant","Other something"
"34524 xyz","+stringa +dog +cat","third something"
How I can change the strings, that match my list of patterns, to lowercase?
My comma-separated values file have about 30 columns and about 1500 rows.
text-processing
add a comment |Â
up vote
2
down vote
favorite
I have two files, one file contains a list of strings.
+stringa +Dog +Cat
+cat +Tux +elephant
and the second file (csv) contains something like:
"123456 Abc","+Stringx +123","something"
"23456 dEf","+cat +Tux +elephant","Other something"
"34524 xyz","+stringa +Dog +Cat","third something"
the result should be:
"123456 Abc","+Stringx +123","something"
"23456 dEf","+cat +tux +elephant","Other something"
"34524 xyz","+stringa +dog +cat","third something"
How I can change the strings, that match my list of patterns, to lowercase?
My comma-separated values file have about 30 columns and about 1500 rows.
text-processing
1
can you add some code you tried? what tools do you have? GNU sed? perl? awk?
â Sundeep
Nov 27 '17 at 10:34
Despite newer versions having an upper- & lower-case variable operator, bash is not a text editor.
â Jeff Schaller
Nov 27 '17 at 10:48
@Sundeep I can not find the right approach. I do not understandperl
but it is available. I use Debian 8.9 (Jessie). The point is i like change only the pattern, not the whole file. I havesed
,awk
,tr
â FaxMax
Nov 27 '17 at 10:54
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have two files, one file contains a list of strings.
+stringa +Dog +Cat
+cat +Tux +elephant
and the second file (csv) contains something like:
"123456 Abc","+Stringx +123","something"
"23456 dEf","+cat +Tux +elephant","Other something"
"34524 xyz","+stringa +Dog +Cat","third something"
the result should be:
"123456 Abc","+Stringx +123","something"
"23456 dEf","+cat +tux +elephant","Other something"
"34524 xyz","+stringa +dog +cat","third something"
How I can change the strings, that match my list of patterns, to lowercase?
My comma-separated values file have about 30 columns and about 1500 rows.
text-processing
I have two files, one file contains a list of strings.
+stringa +Dog +Cat
+cat +Tux +elephant
and the second file (csv) contains something like:
"123456 Abc","+Stringx +123","something"
"23456 dEf","+cat +Tux +elephant","Other something"
"34524 xyz","+stringa +Dog +Cat","third something"
the result should be:
"123456 Abc","+Stringx +123","something"
"23456 dEf","+cat +tux +elephant","Other something"
"34524 xyz","+stringa +dog +cat","third something"
How I can change the strings, that match my list of patterns, to lowercase?
My comma-separated values file have about 30 columns and about 1500 rows.
text-processing
edited Nov 27 '17 at 14:44
Philip Kirkbride
2,2922470
2,2922470
asked Nov 27 '17 at 10:24
FaxMax
428219
428219
1
can you add some code you tried? what tools do you have? GNU sed? perl? awk?
â Sundeep
Nov 27 '17 at 10:34
Despite newer versions having an upper- & lower-case variable operator, bash is not a text editor.
â Jeff Schaller
Nov 27 '17 at 10:48
@Sundeep I can not find the right approach. I do not understandperl
but it is available. I use Debian 8.9 (Jessie). The point is i like change only the pattern, not the whole file. I havesed
,awk
,tr
â FaxMax
Nov 27 '17 at 10:54
add a comment |Â
1
can you add some code you tried? what tools do you have? GNU sed? perl? awk?
â Sundeep
Nov 27 '17 at 10:34
Despite newer versions having an upper- & lower-case variable operator, bash is not a text editor.
â Jeff Schaller
Nov 27 '17 at 10:48
@Sundeep I can not find the right approach. I do not understandperl
but it is available. I use Debian 8.9 (Jessie). The point is i like change only the pattern, not the whole file. I havesed
,awk
,tr
â FaxMax
Nov 27 '17 at 10:54
1
1
can you add some code you tried? what tools do you have? GNU sed? perl? awk?
â Sundeep
Nov 27 '17 at 10:34
can you add some code you tried? what tools do you have? GNU sed? perl? awk?
â Sundeep
Nov 27 '17 at 10:34
Despite newer versions having an upper- & lower-case variable operator, bash is not a text editor.
â Jeff Schaller
Nov 27 '17 at 10:48
Despite newer versions having an upper- & lower-case variable operator, bash is not a text editor.
â Jeff Schaller
Nov 27 '17 at 10:48
@Sundeep I can not find the right approach. I do not understand
perl
but it is available. I use Debian 8.9 (Jessie). The point is i like change only the pattern, not the whole file. I have sed
, awk
, tr
â FaxMax
Nov 27 '17 at 10:54
@Sundeep I can not find the right approach. I do not understand
perl
but it is available. I use Debian 8.9 (Jessie). The point is i like change only the pattern, not the whole file. I have sed
, awk
, tr
â FaxMax
Nov 27 '17 at 10:54
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
3
down vote
accepted
With GNU sed
, assumes that you do not have any meta character in list of strings, +
is not a meta character with default BRE
$ # create substitute command for each line
$ sed 's/.*/s|"&"|\L&|gi/' f1
s|"+stringa +Dog +Cat"|L&|gi
s|"+cat +Tux +elephant"|L&|gi
$ # pass those commands as sed script
$ sed -f <(sed 's/.*/s|"&"|\L&|gi/' f1) ip.csv
"123456 Abc","+Stringx +123","something"
"23456 dEf","+cat +tux +elephant","Other something"
"34524 xyz","+stringa +dog +cat","third something"
$ # or save them in a file and use
$ sed 's/.*/s|"&"|\L&|gi/' f1 > f2
$ sed -f f2 ip.csv
L
to convert string to lowercaseg
for replacing all occurrences in a line,i
for case-insensitive matching
If you don't have GNU sed
$ # Q to quote metacharacters
$ # but will have issues if you have or $ or @
$ sed 's/.*/s|\Q"&"|\L$&|gi;/' f1
s|Q"+stringa +Dog +Cat"|L$&|gi;
s|Q"+cat +Tux +elephant"|L$&|gi;
$ perl -p <(sed 's/.*/s|\Q"&"|\L$&|gi;/' f1) ip.csv
"123456 Abc","+Stringx +123","something"
"23456 dEf","+cat +tux +elephant","Other something"
"34524 xyz","+stringa +dog +cat","third something"
As noted by Stéphane Chazelas, this could lead to code injection vulnerabilities if contents of f1
is not under control
1
It may be worth noting that those amount to command injection vulnerabilities if the content off1
is not under your control.
â Stéphane Chazelas
Nov 27 '17 at 11:23
good observation as always :) added a note
â Sundeep
Nov 27 '17 at 11:31
add a comment |Â
up vote
2
down vote
With perl
, assuming you want each word in the first file to be turned to lowercase:
perl -pe '
BEGIN local $/ = undef; $regex = join "
s/$regex/L$&/g' file1.words file2.csv
local $/ = undef
makes the record separator for the BEGIN block undefined so that the one invocation of <>
there, slurps the whole first file (file1.words
) in. We split that on whitespace (split " "
is special in perl
in the same way as awk -F " "
is in awk
), and join the resulting words with |
after having regex-quoted them and made them case insensitive.
So we have a huge regexp that is something like (?i:word1)|(?i:word2)|...
which we apply on each line of the second file in the rest of the code.
If it's each string in each line of the first file, then that can be simplified to:
perl -pe '
BEGIN ", map qrQ$_Ei, @strings
s/$regex/L$&/g' < file1.strings file2.csv
There, we open the first file on stdin instead of passing it as argument. <STDIN>
returns a list of its lines from which we remove the delimiters with chomp
, and join with |
as above.
If you don't want it to be limited to ASCII characters, add the -Mopen=locale
option.
1
@Sundeep. That was my initial interpretation, but now that you mention it, you're probably right the lines offile1
are probably to be the strings to be matched. See edit.
â Stéphane Chazelas
Nov 27 '17 at 11:45
add a comment |Â
up vote
2
down vote
AWK
solution (for your current input):
Assuming that the 2nd field is of main interest and values in search file are double-quoted.
awk 'NR==FNR $0="42"$0"42"; a[$0]; next
$2 in a $2=tolower($2) 1' patterns FS=',' OFS=',' file.csv
$0="42"$0"42"
- wrap a pattern line with double quotes while iterating through the lines ofpatterns
filea[$0]
- capturing a pattern line into arraya
$2 in a $2=tolower($2)
- if the 2nd field value from line offile.csv
file is in the list of patterns(i.e. arraya
) - convert all characters in it to lowercase$2=tolower($2)
The output:
"123456 Abc","+Stringx +123","something"
"23456 dEf","+cat +tux +elephant","Other something"
"34524 xyz","+stringa +dog +cat","third something"
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
With GNU sed
, assumes that you do not have any meta character in list of strings, +
is not a meta character with default BRE
$ # create substitute command for each line
$ sed 's/.*/s|"&"|\L&|gi/' f1
s|"+stringa +Dog +Cat"|L&|gi
s|"+cat +Tux +elephant"|L&|gi
$ # pass those commands as sed script
$ sed -f <(sed 's/.*/s|"&"|\L&|gi/' f1) ip.csv
"123456 Abc","+Stringx +123","something"
"23456 dEf","+cat +tux +elephant","Other something"
"34524 xyz","+stringa +dog +cat","third something"
$ # or save them in a file and use
$ sed 's/.*/s|"&"|\L&|gi/' f1 > f2
$ sed -f f2 ip.csv
L
to convert string to lowercaseg
for replacing all occurrences in a line,i
for case-insensitive matching
If you don't have GNU sed
$ # Q to quote metacharacters
$ # but will have issues if you have or $ or @
$ sed 's/.*/s|\Q"&"|\L$&|gi;/' f1
s|Q"+stringa +Dog +Cat"|L$&|gi;
s|Q"+cat +Tux +elephant"|L$&|gi;
$ perl -p <(sed 's/.*/s|\Q"&"|\L$&|gi;/' f1) ip.csv
"123456 Abc","+Stringx +123","something"
"23456 dEf","+cat +tux +elephant","Other something"
"34524 xyz","+stringa +dog +cat","third something"
As noted by Stéphane Chazelas, this could lead to code injection vulnerabilities if contents of f1
is not under control
1
It may be worth noting that those amount to command injection vulnerabilities if the content off1
is not under your control.
â Stéphane Chazelas
Nov 27 '17 at 11:23
good observation as always :) added a note
â Sundeep
Nov 27 '17 at 11:31
add a comment |Â
up vote
3
down vote
accepted
With GNU sed
, assumes that you do not have any meta character in list of strings, +
is not a meta character with default BRE
$ # create substitute command for each line
$ sed 's/.*/s|"&"|\L&|gi/' f1
s|"+stringa +Dog +Cat"|L&|gi
s|"+cat +Tux +elephant"|L&|gi
$ # pass those commands as sed script
$ sed -f <(sed 's/.*/s|"&"|\L&|gi/' f1) ip.csv
"123456 Abc","+Stringx +123","something"
"23456 dEf","+cat +tux +elephant","Other something"
"34524 xyz","+stringa +dog +cat","third something"
$ # or save them in a file and use
$ sed 's/.*/s|"&"|\L&|gi/' f1 > f2
$ sed -f f2 ip.csv
L
to convert string to lowercaseg
for replacing all occurrences in a line,i
for case-insensitive matching
If you don't have GNU sed
$ # Q to quote metacharacters
$ # but will have issues if you have or $ or @
$ sed 's/.*/s|\Q"&"|\L$&|gi;/' f1
s|Q"+stringa +Dog +Cat"|L$&|gi;
s|Q"+cat +Tux +elephant"|L$&|gi;
$ perl -p <(sed 's/.*/s|\Q"&"|\L$&|gi;/' f1) ip.csv
"123456 Abc","+Stringx +123","something"
"23456 dEf","+cat +tux +elephant","Other something"
"34524 xyz","+stringa +dog +cat","third something"
As noted by Stéphane Chazelas, this could lead to code injection vulnerabilities if contents of f1
is not under control
1
It may be worth noting that those amount to command injection vulnerabilities if the content off1
is not under your control.
â Stéphane Chazelas
Nov 27 '17 at 11:23
good observation as always :) added a note
â Sundeep
Nov 27 '17 at 11:31
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
With GNU sed
, assumes that you do not have any meta character in list of strings, +
is not a meta character with default BRE
$ # create substitute command for each line
$ sed 's/.*/s|"&"|\L&|gi/' f1
s|"+stringa +Dog +Cat"|L&|gi
s|"+cat +Tux +elephant"|L&|gi
$ # pass those commands as sed script
$ sed -f <(sed 's/.*/s|"&"|\L&|gi/' f1) ip.csv
"123456 Abc","+Stringx +123","something"
"23456 dEf","+cat +tux +elephant","Other something"
"34524 xyz","+stringa +dog +cat","third something"
$ # or save them in a file and use
$ sed 's/.*/s|"&"|\L&|gi/' f1 > f2
$ sed -f f2 ip.csv
L
to convert string to lowercaseg
for replacing all occurrences in a line,i
for case-insensitive matching
If you don't have GNU sed
$ # Q to quote metacharacters
$ # but will have issues if you have or $ or @
$ sed 's/.*/s|\Q"&"|\L$&|gi;/' f1
s|Q"+stringa +Dog +Cat"|L$&|gi;
s|Q"+cat +Tux +elephant"|L$&|gi;
$ perl -p <(sed 's/.*/s|\Q"&"|\L$&|gi;/' f1) ip.csv
"123456 Abc","+Stringx +123","something"
"23456 dEf","+cat +tux +elephant","Other something"
"34524 xyz","+stringa +dog +cat","third something"
As noted by Stéphane Chazelas, this could lead to code injection vulnerabilities if contents of f1
is not under control
With GNU sed
, assumes that you do not have any meta character in list of strings, +
is not a meta character with default BRE
$ # create substitute command for each line
$ sed 's/.*/s|"&"|\L&|gi/' f1
s|"+stringa +Dog +Cat"|L&|gi
s|"+cat +Tux +elephant"|L&|gi
$ # pass those commands as sed script
$ sed -f <(sed 's/.*/s|"&"|\L&|gi/' f1) ip.csv
"123456 Abc","+Stringx +123","something"
"23456 dEf","+cat +tux +elephant","Other something"
"34524 xyz","+stringa +dog +cat","third something"
$ # or save them in a file and use
$ sed 's/.*/s|"&"|\L&|gi/' f1 > f2
$ sed -f f2 ip.csv
L
to convert string to lowercaseg
for replacing all occurrences in a line,i
for case-insensitive matching
If you don't have GNU sed
$ # Q to quote metacharacters
$ # but will have issues if you have or $ or @
$ sed 's/.*/s|\Q"&"|\L$&|gi;/' f1
s|Q"+stringa +Dog +Cat"|L$&|gi;
s|Q"+cat +Tux +elephant"|L$&|gi;
$ perl -p <(sed 's/.*/s|\Q"&"|\L$&|gi;/' f1) ip.csv
"123456 Abc","+Stringx +123","something"
"23456 dEf","+cat +tux +elephant","Other something"
"34524 xyz","+stringa +dog +cat","third something"
As noted by Stéphane Chazelas, this could lead to code injection vulnerabilities if contents of f1
is not under control
edited Nov 28 '17 at 10:27
answered Nov 27 '17 at 11:02
Sundeep
6,9611826
6,9611826
1
It may be worth noting that those amount to command injection vulnerabilities if the content off1
is not under your control.
â Stéphane Chazelas
Nov 27 '17 at 11:23
good observation as always :) added a note
â Sundeep
Nov 27 '17 at 11:31
add a comment |Â
1
It may be worth noting that those amount to command injection vulnerabilities if the content off1
is not under your control.
â Stéphane Chazelas
Nov 27 '17 at 11:23
good observation as always :) added a note
â Sundeep
Nov 27 '17 at 11:31
1
1
It may be worth noting that those amount to command injection vulnerabilities if the content of
f1
is not under your control.â Stéphane Chazelas
Nov 27 '17 at 11:23
It may be worth noting that those amount to command injection vulnerabilities if the content of
f1
is not under your control.â Stéphane Chazelas
Nov 27 '17 at 11:23
good observation as always :) added a note
â Sundeep
Nov 27 '17 at 11:31
good observation as always :) added a note
â Sundeep
Nov 27 '17 at 11:31
add a comment |Â
up vote
2
down vote
With perl
, assuming you want each word in the first file to be turned to lowercase:
perl -pe '
BEGIN local $/ = undef; $regex = join "
s/$regex/L$&/g' file1.words file2.csv
local $/ = undef
makes the record separator for the BEGIN block undefined so that the one invocation of <>
there, slurps the whole first file (file1.words
) in. We split that on whitespace (split " "
is special in perl
in the same way as awk -F " "
is in awk
), and join the resulting words with |
after having regex-quoted them and made them case insensitive.
So we have a huge regexp that is something like (?i:word1)|(?i:word2)|...
which we apply on each line of the second file in the rest of the code.
If it's each string in each line of the first file, then that can be simplified to:
perl -pe '
BEGIN ", map qrQ$_Ei, @strings
s/$regex/L$&/g' < file1.strings file2.csv
There, we open the first file on stdin instead of passing it as argument. <STDIN>
returns a list of its lines from which we remove the delimiters with chomp
, and join with |
as above.
If you don't want it to be limited to ASCII characters, add the -Mopen=locale
option.
1
@Sundeep. That was my initial interpretation, but now that you mention it, you're probably right the lines offile1
are probably to be the strings to be matched. See edit.
â Stéphane Chazelas
Nov 27 '17 at 11:45
add a comment |Â
up vote
2
down vote
With perl
, assuming you want each word in the first file to be turned to lowercase:
perl -pe '
BEGIN local $/ = undef; $regex = join "
s/$regex/L$&/g' file1.words file2.csv
local $/ = undef
makes the record separator for the BEGIN block undefined so that the one invocation of <>
there, slurps the whole first file (file1.words
) in. We split that on whitespace (split " "
is special in perl
in the same way as awk -F " "
is in awk
), and join the resulting words with |
after having regex-quoted them and made them case insensitive.
So we have a huge regexp that is something like (?i:word1)|(?i:word2)|...
which we apply on each line of the second file in the rest of the code.
If it's each string in each line of the first file, then that can be simplified to:
perl -pe '
BEGIN ", map qrQ$_Ei, @strings
s/$regex/L$&/g' < file1.strings file2.csv
There, we open the first file on stdin instead of passing it as argument. <STDIN>
returns a list of its lines from which we remove the delimiters with chomp
, and join with |
as above.
If you don't want it to be limited to ASCII characters, add the -Mopen=locale
option.
1
@Sundeep. That was my initial interpretation, but now that you mention it, you're probably right the lines offile1
are probably to be the strings to be matched. See edit.
â Stéphane Chazelas
Nov 27 '17 at 11:45
add a comment |Â
up vote
2
down vote
up vote
2
down vote
With perl
, assuming you want each word in the first file to be turned to lowercase:
perl -pe '
BEGIN local $/ = undef; $regex = join "
s/$regex/L$&/g' file1.words file2.csv
local $/ = undef
makes the record separator for the BEGIN block undefined so that the one invocation of <>
there, slurps the whole first file (file1.words
) in. We split that on whitespace (split " "
is special in perl
in the same way as awk -F " "
is in awk
), and join the resulting words with |
after having regex-quoted them and made them case insensitive.
So we have a huge regexp that is something like (?i:word1)|(?i:word2)|...
which we apply on each line of the second file in the rest of the code.
If it's each string in each line of the first file, then that can be simplified to:
perl -pe '
BEGIN ", map qrQ$_Ei, @strings
s/$regex/L$&/g' < file1.strings file2.csv
There, we open the first file on stdin instead of passing it as argument. <STDIN>
returns a list of its lines from which we remove the delimiters with chomp
, and join with |
as above.
If you don't want it to be limited to ASCII characters, add the -Mopen=locale
option.
With perl
, assuming you want each word in the first file to be turned to lowercase:
perl -pe '
BEGIN local $/ = undef; $regex = join "
s/$regex/L$&/g' file1.words file2.csv
local $/ = undef
makes the record separator for the BEGIN block undefined so that the one invocation of <>
there, slurps the whole first file (file1.words
) in. We split that on whitespace (split " "
is special in perl
in the same way as awk -F " "
is in awk
), and join the resulting words with |
after having regex-quoted them and made them case insensitive.
So we have a huge regexp that is something like (?i:word1)|(?i:word2)|...
which we apply on each line of the second file in the rest of the code.
If it's each string in each line of the first file, then that can be simplified to:
perl -pe '
BEGIN ", map qrQ$_Ei, @strings
s/$regex/L$&/g' < file1.strings file2.csv
There, we open the first file on stdin instead of passing it as argument. <STDIN>
returns a list of its lines from which we remove the delimiters with chomp
, and join with |
as above.
If you don't want it to be limited to ASCII characters, add the -Mopen=locale
option.
edited Nov 27 '17 at 14:07
answered Nov 27 '17 at 11:14
Stéphane Chazelas
282k53521854
282k53521854
1
@Sundeep. That was my initial interpretation, but now that you mention it, you're probably right the lines offile1
are probably to be the strings to be matched. See edit.
â Stéphane Chazelas
Nov 27 '17 at 11:45
add a comment |Â
1
@Sundeep. That was my initial interpretation, but now that you mention it, you're probably right the lines offile1
are probably to be the strings to be matched. See edit.
â Stéphane Chazelas
Nov 27 '17 at 11:45
1
1
@Sundeep. That was my initial interpretation, but now that you mention it, you're probably right the lines of
file1
are probably to be the strings to be matched. See edit.â Stéphane Chazelas
Nov 27 '17 at 11:45
@Sundeep. That was my initial interpretation, but now that you mention it, you're probably right the lines of
file1
are probably to be the strings to be matched. See edit.â Stéphane Chazelas
Nov 27 '17 at 11:45
add a comment |Â
up vote
2
down vote
AWK
solution (for your current input):
Assuming that the 2nd field is of main interest and values in search file are double-quoted.
awk 'NR==FNR $0="42"$0"42"; a[$0]; next
$2 in a $2=tolower($2) 1' patterns FS=',' OFS=',' file.csv
$0="42"$0"42"
- wrap a pattern line with double quotes while iterating through the lines ofpatterns
filea[$0]
- capturing a pattern line into arraya
$2 in a $2=tolower($2)
- if the 2nd field value from line offile.csv
file is in the list of patterns(i.e. arraya
) - convert all characters in it to lowercase$2=tolower($2)
The output:
"123456 Abc","+Stringx +123","something"
"23456 dEf","+cat +tux +elephant","Other something"
"34524 xyz","+stringa +dog +cat","third something"
add a comment |Â
up vote
2
down vote
AWK
solution (for your current input):
Assuming that the 2nd field is of main interest and values in search file are double-quoted.
awk 'NR==FNR $0="42"$0"42"; a[$0]; next
$2 in a $2=tolower($2) 1' patterns FS=',' OFS=',' file.csv
$0="42"$0"42"
- wrap a pattern line with double quotes while iterating through the lines ofpatterns
filea[$0]
- capturing a pattern line into arraya
$2 in a $2=tolower($2)
- if the 2nd field value from line offile.csv
file is in the list of patterns(i.e. arraya
) - convert all characters in it to lowercase$2=tolower($2)
The output:
"123456 Abc","+Stringx +123","something"
"23456 dEf","+cat +tux +elephant","Other something"
"34524 xyz","+stringa +dog +cat","third something"
add a comment |Â
up vote
2
down vote
up vote
2
down vote
AWK
solution (for your current input):
Assuming that the 2nd field is of main interest and values in search file are double-quoted.
awk 'NR==FNR $0="42"$0"42"; a[$0]; next
$2 in a $2=tolower($2) 1' patterns FS=',' OFS=',' file.csv
$0="42"$0"42"
- wrap a pattern line with double quotes while iterating through the lines ofpatterns
filea[$0]
- capturing a pattern line into arraya
$2 in a $2=tolower($2)
- if the 2nd field value from line offile.csv
file is in the list of patterns(i.e. arraya
) - convert all characters in it to lowercase$2=tolower($2)
The output:
"123456 Abc","+Stringx +123","something"
"23456 dEf","+cat +tux +elephant","Other something"
"34524 xyz","+stringa +dog +cat","third something"
AWK
solution (for your current input):
Assuming that the 2nd field is of main interest and values in search file are double-quoted.
awk 'NR==FNR $0="42"$0"42"; a[$0]; next
$2 in a $2=tolower($2) 1' patterns FS=',' OFS=',' file.csv
$0="42"$0"42"
- wrap a pattern line with double quotes while iterating through the lines ofpatterns
filea[$0]
- capturing a pattern line into arraya
$2 in a $2=tolower($2)
- if the 2nd field value from line offile.csv
file is in the list of patterns(i.e. arraya
) - convert all characters in it to lowercase$2=tolower($2)
The output:
"123456 Abc","+Stringx +123","something"
"23456 dEf","+cat +tux +elephant","Other something"
"34524 xyz","+stringa +dog +cat","third something"
edited Nov 27 '17 at 15:39
answered Nov 27 '17 at 10:50
RomanPerekhrest
22.4k12145
22.4k12145
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f407252%2fchange-a-list-of-strings-to-lowercase%23new-answer', 'question_page');
);
Post as a guest
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
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
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
1
can you add some code you tried? what tools do you have? GNU sed? perl? awk?
â Sundeep
Nov 27 '17 at 10:34
Despite newer versions having an upper- & lower-case variable operator, bash is not a text editor.
â Jeff Schaller
Nov 27 '17 at 10:48
@Sundeep I can not find the right approach. I do not understand
perl
but it is available. I use Debian 8.9 (Jessie). The point is i like change only the pattern, not the whole file. I havesed
,awk
,tr
â FaxMax
Nov 27 '17 at 10:54