尝试在shell脚本中grep / filter文件名

时间:2018-03-29 15:03:30

标签: bash shell sh

我正在尝试使用grep过滤文件名。我将这些文件名作为输入:

1234.txt
1234.txt.dev1
123.doc1
123.txt.dev1
123.txt.dev2
234.jpg
456.php
tmp
tmppppp

我想只列出文件名中有一个.(点/周期)的文件。文件名和扩展名会有所不同,但不是固定的。

预期结果:

1234.txt
123.doc1
234.jpg
456.php

我可以用grep吗?

2 个答案:

答案 0 :(得分:2)

与per'ls正则表达式开关一起使用:

grep -P '^[^.]+(?<!\.)[^.]+\.[^.]+$' file

demo and explanations

或简单地(归功于@gniourf_gniourf):

grep -E '^[^.]+\.[^.]+$' file

demo and explanations

使用的另一种方式:

awk '{split($0, a, ".")} length(a) == 2' file

awk '!/^\./ && !/\.$/{split($0, a, ".")} length(a) == 2' file

如果您要跳过以.

开头或结尾的文件

输出:

1234.txt
123.doc1
234.jpg
456.php

答案 1 :(得分:-3)

只使用a过滤文件名。你可以使用:

theta = np.random.standard_normal((64, 4))
xs = np.zeros((available_states))
xs[some_index] = 1

def pi(xs, theta):
    H_s_a_Theta = np.matmul(tf.transpose(theta), xs) 
    softmax = [scipy.exp(x) for x in H_s_a_Theta]
    sum = np.sum(softmax)

    return softmax / sum

first_derv_fn = tfe.gradients_function(pi, params = 'theta') #xs:0, theta:1 
secon_derv_fn = tfe.gradients_function(first_derv_fn, params = 'theta')

  cat file.txt | grep '\.'

  grep '\.' file.txt