芭比哇玩123 发表于 2017-6-9 13:42:14

shell 脚本问题求指教

背景
本想每天刷一道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="^{3}-{3}-{4}$"
pattern2="^\({3}\) {3}-{4}$"

cat ./file.txt | while read line
do
#echo "ori=$line"
      if [[ $line =~ $pattern1 ]] || [[ $line =~ $pattern2 ]] ; then
                echo $line
      fi
done结果是一个都没匹配出来查找问题的时候,尝试改成
#!/bin/bash
pattern1="^{3}-{3}-{4}$"
pattern2="^\({3}\)\s{3}-{4}$"

cat ./file.txt | while read line
do

echo "ori=$line"
      if [[ $line =~ ^{3}-{3}-{4}$ ]] || [[ $line =~ ^\({3}\)' '{3}-{4}$ ]] ; then
                echo $line
      fi
done输出符合期望,如果在正则表达式附近加上双引号,则一个都匹配不出来,便以为表达式不可以用字符串的形式,于是修改脚本为
#!/bin/bash
pattern1=^{3}-{3}-{4}$
pattern2=^\({3}\)' '{3}-{4}$

cat ./file.txt | while read line
do

echo "ori=$line"
      if [[ $line =~ ${pattern1} ]] || [[ $line =~ ${pattern2} ]] ; then
                echo $line
      fi
done
发现只有第一个正则表达式work了,第二个依然没有匹配出来
那么如果我想用变量的方式让第二个正则表达式work,该怎么修改?



测试就是来开荒 发表于 2017-6-9 14:47:23

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

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

测试的味道 发表于 2017-6-9 14:48:49

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

cat ./file.txt | while read line
do

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

小爸爸 发表于 2017-6-9 14:49:23

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

测试的味道 发表于 2017-6-9 14:50:12

小爸爸 发表于 2017-6-9 14:49
楼上正解, 方法2 echo "ori=$line" 是否注释似乎不影响吧。

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

小皮球的故事 发表于 2017-6-9 14:51:06

我之前用方法一在本地调试一直过不了,所以后来才会把表达式改的这么诡异,相同的输入用方法二就可以过,所以没有怀疑环境本身;刚用leetcode试了一下方法一,确实过了,看来我给在本地找找原因了,3Q~
页: [1]
查看完整版本: shell 脚本问题求指教