bash sed - 删除破折号前的所有字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16201134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:37:20  来源:igfitidea点击:

sed - delete all characters before dash

bashsedawk

提问by scripting_newbie

I have list of filenames for which I want to remove all character before the first instance of -. So the names below in the Before: list appears as those in the After: list.

我有一个文件名列表,我想在第一个实例之前删除所有字符-。因此,Before: 列表中的以下名称显示为 After: 列表中的名称。

Before:
Adam James - Welcome Home.txt
Mike & Harry - One Upon - A Time.txt
William-Kent - Prince & The Frog.txt

After:
Welcome Home.txt
One Upon - A Time.txt
Prince & The Frog.txt

I've been playing around with sed for hours with no avail.

我一直在玩 sed 几个小时但无济于事。

I found that sed 's/ - .*//'removes all characters after the first instance of -but I cannot find the same for before.

我发现sed 's/ - .*//'在第一个实例之后删除了所有字符,-但我之前找不到相同的字符。

采纳答案by anubhava

Using awk you can do:

使用 awk,您可以执行以下操作:

awk 'BEGIN{FS=OFS="- "} NF>1{="";sub(/^- */, "")}'1 inFIle

Live Demo: http://ideone.com/2tBU4v

现场演示:http: //ideone.com/2tBU4v

回答by pdw

Like this.

像这样。

sed 's/^[^-]* - //'

Many regular expression engines allow *?for a non-greedy search, but sed doesn't.

许多正则表达式引擎允许*?非贪婪搜索,但 sed 不允许。

EDIT: This won't change the William-Kent example, the embedded hyphen prevents a match.

编辑:这不会改变 William-Kent 示例,嵌入的连字符会阻止匹配。

(Also, Perl ships a very handy renamescript to batch-rename files using regular expressions, but not every distribution installs it.)

(此外,Perl 提供了一个非常方便的rename脚本来使用正则表达式批量重命名文件,但并非每个发行版都安装了它。)

回答by Ed Morton

$ awk '{print substr(
$ grep -Po ' - \K.*' file
Welcome Home.txt
One Upon - A Time.txt
Prince & The Frog.txt
,index(
awk -v FPAT="- .*$" 'sub(/^- /,"",)' file
," - ")+3)}' file Welcome Home.txt One Upon - A Time.txt Prince & The Frog.txt

i.e. just print from the end of the first occurrence of " - " to the end of the line.

即只是从第一次出现的“-”的末尾打印到行的末尾。

回答by Chris Seymour

Simplest approach is too use grep:

最简单的方法太用了grep

kent$  echo "Adam James - Welcome Home.txt
Mike & Harry - One Upon - A Time.txt
William-Kent - Prince & The Frog.txt"|awk -v FPAT="- .*$" 'sub(/^- /,"",)'
Welcome Home.txt
One Upon - A Time.txt
Prince & The Frog.txt

回答by Kent

I think gawkmay be easier for this job.

我认为gawk这份工作可能会更容易一些。

the usage of FPATcould simplify the problem:

的使用FPAT可以简化问题:

list=('Adam James - Welcome Home.txt' 'Mike & Harry - One Upon - A Time.txt')
for str in "${list[@]}"; do echo ${str#*-}; done

with your data:

使用您的数据:

Welcome Home.txt
One Upon - A Time.txt

回答by bobah

In pure Bash (no extra child processes spawned):

在纯 Bash 中(没有产生额外的子进程):

$ cat the_file_names 
Adam James - Welcome Home.txt
Mike & Harry - One Upon - A Time.txt
William-Kent - Prince & The Frog.txt

$ { while read FN; do echo ${FN#* - }; done; } < /tmp/the_file_names 
Welcome Home.txt
One Upon - A Time.txt
Prince & The Frog.txt

$ 

Prints:

印刷:

 sed 's/ - /\n/;s/.*\n//' file

回答by pilcrow

This can be done in plain POSIX sh, without bash features:

这可以在没有 bash 功能的普通 POSIX sh 中完成:

##代码##

回答by potong

This might work for you (GNU sed):

这可能对你有用(GNU sed):

##代码##