1

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

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

例如:

First. [second] Third.
      ^        ^

我想将文件的行排序为两个文件,withnumbers.csv并且withoutnumbers.csv本质上是由第三列中包含数字的行和第三列中不包含数字的行排序。

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

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.]

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

4

3 回答 3

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
于 2012-05-15T01:34:39.607 回答
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
于 2012-05-15T02:10:07.130 回答
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
于 2012-05-15T05:58:14.467 回答