[英]Find and replace - simple bash script
我对bash脚本并不熟悉,但假设我有一个文件textfile.txt
,其名称和邮件由几行组成,其中包含几个这些模式的出现次数:
name@surname.net;othername.othersurname;name@surname.net;othername.othersurname;name@surname.net;...
我想从这个列表中删除所有不是邮件的条目。 所以假设我的可执行文件是file.sh,我运行sh file.sh textfile.txt
#!/bin/bash
if [–f $1];
awk -F ";" '{//here comes what I am looking for
}' $1
else
echo "there is no such file"
fi
我不知道我可以使用哪种语法获取最后一个已过滤的条目(以检查是否没有@
符号从列表中删除它)。 我试图谷歌但没有成功
我不知道awk
遗憾,但你可以用Perl做
perl -p -e 's/;[^;@]+;/;/g'
但是它有一个错误,如果该行中的第一个或最后一个条目是无效的电子邮件,它将会错过它。 要正确解决这些问题,您需要拆分/检查/加入,它开始变得混乱为一条线
perl -p -e 'join(";",grep(/@/,split(";",$_)))'
编辑:糟糕,抱歉,从ideone切换到命令行时出错。 我错过了返回$_
的任务,这是由-p
打印的
perl -p -e '$_ = join(";",grep(/@/,split(";",$_)))'
split(";",$_)
将当前行( $_
)拆分为元素数组;
作为分隔符。 grep(/@/,...)
然后只返回包含@
的数组元素。 这是我对有效电子邮件地址的简单测试。 如果你想要更多,你可以使用更严格的正则表达式的电子邮件地址。 也许/^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$/
join(";"...)
将有效的电子邮件地址重新组合成一个;
分隔字符串。 这是在没有awk或perl的bash脚本中执行此操作的一种方法...
origfile=$1
copyfile=`mktemp`
for email in `sed 's/;/\n/g' $origfile | grep "@"`; do
printf "$email;" >> $copyfile
done
#you may want to check that $copyfile is valid in some way before the next step
mv $copyfile $origfile
这是一个awk
解决方案。 但只有awk
,所以我不建议将它包含在shell脚本中。 它应该从命令行运行它:
awk '
## Split (in input) and join (in output) fields with colon.
BEGIN { FS = OFS = ";" }
{
## Traverse all fields and delete those that do not contain one "@".
for ( i = 1; i <= NF; i++ ) { if ( index( $i, "@" ) == 0 ) { $i = "" } }
## There will be some consecutive colons between those fields deleted.
## Keep only one.
gsub( /;{2,}/, ";" )
## Print the whole line only with emails.
print
}
' infile
使用您的示例行,它提供:
name@surname.net;name@surname.net;name@surname.net
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.