简体   繁体   English

检查行是否以数字开头,然后使用Bash将这些行放在单独的文件中

[英]Check if line starts with digit then put those lines in a separate file using Bash

I would like to check in bash if line starts with digit then put those lines in a separate file. 我想检查bash如果行以数字开头,然后将这些行放在单独的文件中。 I tried ^[[0-9]] but it doesn't work. 我尝试了^[[0-9]]但是它不起作用。

Here is the code which I tried: 这是我尝试的代码:

myFailedFile=/tmp/1.txt

myTestsFile:
177711 TESTING ...yahoo.tests.calendar.resources.scheduler.1
854756 TESTING ...yahoo.tests.calendar.resources.scheduler.2
* 2102637 DONE ...yahoo.tests.mail.contacts.add.3

while read line
do
    if ( [[ ${line} = "^[[0-9]]"* ]] && [[ ${line} = *".tests."* ]] ); then
        echo -e "${line}\r" >> ${myFailedFile}
    fi
done <"${myTestsFile}"

Expected output of myFailedFile:
177711 TESTING ...yahoo.tests.calendar.resources.scheduler.1
854756 TESTING ...yahoo.tests.calendar.resources.scheduler.2

The correct operator to use regex in Bash script is =~ . 在Bash脚本中使用正则表达式的正确运算符是=~ Moreoever you don't need to double [ in range of characters. 此外,您无需在字符范围内加倍[ Try this: 尝试这个:

while read line
do
    if ( [[ ${line} =~ ^[0-9] ]] && [[ ${line} = *".tests."* ]] ); then
        echo -e "${line}\r" >> ${myFailedFile}
    fi
done <"${myTestsFile}"

Edit : 编辑

But you don't need a Bash loop for that job. 但是您不需要该工作的Bash循环。 You can do it with a sed one-liner: 您可以使用单线sed做到这一点:

sed '/^[0-9].*\.tests\./!d' "${myTestsFile}" > myFailedFile

Explanations (from right to left): 说明 (从右到左):

  • !d : do not delete !d :不删除
  • /^[0-9].*\\.tests\\./ : all lines that start with one or more digits and that contain .tests. /^[0-9].*\\.tests\\./ :所有以一位或多位数字开头并且包含.tests. string

Without using regex you can use glob as this: 无需使用正则表达式,您可以按以下方式使用glob

[[ $line = [0-9]* ]] && [[ $line = *".tests."* ]]
  • [0-9]* matches a string start start with digits [0-9]*匹配以数字开头的字符串
  • *".tests."* matches a string that contains .tests. *".tests."*与包含.tests.的字符串匹配.tests.

you can use 您可以使用

if [[ ${line} =~ ^[0-9] && ${line} =~ ".tests." ]] ; then

or 要么

if [[ ${line} == [0-9]* && ${line} == *".tests."* ]] ; then

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用bash脚本检查“ {”是否在Java文件的单独行中开始 - How to check if “{” begins in separate line in java file using bash script 使用 xargs,对于文件中的每一行,计算这些行的出现次数 - Using xargs, for each line in file count the occurrences of those lines another 如何在文件中搜索多行并在bash / dash中注释这些行 - how to search for multiple lines in a file and comment those lines in bash/dash 如何在Linux bash中将相邻行放在.txt文件中的同一行上? - How do I put adjacent lines in a .txt file on the same line in linux bash? 将文本文件的行放入bash中的数组中 - Put lines of a text file in an array in bash 使用文件路径中的通配符检查文件路径是否存在的 Bash 行 - Bash line to check if a file path exists using wildcards in filepath 如何在多行中分隔一行Bash脚本? - How Do I Separate a Line of Bash Script Over Multiple Lines? 在行列表中的行号之间打印行,并使用GNU Parallel将每个实例保存在单独的文件中 - Print lines between line numbers from a line list and save every instance in separate file using GNU Parallel 检查bash文件的前N行是否包含字符串 - Check first N lines of bash file for string 使用 Bash 检查数字是单数、双数还是三位数 - Check whether a number is single, double, or triple digit using Bash
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM