Bash 与str_replace等效的awk

Bash 与str_replace等效的awk,bash,awk,Bash,Awk,awk中是否有一个函数可以用另一个字符串替换一个字符串?例如,我们有一个e文件,其值如下: data_file: /some/path/to/data/2014/01-02/some_file /some/path/to/data/2014/01-02/some_file2 /some/path/to/data/2014/01-02/some_file3 cat data_file | awk '{ str_replace("/some/path/to/data/", ""); print }

awk中是否有一个函数可以用另一个字符串替换一个字符串?例如,我们有一个e文件,其值如下:

data_file:
/some/path/to/data/2014/01-02/some_file
/some/path/to/data/2014/01-02/some_file2
/some/path/to/data/2014/01-02/some_file3

cat data_file | awk '{ str_replace("/some/path/to/data/", ""); print }'
# the above should output
2014/01-02/some_file
2014/01-02/some_file2
2014/01-02/some_file3
awk '{sub(/.*data/,"")}8' file
/2014/01-02/some_file
/2014/01-02/some_file2
/2014/01-02/some_file3

在这种情况下,
cut
似乎更合适:

$ cut -d/ -f6- inputfile
2014/01-02/some_file
2014/01-02/some_file2
2014/01-02/some_file3
用于
awk

$ awk '{sub("/some/path/to/data/", "", $0)}1' inputfile
2014/01-02/some_file
2014/01-02/some_file2
2014/01-02/some_file3

在这种情况下,
cut
似乎更合适:

$ cut -d/ -f6- inputfile
2014/01-02/some_file
2014/01-02/some_file2
2014/01-02/some_file3
用于
awk

$ awk '{sub("/some/path/to/data/", "", $0)}1' inputfile
2014/01-02/some_file
2014/01-02/some_file2
2014/01-02/some_file3
有些人喜欢这样:

data_file:
/some/path/to/data/2014/01-02/some_file
/some/path/to/data/2014/01-02/some_file2
/some/path/to/data/2014/01-02/some_file3

cat data_file | awk '{ str_replace("/some/path/to/data/", ""); print }'
# the above should output
2014/01-02/some_file
2014/01-02/some_file2
2014/01-02/some_file3
awk '{sub(/.*data/,"")}8' file
/2014/01-02/some_file
/2014/01-02/some_file2
/2014/01-02/some_file3
有些人喜欢这样:

data_file:
/some/path/to/data/2014/01-02/some_file
/some/path/to/data/2014/01-02/some_file2
/some/path/to/data/2014/01-02/some_file3

cat data_file | awk '{ str_replace("/some/path/to/data/", ""); print }'
# the above should output
2014/01-02/some_file
2014/01-02/some_file2
2014/01-02/some_file3
awk '{sub(/.*data/,"")}8' file
/2014/01-02/some_file
/2014/01-02/some_file2
/2014/01-02/some_file3
不可以。有
[g]sub()
可以用字符串替换正则表达式,但要用字符串替换字符串,则需要index()、length()和substr()的组合:

如果搜索字符串中有任何RE元字符,则使用此方法和使用[g]sub()之间的区别将变得很明显,例如:

$ cat file
/some/.*/2014/01-02/some_file
/some/.*/2014/01-02/some_file2
/some/.*/2014/01-02/some_file3

$ awk '{sub("/some/.*/","")}1' file
some_file
some_file2
some_file3

$ awk 'BEGIN{old="/some/.*/"; new=""}
  idx=index($0,old){ $0 = substr($0,1,idx-1) new substr($0,idx+length(old))} 1' file
2014/01-02/some_file
2014/01-02/some_file2
2014/01-02/some_file3
不可以。有
[g]sub()
可以用字符串替换正则表达式,但要用字符串替换字符串,则需要index()、length()和substr()的组合:

如果搜索字符串中有任何RE元字符,则使用此方法和使用[g]sub()之间的区别将变得很明显,例如:

$ cat file
/some/.*/2014/01-02/some_file
/some/.*/2014/01-02/some_file2
/some/.*/2014/01-02/some_file3

$ awk '{sub("/some/.*/","")}1' file
some_file
some_file2
some_file3

$ awk 'BEGIN{old="/some/.*/"; new=""}
  idx=index($0,old){ $0 = substr($0,1,idx-1) new substr($0,idx+length(old))} 1' file
2014/01-02/some_file
2014/01-02/some_file2
2014/01-02/some_file3

您可以从
awk
中删除
$0
,因为如果省略它的默认数据,它将变得非常流行。@Jotne-Yup!不适用于一般情况。如果替换字符串包含
&
,则会重复找到的字符串。您可以从
awk
中删除
$0
,因为如果省略它的默认数据。@Jotne-Yup,无论如何都会变得流行!不适用于一般情况。如果替换字符串包含
&
,则会重复找到的字符串。最后的
8
是什么意思?@RolandIllig它只是一个随机数。任何数字都将是一个真实状态,任何没有操作的真实状态都将执行默认操作,在本例中,打印该行,因此可以将其更改为:
awk'{sub(/.*data/,“”)}{print$0}文件
,感谢您的解释。我一直以为随机数是4,没注意到它同时翻了一倍。最后的
8
是什么意思?@RolandIllig它只是一个随机数。任何数字都将是一个真实状态,任何没有操作的真实状态都将执行默认操作,在本例中,打印该行,因此可以将其更改为:
awk'{sub(/.*data/,“”)}{print$0}文件
,感谢您的解释。我一直认为随机数是4,但没有注意到它同时翻了一倍。