如何将符合特定条件的行分类到单独的文件中?

时间:2012-05-15 00:51:35

标签: bash sorting grep

我有一个CSV文件file1.csv,它有一个自定义格式,有三列,如下所示:

This is some data. [text] This is some more data.
  • 第一个[之前的所有内容都位于第一列。
  • 第一组方括号之间的所有内容都位于第二列。
  • 第一列]之后的所有内容都在第三列中,无论后面是什么内容。

E.g:

First. [second] Third.
      ^        ^

我想将文件的行分为两个文件withnumbers.csvwithoutnumbers.csv,基本上是由第三列中包含数字的文件和第三列中不包含数字的文件。< / p>

可能会出现后面的方括号,但它们不被视为新列,它们仍然是第三列数据的一部分,例如:

First. [second] Third. [some more text] This is still in the third column.
      ^        ^

包含数字的行可以与*0**1**2*等匹配。这些都包含数字:

Water is H20.
The bear ate 2,120 fish.
The Wright Flyer flew in 1903.

在第三列的一对方括号内找到的数字不算作匹配,例如,这些行将被发送到withoutnumbers.csv

First. [second] Some text. [This has the number 1.]
First. [second] Some more text. [The Wright Flyer flew in 1903.]

这些会被发送到withnumbers.csv,因为它们仍然在方括号外面有一个数字,但在第三列内:

First. [second] Some text with 1. [This has the number 1.]
First. [second] Some more text with the number 3. [The Wright Flyer flew in 1903.]

如何将文件行排序到第三列中包含数字的行,不考虑方括号内的数字,以及那些不包含数字的行?

3 个答案:

答案 0 :(得分:3)

嗯,我不会撒谎,我不喜欢我提出的解决方案。然而,你的问题是相当特殊的,绝望的时候需要采取绝望的措施。所以,试一试:

awk -F'\[[^\]]*\]' '{
  printed = 0
  for (i = 2; i <= NF; i++) {
    if ($i ~ /[0-9]+/) {
      print $0 >> "withNumbers"
      printed = 1
      break
    }
  }

  if (! printed) {
    print $0 >> "withoutNumbers"
  }
}' file

答案 1 :(得分:1)

这是一个去

shopt -s extglob
rm withnumbers.csv withoutnumbers.csv
touch withnumbers.csv withoutnumbers.csv

while IFS= read -r line; do
  col3=${line#*\]}            # remove everything before and including the first ]
  col3=${col3//\[*([^]])\]/}  # remove all bracketed items
  if [[ $col3 == *[[:digit:]]* ]]; then
    printf "%s\n" "$line" >> withnumbers.csv
  else
    printf "%s\n" "$line" >> withoutnumbers.csv
   fi
done < file1.csv

答案 2 :(得分:1)

这会在第一个右方括号上拆分,并检查第一个右方括号后的行部分中方括号内的数字,或者该部分是否仅包含非数字。它将这些行写入withoutnumbers.csv。否则,它将该行写入withnumbers.csv。

perl -lne 'BEGIN {open ND, ">", withoutnumbers.csv; open D, ">", withnumbers.csv} @fields = split(/]/,$_,2); $fields[1] =~ /\[.*?\d.*?\]|^\D+$/ ? print ND $_ : print D $_' file1.csv