The content of the file.txt text is as follows
domain_wisgene.com keyword谷歌搜索 - This,is,keyword,google,search,awesome title谷歌搜索 - Google,Search,is,Awesome
code show as below
while IFS= read -r -d '' now_list_url now_list_keyword now_list_title; do
# Do something with the variables here
echo "domain_name:${now_list_url}"
echo "Key words:${now_list_keyword}"
echo "title:${now_list_title}"
done <file.txt
result
#domain
domain_name:domain_wisgene.com
#keywords
Key words:keyword谷歌搜索
#title
title:- This,is,keyword,google,search,awesome title谷歌搜索 - Google,Search,is,Awesome
wrong result
#domain
domain_name:domain_wisgene.com
#keywrods
Key words:keyword谷歌搜索 - This,is,keyword,google,search,awesome
#title
title:- This,is,keyword,google,search,awesometitle 谷歌搜索 - Google,Search,is,Awesome
I want the correct result
#domain
domain_name:domain_wisgene.com
#keywords
Key words:keyword谷歌搜索 - This,is,keyword,google,search,awesome
#title
title:title谷歌搜索 - Google,Search,is,Awesome"
Looking for help How do I solve this complicated delimiter problem, I want to use the default space as the delimiter, but there are also spaces in the variable value characters, how do I deal with the " - " in the variable without delimiting?
Example: This is one of the lines in file.txt
domain_wisgene.com keyword Google Search - This,is,keyword,google,search,awesome title Google Search - Google,Search,is,Awesome
Pass variable 1 variable 2 variable 3
Variable 1 = domain_wisgene.com
Variable 2 = keyword google search - This,is,keyword,google,search,awesome
Variable 3 = title Google Search - Google,Search,is,Awesome`
I want the correct result
domain_name:domain_wisgene.com
Key words:keyword谷歌搜索 - This,is,keyword,google,search,awesome
title:title谷歌搜索 - Google,Search,is,Awesome
CodePudding user response:
Your delimiter needs are too complex for read. You can maybe use sed to separate the fields like so:
IFS=$'\n' read -rd '' now_list_url now_list_keyword now_list_title < <(sed -E 's/^(<domain-regex>)\s (<keywords-regex>)\s (<title-regex>)$/\1\n\2\n\3/g' file.txt)
Explanation:
- sed replaces \1, \2, and \3 with the text that 1st, 2nd, and 3rd parentheses-wrapped expressions captured.
- I capture the 3 variables then write them again with a newline delimiter. Now they are in a format suitable for read. You can replace the newline with whatever delimiter you know won't be in the input (personally I am fond of
\x1F
, the Unit Separator). - \s is shorthand for any whitespace character.
- You will have to fill in the
<regex>
s yourself as I don't know what the general rule is that you want.
CodePudding user response:
@Verpous First of all thank you very much, this is how I operate
while IFS=$'\n' read -rd '' now_list_url now_list_keyword now_list_title < <(sed -E 's/^(<domain-regex>)\s (<keywords-regex>)\s (<title-regex>)$/\1\n\2\n\3/g' ${_site_dns_com_file}); do
#while read -r now_list_url now_list_keyword now_list_title; do
_deliver_site_dns_com_url="${now_list_url}" #url
_deliver_site_dns_com_url_keyword="${now_list_keyword}" #keyword
_deliver_site_dns_com_url_title="${now_list_title}" #title
sleep 0.02
echo "url:${now_list_url}"
echo "keyword:${now_list_keyword}"
echo "title:${now_list_title}"
done
Can you write a complete example in code? Because tried using your way, there is no output