![](/img/trans.png)
[英]Trying to split a string in Java, containing newlines, !, spaces, ?, commas, etc
[英]How to split a string or file that may be delimited by a combination of comments and spaces, tabs, newlines, commas, or other characters
如果file:list.txt包含如此丑陋的数据:
aaaa
#bbbb
cccc, dddd; eeee
ffff;
#gggg hhhh
iiii
jjjj,kkkk ;llll;mmmm
nnnn
我们如何解析/拆分该文件,不包括注释行,用所有逗号,分号和所有空格(包括制表符,空格,换行符和carrage-return字符)用bash脚本分隔?
可以使用以下代码完成:
#!/bin/bash
### read file:
file="list.txt"
IFSO=$IFS
IFS=$'\r\n'
while read line; do
### skip lines that begin with a "#" or "<whitespace>#"
match_pattern="^\s*#"
if [[ "$line" =~ $match_pattern ]];
then
continue
fi
### replace semicolons and commas with a space everywhere...
temp_line=(${line//[;|,]/ })
### splitting the line at whitespaces requires IFS to be set back to default
### and then back before we get to the next line.
IFS=$IFSO
split_line_arr=($temp_line)
IFS=$'\r\n'
### push each word in the split_line_arr onto the final array
for word in ${split_line_arr[*]}; do
array+=(${word})
done
done < $file
echo "Array items:"
for item in ${array[*]} ; do
printf " %s\n" $item
done
这并不是一个问题,而是一个更好的解决方案,以解决其他人在回答其他相关问题时所提到的问题。 这里唯一的一点就是那些其他问题/解决方案并没有真正解决如何在用空格,字符和注释的组合分隔字符串时拆分字符串; 这是一个同时解决所有三个问题的解决方案......
相关问题:
如何将一个字符串拆分为多个字符串,由bash shell中的至少一个空格分隔?
补充笔记:
当其他脚本语言更适合拆分时,为什么使用bash? 与perl程序相比,bash脚本更有可能拥有从基本的upstart或cron(sh)shell运行时所需的所有库。 在这些情况下经常需要一个参数列表,我们应该期待维护这些列表的人的最坏情况......
希望这篇文章将在未来很多时间(包括我)节省bash新手...祝你好运!
使用shell命令:
grep -v "^[ |\t]*#" file|tr ";," "\n"|awk '$1=$1'
sed 's/[# \\t,]/REPLACEMENT/g' input.txt
上面的命令用任意字符串( 'REPLACEMENT'
)替换注释字符( '#'
),空格( ' '
),制表符( '\\t'
)和逗号( ','
'REPLACEMENT'
)
要替换换行符,您可以尝试:
sed 's/[# \\t,]/replacement/g' input.txt | tr '\\n' 'REPLACEMENT'
如果你的系统上有Ruby
File.open("file").each_line do |line|
next if line[/^\s*#/]
puts line.split(/\s+|[;,]/).reject{|c|c.empty?}
end
产量
# ruby test.rb
aaaa
cccc
dddd
eeee
ffff
iiii
jjjj
kkkk
llll
mmmm
nnnn
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.