Sed 如何在文件中搜索位置12处的字符,如果找到,则从所有行中删除该字符和接下来的5个字符?

Sed 如何在文件中搜索位置12处的字符,如果找到,则从所有行中删除该字符和接下来的5个字符?,sed,cut,Sed,Cut,我试图解析RSS提要并压缩行上的信息,这样我仍然有条目的日期和时间,但没有百万分之一秒或浪费空间,因为我将文件提供给限制在可读屏幕宽度上的xscreensaver文本爬网。我可以修改我的代码,在文本格式化之前不添加两行标题行,如果这样做更容易的话。谢谢你的建议 The input file at this point looks like this: ABC World News Feed RSS Data retrieved from https:--abcnews.go.com-abcne

我试图解析RSS提要并压缩行上的信息,这样我仍然有条目的日期和时间,但没有百万分之一秒或浪费空间,因为我将文件提供给限制在可读屏幕宽度上的xscreensaver文本爬网。我可以修改我的代码,在文本格式化之前不添加两行标题行,如果这样做更容易的话。谢谢你的建议

The input file at this point looks like this:

ABC World News Feed
RSS Data retrieved from https:--abcnews.go.com-abcnews-headlines
05-24 18:48:16    Truckers' strike leads to fuel shortages in Brazil
05-24 18:48:16    The marathon atop the world's deepest lake
           ^^^^^^
           Remove these character positions starting from 12 to 17 
           from each title line, with colon in 12 but not from the heading lines

So the result should look like:

ABC World News Feed
RSS Data retrieved from https:--abcnews.go.com-abcnews-headlines
05-24 18:48 Truckers' strike leads to fuel shortages in Brazil
05-24 18:48 The marathon atop the world's deepest lake

我的做法是用一个空格替换冒号后跟两个数字,后跟至少一个空格:

$ sed 's/:[[:digit:]][[:digit:]]  */ /' file
ABC World News Feed
RSS Data retrieved from https:--abcnews.go.com-abcnews-headlines
05-24 18:48 Truckers' strike leads to fuel shortages in Brazil
05-24 18:48 The marathon atop the world's deepest lake
如果您想真正明确位置,可以将带有“^”的搜索定位到行的开头,并使用带反引用\1的括号。这是圆点。匹配任意字符:

$ sed 's/^\(..-.. ..:..\):[[:digit:]][[:digit:]]  */\1 /' file
ABC World News Feed
RSS Data retrieved from https:--abcnews.go.com-abcnews-headlines
05-24 18:48 Truckers' strike leads to fuel shortages in Brazil
05-24 18:48 The marathon atop the world's deepest lake
以下awk可能对您有所帮助

awk '$2 ~ /[0-9]+:[0-9]+:[0-9]+/{sub(/:[0-9]+ +/,OFS)} 1'  Input_file
若要将输出保存到输入文件本身,请在上述命令中附加>temp\u file&&mv temp\u file Input\u file

说明:也在此处添加说明

awk '
$2 ~ /[0-9]+:[0-9]+:[0-9]+/{ ##Checking condition here if 2nd field is matching digit colon digit colon digit pattern then do following.
  sub(/:[0-9]+ +/,OFS)       ##Using substitute function of awk to substitute colon digit(s) then space with OFS whose default value is space in current line.
}
1                            ##awk works on method of condition and then action, so making condition TRUE here and not mentioning action so print will happen.
' Input_file                 ##Mentioning Input_file name here.

到目前为止你们都做了些什么?帮助观众的建议:避免长线,避免水平滚动条。读起来很痛苦。这段代码也起了作用。您可以选择“全部”并粘贴到我找到的Geany中,这样评论就更具可读性了。@BobC,酷,很高兴它帮助了您,您可以投票选出那些帮助您鼓励人们、欢呼和快乐学习的答案!!我使用了第二个版本,效果非常好。我需要更多的阅读和游戏,因为我不理解背景资料。我会去查的,谢谢。PS:抱歉,不允许向上投票。@BobC类似\1的反向引用表示第一对括号匹配的部分。您还可以对第二个、第三个、。。。一对您可以通过单击左侧的复选符号来接受对您帮助最大的答案,这将为您带来另外2个声誉。