문제

I have txt file with a lot of lines the most of them repeat, want to change on every line which contains "find" make separation on the last "/" and to add after it " -name ".

Txt file:

find /etc/cron.*
find /etc/inet.d/*.conf
find /etc/rc*
grep root /etc/passwd

Expected view:

find /etc/ -name cron.*
find /etc/inet.d/ -name *.conf
find /etc/ -name rc*
grep root /etc/passwd
도움이 되었습니까?

해결책 2

Modify every line which contains find only:

$ 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

다른 팁

This should work:

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
perl -wpe's!^(find.*/)!$1 -name !' file

Add -i before -wpe to actually change the file.

On more modern perl versions:

perl -wpe's!^(find.*/)\K! -name !' file
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";
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top