我有大量行重复的txt文件,想要在包含“find”的每一行上更改最后一行“/”并在“-name”之后添加。
Txt文件:
find /etc/cron.*
find /etc/inet.d/*.conf
find /etc/rc*
grep root /etc/passwd
预期观点:
find /etc/ -name cron.*
find /etc/inet.d/ -name *.conf
find /etc/ -name rc*
grep root /etc/passwd
答案 0 :(得分:4)
这应该有效:
awk 'BEGIN{FS=OFS="/"}{$NF=" -name "$NF}1' file
$ cat file
find /etc/cron.*
find /etc/inet.d/*.conf
find /etc/rc*
grep root /etc/passwd
$ awk 'BEGIN{FS=OFS="/"}{$NF=" -name "$NF}1' file
find /etc/ -name cron.*
find /etc/inet.d/ -name *.conf
find /etc/ -name rc*
grep root /etc/ -name passwd
答案 1 :(得分:2)
仅修改包含find
的每一行:
$ awk '/^find /{$NF=" -name "$NF}1' FS='/' OFS='/' file
find /etc/ -name cron.*
find /etc/inet.d/ -name *.conf
find /etc/ -name rc*
grep root /etc/passwd
答案 2 :(得分:1)
perl -wpe's!^(find.*/)!$1 -name !' file
在-wpe之前添加-i以实际更改文件。
更现代的perl版本:
perl -wpe's!^(find.*/)\K! -name !' file
答案 3 :(得分:0)
use warnings;
use strict;
open (FILE, "$ARGV[0]"); #pass file containing commands as first argument
my @file_contents = <FILE>;
close FILE;
#my @file_contents = (
# 'find /etc/cron.*',
# 'find /etc/inet.d/*.conf',
# 'find /etc/rc*',
# 'grep root /etc/passwd',
#);
#Build command line
foreach my $line (@file_contents){
chomp $line;
$line =~ s/(.*?) (\/.*\/)(.*)/$1 $2 -name $3/ if $line =~ /find/;
print "$line\n";
}