问答 shell 脚本问题求指教

嘎哈 · 2017年03月19日 · 最后由 嘎哈 回复于 2017年04月11日 · 1472 次阅读

背景

本想每天刷一道 leetcode,保持学习,但第一道就被困住了
题目如下

Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bash script to print all valid phone numbers.

You may assume that a valid phone number must appear in one of the following two formats: (xxx) xxx-xxxx or xxx-xxx-xxxx. (x means a digit)

You may also assume each line in the text file must not contain leading or trailing white spaces.

For example, assume that file.txt has the following content:

987-123-4567
123 456 7890
(123) 456-7890
Your script should output the following valid phone numbers:
987-123-4567
(123) 456-7890

我的解法

#!/bin/bash
pattern1="^[0-9]{3}-[0-9]{3}-[0-9]{4}$"
pattern2="^\([0-9]{3}\) [0-9]{3}-[0-9]{4}$"

cat ./file.txt | while read line
do
#echo "ori=$line"
        if [[ $line =~ $pattern1 ]] || [[ $line =~ $pattern2 ]] ; then
                echo $line
        fi
done

结果是一个都没匹配出来

查找问题的时候,尝试改成

#!/bin/bash
pattern1="^[0-9]{3}-[0-9]{3}-[0-9]{4}$"
pattern2="^\([0-9]{3}\)\s[1][0-9]{3}-[0-9]{4}$"

cat ./file.txt | while read line
do

echo "ori=$line"
        if [[ $line =~ ^[0-9]{3}-[0-9]{3}-[0-9]{4}$ ]] || [[ $line =~ ^\([0-9]{3}\)' '[0-9]{3}-[0-9]{4}$ ]] ; then
                echo $line
        fi
done

输出符合期望,如果在正则表达式附近加上双引号,则一个都匹配不出来,便以为表达式不可以用字符串的形式,于是修改脚本为

#!/bin/bash
pattern1=^[0-9]{3}-[0-9]{3}-[0-9]{4}$
pattern2=^\([0-9]{3}\)' '[0-9]{3}-[0-9]{4}$

cat ./file.txt | while read line
do

echo "ori=$line"
        if [[ $line =~ ${pattern1} ]] || [[ $line =~ ${pattern2} ]] ; then
                echo $line
        fi
done

发现只有第一个正则表达式 work 了,第二个依然没有匹配出来

那么如果我想用变量的方式让第二个正则表达式 work,该怎么修改?

共收到 7 条回复 时间 点赞

注意换行符

给出一种方案:
awk '/\[0-9]{3}) |[0-9]{3}-)[0-9]{3}-[0-9]{4}$/ {print $0}' file.txt

建议把不符合的输入内容也提供出来,方便使用你的代码调试。

话题中一共给了 3 个方法,经在 leetcode.com 网站上验证和测试,
方法 1 可行;
方法 2 注释掉echo "ori=$line"后也可行;
方法 3:可以用引号括起来,但空格两边的单引号需要去掉。

#!/bin/bash
pattern1="^[0-9]{3}-[0-9]{3}-[0-9]{4}$"
pattern2="^\([0-9]{3}\) [0-9]{3}-[0-9]{4}$"

cat ./file.txt | while read line
do

# echo "ori=$line"
        if [[ $line =~ ${pattern1} ]] || [[ $line =~ ${pattern2} ]] ; then
                echo $line
        fi
done

楼上正解, 方法 2 echo "ori=$line" 是否注释似乎不影响吧。

呆若木吉 回复

对于完成一个具体的工作来说,echo 是一条调试信息。但对于这个题来说,echo 就影响了最终的结果,没有必要打印无关的内容。

stack 回复

我之前用方法一在本地调试一直过不了,所以后来才会把表达式改的这么诡异,相同的输入用方法二就可以过,所以没有怀疑环境本身;刚用 leetcode 试了一下方法一,确实过了,看来我给在本地找找原因了,3Q~

嘎哈 回复

由于自己本身工作中涉及的都是 java,所以工作中也是在用非脚本语言的思维写脚本,在 solutions 里看到了很多类似的一句话脚本,感觉挺开心的,哈哈

需要 登录 後方可回應,如果你還沒有帳號按這裡 注册