[Contents]
In bash, I would like to refer to the list with keywords (with Japanese, spaces, separated by new lines) file, compare the given string (with Japanese, space hyphen, etc.) with the list, and use it to replace or change the given string.
[Example]
List of keywords (hoge_list)
Chuo Ward, Tokyo
Meguro Ward, Tokyo
Suginami Ward, Tokyo
Yokohama City, Kanagawa Prefecture
Give String ($INPUT)
"Asagaya 1-2-3 in Suginami Ward, Tokyo"
The common part of the keyword in the list is Suginami Ward, Tokyo, so extract that part
Asagaya 1-2-3
I would like to print the .
Thank you for your cooperation.
linux bash shellscript
If you use hoge_list like a dictionary and try to find a match and delete it…, it's quite troublesome, so make it simpler,
It is useful to use sed for 一致remove matches 削除
s|^Chuo Ward, Tokyo||
s|^Meguro Ward, Tokyo||
s|^Suginami Ward, Tokyo||
s|^Yokohama City, Kanagawa Prefecture||
Create a file named hoge_list.sed and
echo 'Asagaya 1-2-3' in Suginami Ward, Tokyo | sed-foge_list.sed
and
Asagaya 1-2-3
is obtained.
Since hoge_list actually has quite a few lines, you want to automate the creation of hoge_list.sed based on it.
Including that part, the shell script looks like this:
#!/bin/bash
cathoge_list | sed-e's / \(.*\)/s |^\1||/'>hoge_list.sed
sed-foge_list.sed | sed-e's /^//'
Save this script as test.sh and run it.
$echo'Tokyo Suginami Ward Asagaya 1-2-3'|./test.sh
Asagaya 1-2-3
$
You can see the output:
I tried Awk.
$INPUT='Asagaya 1-2-3 in Suginami Ward, Tokyo'
$ awk-vinput="$INPUT"'
input~"^"$0{
sub("^"$0"[\t]*", "", input)
exit
}END {print input}
' hoge_list
Asagaya 1-2-3
If only the bash function can be realized, I think it would be like the following.
#!/bin/bash
INPUT="Asagaya 1-2-3 in Suginami Ward, Tokyo"
shopt-extglob
list = $(<hoge_list)
pattern=${list//$'\n'/|}
echo${INPUT#@($pattern)}
© 2024 OneMinuteCode. All rights reserved.