简体   繁体   English

如何避免文件中所有行的开头出现特定的字符串?

[英]How can I avoid particular string from beginning of all the lines in a file?

I have a file, the contents are like below, 我有一个文件,内容如下

    /hello/dir1/dir2/remove_til_here/code/dira/dir1/a.c
    /hello/dir1/dir2/remove_til_here/code/dirb/dir7/b.c
    /hello/dir1/dir2/remove_til_here/code/dirc/dir3/c.c
    /hello/dir1/dir2/remove_til_here/code/dird/dir2/d.c
    ......

I want o/p as below: 我要如下所示的o / p:

code/dira/dir1/a.c
code/dirb/dir7/b.c
code/dirc/dir3/c.c
code/dird/dir2/d.c

I want to avoid string /hello/dir1/dir2/remove_stil_here/ from all the line. 我想避免所有/hello/dir1/dir2/remove_stil_here/字符串/hello/dir1/dir2/remove_stil_here/ Please shed some light on this. 请对此有所说明。 I have tried with cat file | awk '{print $5}' 我已经尝试过cat file | awk '{print $5}' cat file | awk '{print $5}' But it's of no use, thanks in advance! cat file | awk '{print $5}'但这没有用,谢谢!

How about sed ? sed怎么样?

sed 's|^.*remove_til_here/||'

For a non-greedy match I think you can also use perl - no, wait, don't run away: 对于非贪婪的比赛,我认为您也可以使用perl-不,请稍等,不要逃避:

perl -pe 's|^(.*?)remove_til_here/||' file

Since the pattern to be removed is a fixed path , you know how many directory separators are present. 由于要删除的模式固定路径 ,因此您知道存在多少个目录分隔符。 So might as well use cut : 所以不妨使用cut

cut -d/ -f6- inputfile

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从一行的开头获取正则表达式并将其复制到以下几行的开头? - How can I take a regular expression from the beginning of one line and copy it to the beginning of following lines? 如何打印与特定字符串匹配的所有行? - How to print all lines that matches a particular string? 如何将字符串添加到文件中每一行的开头? - How can I prepend a string to the beginning of each line in a file? 如何在bash文件夹中的每个文件的开头添加一个字符串? - How can I add a string to the beginning of each file in a folder in bash? 从文件的所有行中提取字符串 - Extract a string from all lines in a file 如何在外壳脚本中不使用管道或重定向的情况下将字符串附加到文件的开头或结尾? - How can I attach a string to the beginning or end of a file without using pipe or redirection in shell script? 在bash / linux中,尝试从文件中满足特定条件的文件中删除以特定字符串开头的行 - Trying to delete lines beginning with a specific string from files where the file meets a target condition, in bash/linux 我可以从终端输出中 grep(或过滤)特定颜色的行吗? - Can i grep ( or filter) lines of a particular color from the terminal output? 如何从文件[shell]中某些特定行的末尾删除\\ n? - How to delete \n from the end of some particular lines in file [shell]? 打印不以哈希开头的文件中的所有行,并按特定列过滤 - Print all lines in a file not beginning with hash and also filter by a specific column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM