删除逗号前一行中的所有内容 [英] Remove everything in a line before comma

查看:82
本文介绍了删除逗号前一行中的所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个文件,行如下:

I have multiple files with lines like:

foo, 123456
bar, 654321
baz, 098765

我想删除每行之前(包括)逗号的所有内容.输出将是:

I would like to remove everything on each line before (and including) the comma. The output would be:

123456
654321
098765

在另一个问题上看到类似的内容后,我尝试使用以下内容,但用户没有留下解释,所以我不确定如何处理通配符:

I attempted to use the following after seeing something similar on another question, but the user didn't leave an explanation, so I'm not sure how the wildcard would be handled:

find . -name "*.csv" -type f | xargs sed -i -e '/*,/d'

感谢您提供的任何帮助.

Thank you for any help you can offer.

推荐答案

方法一:如果它总是你想要的 2nd 列,你可以用 awk 来做到这一点——这个命令实际上是在空格而不是逗号上分割行,所以它得到你的第二列——数字,但没有前导空格:

METHOD 1: If it's always the 2nd column you want, you can do this with awk -- this command is actually splitting the rows on the whitespace rather than the comma, so it gets your second column -- the numbers, but without the leading space:

awk '{print $2}' < whatever.csv

方法 2:或者获取逗号之后的所有内容(包括空格):

METHOD 2: Or to get everything after the comma (including the space):

sed -e 's/^.*,//g' < whatever.csv

方法 3:如果您想找到所有 .csv 文件并将所有这些文件的输出放在一起,您可以这样做:

METHOD 3: If you want to find all of the .csv files and get the output of all of them together, you can do:

sed -e 's/^.*,//g' `find . -name '*.csv' -print`

方法 4:或者与您开始使用的方式相同 - 使用 findxargs:

METHOD 4: Or the same way you were starting to -- with find and xargs:

find . -name '*.csv' -type f -print | xargs sed -e 's/^.*,//'

方法 5:把所有的.csv文件做成.txt文件,按照上面的方法处理,就可以制作一个简短的shell脚本了.像这样:

METHOD 5: Making all of the .csv files into .txt files, processed in the way described above, you can make a brief shell script. Like this:

创建一个脚本bla.sh":

#!/bin/sh
for infile in `find . -name '*.csv' -print` ; do
        outfile=`echo $infile | sed -e 's/.csv/.txt/'`
        echo "$infile --> $outfile"

        sed -e 's/^.*,//g' < $infile > $outfile
done

输入以下内容使其可执行:

Make it executable by typing this:

chmod 755 bla.sh

然后运行它:

./bla.sh

这将创建一个 .txt 输出文件,其中包含每个 .csv 输入文件的逗号后的所有内容.

This will create a .txt output file with everything after the comma for each .csv input file.

替代方法 5:或者,如果您需要将它们命名为 .csv,则可以像这样更新脚本——这只会为每个文件生成一个名为file-new.csv"的输出文件名为file.csv"的输入文件:

ALTERNATE METHOD 5: Or if you need them to be named .csv, the script could be updated like this -- this just makes an output file named "file-new.csv" for each input file named "file.csv":

#!/bin/sh
for infile in `find . -name '*.csv' -print` ; do
    outfile=`echo $infile | sed -e 's/.csv/-new.csv/'`
    echo "$infile --> $outfile"

    sed -e 's/^.*,//g' < $infile > $outfile
done

这篇关于删除逗号前一行中的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆