-1

如何在我的 csv 文件中找到以 $ 开头的所有单词

我的文件是这样的:

Test1,$Var1,$varCab1,$Vargab1,Comment1
Test2,$Var2,$varCab2,$Vargab2,Comment2
Test3,$Var3,$varCab3,$Vargab3,Comment3

作为我想要的输出

$Var1
$varCab1
$Vargab1
$Var2
$varCab2
$Vargab2
$Var3
$varCab3
$Vargab3
4

3 回答 3

1

尝试以下(grep -oE '\$\w+' filename):

$ cat 1.csv
Test1,$Var1,$varCab1,$Vargab1,Comment1
Test2,$Var2,$varCab2,$Vargab2,Comment2
Test3,$Var3,$varCab3,$Vargab3,Comment3
$ grep -oE '\$\w+' 1.csv
$Var1
$varCab1
$Vargab1
$Var2
$varCab2
$Vargab2
$Var3
$varCab3
$Vargab3

使用awk

$ awk -F, '{ for(i=1;i<=NF;i++) if ($i ~ /\$/) print $i; }' 1.csv
$Var1
$varCab1
$Vargab1
$Var2
$varCab2
$Vargab2
$Var3
$varCab3
$Vargab3
于 2013-08-27T06:40:12.457 回答
0

Using perl:

perl -ne 'for (m/\$\w+/g) { print $_, "\n" }' < inputfile

Or even shorter:

perl -ne 'print map("$_\n", m/\$\w+/g)' < inputfile

Explanation:

  • The regular expression \$\w+ matches a $ followed by one or more word characters.
  • The m//g expression returns a list of matches.
  • perl -ne runs the expression for each file in the input, inserting the line in $_, which is then used by the m//g expression.
于 2013-08-28T17:00:57.287 回答
0

使用trgrep

$ tr ',' '\n' < inputfile | grep "^[$]"
$Var1
$varCab1
$Vargab1
$Var2
$varCab2
$Vargab2
$Var3
$varCab3
$Vargab3
于 2013-08-27T07:08:21.650 回答