删除更改字符串后的所有内容
Remove everything after a changing string
我遇到了以下问题;
作为输入,我得到几行文件路径如下:
root/child/abc/somefile.txt
root/child/def/123/somefile.txt
root/child/ghijklm/somefile.txt
root/child
部分始终在路径中,之后的内容可能不同。
我想删除孙子文件夹之后的所有内容。所以输出将是:
root/child/abc/
root/child/def/
root/child/ghijklm/
我试过以下方法:
sed 's/\/child\/.*/\/child\/.*/'
当然这只会给出以下输出:
root/child/.*
root/child/.*
root/child/.*
如有任何帮助,我们将不胜感激!
With awk
: 您能否尝试使用 GNU [=13= 中显示的示例进行以下编写和测试].
awk 'match([=10=],/root\/child\/[^/]*/){print substr([=10=],RSTART,RLENGTH)}' Input_file
解释:为以上添加详细解释。
awk ' ##Starting awk program from here.
match([=11=],/root\/child\/[^/]*/){ ##Using match function to match root/child/... till next / in current line.
print substr([=11=],RSTART,RLENGTH) ##printig substring from RSTART to till RLENGTH.
}
' Input_file ##Mentioning Input_file name here.
与sed
:
sed 's/.*\(root\/child\/[^/]*\).*//' Input_file
解释:使用sed
的替换方法匹配root/child/ till next occurrence of /
并保存到临时缓冲区(反向引用方法)并用仅匹配的反向引用值替换整行。
剪裁:
cut -d\/ -f1,2,3 file
你很接近。
sed 's%\(/child/[^/]*\)/.*%%'
正则表达式 [^/]*
匹配尽可能多的不是斜杠的字符;然后我们用我们在括号中捕获的部分替换整个匹配,有效地修剪掉其余部分。
使用 Perl:
perl -pe 's{ ^ ( ( [^/]+ / ){3} ) .* $ }{}x' in_file > out_file
Perl 单行代码使用这些命令行标志:
-e
: 告诉 Perl 查找内联代码,而不是在文件中。
-p
:一次循环输入一行,默认情况下将其分配给 $_
。在每次循环迭代后添加 print $_
。
正则表达式使用这个修饰符:
x
: 为了便于阅读,忽略空格和注释。
替换语句,解释:
^
: 行首。
$
: 行尾。
[^/]+ /
: 一个或多个不是斜杠的字符 (/
),后跟一个斜杠。
( [^/]+ / ){3}
: 一个或多个非斜杠字符,后跟一个斜杠,正好重复 3 次。
( ( [^/]+ / ){3} )
:上面,用括号将匹配的部分捕获到第一个捕获变量 </code> 中,以便稍后在替换中使用。捕获组从左到右计数。<br />
<code>.*
: 任意字符出现零次或多次。
s{THIS}{THAT}
: 将 THIS
替换为 THAT
.
另见:
perldoc perlrun
: how to execute the Perl interpreter: command line switches
perldoc perlre
: Perl regular expressions (regexes)
perldoc perlre
: Perl regular expressions (regexes): Quantifiers; Character Classes and other Special Escapes; Assertions; Capture groups
perldoc perlrequick
: Perl regular expressions quick start
这可能对你有用 (GNU sed):
sed -E 's/^(([^/]*[/]){3}).*//' file
删除第三组non-forward-slashes/slash之后的所有内容。
我遇到了以下问题;
作为输入,我得到几行文件路径如下:
root/child/abc/somefile.txt
root/child/def/123/somefile.txt
root/child/ghijklm/somefile.txt
root/child
部分始终在路径中,之后的内容可能不同。
我想删除孙子文件夹之后的所有内容。所以输出将是:
root/child/abc/
root/child/def/
root/child/ghijklm/
我试过以下方法:
sed 's/\/child\/.*/\/child\/.*/'
当然这只会给出以下输出:
root/child/.*
root/child/.*
root/child/.*
如有任何帮助,我们将不胜感激!
With awk
: 您能否尝试使用 GNU [=13= 中显示的示例进行以下编写和测试].
awk 'match([=10=],/root\/child\/[^/]*/){print substr([=10=],RSTART,RLENGTH)}' Input_file
解释:为以上添加详细解释。
awk ' ##Starting awk program from here.
match([=11=],/root\/child\/[^/]*/){ ##Using match function to match root/child/... till next / in current line.
print substr([=11=],RSTART,RLENGTH) ##printig substring from RSTART to till RLENGTH.
}
' Input_file ##Mentioning Input_file name here.
与sed
:
sed 's/.*\(root\/child\/[^/]*\).*//' Input_file
解释:使用sed
的替换方法匹配root/child/ till next occurrence of /
并保存到临时缓冲区(反向引用方法)并用仅匹配的反向引用值替换整行。
剪裁:
cut -d\/ -f1,2,3 file
你很接近。
sed 's%\(/child/[^/]*\)/.*%%'
正则表达式 [^/]*
匹配尽可能多的不是斜杠的字符;然后我们用我们在括号中捕获的部分替换整个匹配,有效地修剪掉其余部分。
使用 Perl:
perl -pe 's{ ^ ( ( [^/]+ / ){3} ) .* $ }{}x' in_file > out_file
Perl 单行代码使用这些命令行标志:
-e
: 告诉 Perl 查找内联代码,而不是在文件中。
-p
:一次循环输入一行,默认情况下将其分配给 $_
。在每次循环迭代后添加 print $_
。
正则表达式使用这个修饰符:
x
: 为了便于阅读,忽略空格和注释。
替换语句,解释:
^
: 行首。
$
: 行尾。
[^/]+ /
: 一个或多个不是斜杠的字符 (/
),后跟一个斜杠。
( [^/]+ / ){3}
: 一个或多个非斜杠字符,后跟一个斜杠,正好重复 3 次。
( ( [^/]+ / ){3} )
:上面,用括号将匹配的部分捕获到第一个捕获变量 </code> 中,以便稍后在替换中使用。捕获组从左到右计数。<br />
<code>.*
: 任意字符出现零次或多次。
s{THIS}{THAT}
: 将 THIS
替换为 THAT
.
另见:
perldoc perlrun
: how to execute the Perl interpreter: command line switches
perldoc perlre
: Perl regular expressions (regexes)
perldoc perlre
: Perl regular expressions (regexes): Quantifiers; Character Classes and other Special Escapes; Assertions; Capture groups
perldoc perlrequick
: Perl regular expressions quick start
这可能对你有用 (GNU sed):
sed -E 's/^(([^/]*[/]){3}).*//' file
删除第三组non-forward-slashes/slash之后的所有内容。