在保留列的同时在子字符串后添加空格

Add blank spaces after substring while keeping the columns

我有这样一个数据文件:

randomthingsbefore $DATAROOT/randompathwithoutanypattern randomthingsafter
randomthingsbefore $DATAROOT/randompathwithoutanypattern randomthingsafter $DATAROOT/randompathwithoutanypattern randomthingsafter
randomthingsbefore $DATAROOT/randompathwithoutanypattern randomthingsafter
(...)

我想从每个路径中删除子字符串 $DATAROOT 并在路径后添加空格以保留 randomthingsafter 开始的列.请注意,同一行中可能有 2 个或更多路径带有 $DATAROOT 子字符串。这样,我想要的输出将如下所示:

randomthingsbefore /randompathwithoutanypattern          randomthingsafter
randomthingsbefore /randompathwithoutanypattern          randomthingsafter /randompathwithoutanypattern          randomthingsafter
randomthingsbefore /randompathwithoutanypattern          randomthingsafter
(...)

我试过:

VAR1=*pathtofile*

VAR2=$(\grep -oP '$DATAROOT\K[^ ]*' $VAR1)
arr=$(echo $VAR2 | tr " " "\n")  

 for x in $arr
 do
   y="${x}          "
   sed -i "s:$x:$y:" $VAR1
 done

sed -i 's/$DATAROOT\///g' $VAR1

但是好像不行。感谢您的帮助!

我相信最简单的方法就是使用 在一行中替换您的脚本:

sed 's/$DATAROOT\([^[:blank:]]*\)/         /g' /path/to/file

注意,在</code>后面有9个空格,也就是字符串<code>$DATAROOT的长度。这里我们使用所谓的 back-reference:

Editing Commands in sed

[2addr]s/BRE/replacement/flags: Substitute the replacement string for instances of the BRE in the pattern space. Any character other than <backslash> or <newline> can be used instead of a <slash> to delimit the BRE and the replacement. Within the BRE and the replacement, the BRE delimiter itself can be used as a literal character if it is preceded by a <backslash>.

The replacement string shall be scanned from beginning to end. An <ampersand> ( & ) appearing in the replacement shall be replaced by the string matching the BRE. The special meaning of & in this context can be suppressed by preceding it by a <backslash>. The characters \n, where n is a digit, shall be replaced by the text matched by the corresponding back-reference expression. If the corresponding back-reference expression does not match, then the characters \n shall be replaced by the empty string. The special meaning of \n where n is a digit in this context, can be suppressed by preceding it by a <backslash>. For each other <backslash> encountered, the following character shall lose its special meaning (if any).

source: POSIX SED

9.3.6 BREs Matching Multiple Characters

  1. The back-reference expression \n shall match the same (possibly empty) string of characters as was matched by a subexpression enclosed between \( and \) preceding the \n. The character n shall be a digit from 1 through 9, specifying the nth subexpression (the one that begins with the nth \( from the beginning of the pattern and ends with the corresponding paired \) ). The expression is invalid if less than n subexpressions precede the \n. The string matched by a contained subexpression shall be within the string matched by the containing subexpression. If the containing subexpression does not match, or if there is no match for the contained subexpression within the string matched by the containing subexpression, then back-reference expressions corresponding to the contained subexpression shall not match. When a subexpression matches more than one string, a back-reference expression corresponding to the subexpression shall refer to the last matched string. For example, the expression ^\(.*\)$ matches strings consisting of two adjacent appearances of the same substring, and the expression \(a\)* fails to match a, the expression \(a\(b\)*\)* fails to match abab, and the expression ^\(ab*\)*$ matches ababbabb, but fails to match ababbab.

source: POSIX Basic Regular Expressions