Я использую sed правильно для выполнения этой условной замены? Существует ли лучший путь?

У меня есть разграниченный пространством файл, это - приблизительно 3 200 строк долго. Каждая строка содержит 7 + поля.

То, что я хотел бы сделать, отредактировать файл с sed так, чтобы каждой строке, содержащей определенную переменную в поле 5, изменили бы ее поле 1 на X.

То, что я думаю, должно сделать что-то как:

for variable in `cat word.list.file`  
do  
sed 's/line_with_$variable_in_field5/replace_field1_with_X/g' old.file > new.file  
cp new.file old.file  
done

Это корректно? Существует ли лучший путь?

То, с чем я нуждаюсь в помощи, заполняет sed команда или нахождение альтернативного способа выполнить то же самое.

Я могу легко преобразовать разграниченный пространством файл в файл разделенных запятыми значений, если он сделал бы вещи легче.

Сообщите мне, необходимо ли какое-либо разъяснение.

5
задан 28.09.2014, 02:26

3 ответа

Это предотвращает необходимость много раз читать каждый файл. Это читает каждый файл только однажды.

awk 'NR == FNR {a[$1]=1;next} $5 in a {$1="XYZ"} {print}' word.list.file old.file > new.file && mv new.file old.file

Объяснение:

# if the current record number is the same as the record number in the file
# which means "if we're reading the first file"
NR == FNR {
    a[$1]=1  # put a flag in an array indexed by the contents of the first field
    next     # read the next line in the file and continue at the top of the script
}

# Now we're processing the second file
# if field 5 exists as an index in the array named "a" (it's a word from the first file)
$5 in a {
    $1="XYZ"  # replace the first field with new contents
}

# for all lines in the second file, changed or not
{
    print    # print them
}' \
    word.list.file old.file \
    > new.file && \
    mv new.file old.file

Используйте файлы "word.list.file" и "old.file", как введено. Запишите вывод в "new.file". Если целая операция не производит ошибку (&&), затем переименуйте "new.file" назад к "old.file". Часть, описанная в этом абзаце, является единственной частью всего этого, которое является Bash (или оболочка). Часть в исходной команде наверху и описала строками комментария, сценарий AWK. AWK является языком программирования самостоятельно и независим от оболочки.

6
ответ дан 07.12.2019, 17:07

Существует много способов сделать это.

Вот способ использовать только bash:

#!/bin/bash

# read word.list.file into words
words=$(<word.list.file)
# read line-by-line, each space-separated field goes into an array called fields
while IFS=$' \n' read -r -a fields; do
    # could possibly be an associative array to make it faster
    for word in $words; do
        # zero-indexed, so 4 means the fifth field
        if test "${fields[4]}" = "$word"; then
            # change the first field to "X"
            fields[0]="X"
        fi
    done
    echo "${fields[*]}"
done <old.file >new.file
mv new.file old.file

И вот использование решения sed:

#!/bin/bash

# bash-only syntax: read word.list.file into an array...
words=( $(<word.list.file) )
OIFS="$IFS"
IFS=$'|'
# ...and make a variable called "wordpattern"
# that contains a sed extended regular expression that matches
# any of those words, i.e. "word1|word2|word3..."
wordpattern="${words[*]}"
IFS="$OIFS"

# sed -r makes sed use extended re, which makes the pattern easier to read,
# but might only work on GNU/Linux and FreeBSD systems
# /...$wordpattern/ matches four words followed by a fifth word from word.list.file
# then the s/.../.../ makes a replacement on only those lines
# note that we have to use double quotes rather than single quotes
# so the shell can expand $wordpattern
sed -r -e "/^([^ ]* ){4}$wordpattern\>/s/^([^ ]*)(.*)/X\2/" old.file >new.file
mv new.file old.file

И версия в (ржавом) Perl в придачу:

#!/usr/bin/env perl

my $wordfile = "word.list.file";
open WORDS, "<$wordfile"
    or die "Cannot open $wordfile: $!\n";

my @words;
while (my $word = <WORDS>) {
    chomp $word;
    push @words, $word;
}
my $wordpattern = join '|', @words;
close WORDS;

my $oldfile = "old.file";
open IN, "<$oldfile"
    or die "Cannot open $oldfile: $!\n";

my $newfile = "new.file";
open OUT, ">$newfile"
    or die "Cannot open $newfile for writing: $!\n";
# output now goes to the OUT file handle (meaning $newfile) by default
select OUT;

while (my $line = <IN>) {
    chomp $line;
    my @fields = split / /, $line;
    if ($fields[4] =~ /$wordpattern/) {
        $fields[0] = "X";
    }
    $line = join ' ', @fields;
    print $line . "\n";
}

close OUT;
close IN;

rename $newfile, $oldfile
    or die "Cannot rename $newfile to $oldfile: $!\n";
3
ответ дан 07.12.2019, 17:07

Это было бы хорошим приложением для awk. Как простой пример:

for variable in $(word.list.file)
do   
    awk -v pat=$variable '$5 ~ pat {$1 = "X"}1' file1 > tmp
    mv tmp > file1
done
1
ответ дан 07.12.2019, 17:07

Теги

Похожие вопросы