Как искать каждую встречу в текстовом файле Linux?

Не используйте -no- параметр cache-inodes.

Mkisofs не очень голоден до памяти. Вы можете стать жертвой плохого варианта mkisofs, основанного на коде 2004 года, и это требует большого количества памяти. В 2006 году было исправлено множество ошибок в mkisofs, поэтому используйте последнюю версию.

Я только что попробовал, и последний mkisofs потребляет ок. 400 МБ памяти для 630000 файлов при создании Rock Ridge и около 80 МБ без Rock Ridge. Для ваших 2,5-мегабайтных файлов потребуется 1,6 ГБ памяти с Rock Ridge и менее 400 МБ без. Это слишком много для вашей машины?

1
21.08.2018, 19:54
4 ответа

Попробуйте это,

users=( User1 User2 User3 User4 )
for i in "${users[@]}"
do
   grep -qw $i file && echo "$i is in the file" || echo "$i is not in the file"
done

Изman:

-q, --quiet, --silent

Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected.

2
27.01.2020, 23:12

Использовать массив:

users=(User1 User2 User3 User4) # et cetera
for i in "${users[@]}"; do
    echo -n "$user is "
    if grep -q "$user" inputfile; then
        echo "present"
    else
        echo "not present"
    fi
done

grep -qвыполнит поиск, но не вернет никаких результатов, что позволит вам использовать его в тесте if.

Кроме того, вы можете поместить каждого пользователя в свою строку в файле с именем Users, а затем:

grep -o -f Users inputfile

Будет выведен список всех увиденных пользователей. Если вы хотите видеть как присутствующих, так и отсутствующих пользователей, вы можете:

echo "Users present:"
grep -o -f Users inputfile
echo "Users absent:"
grep -vo -f Users inputfile
4
27.01.2020, 23:12

Еще одно изменение.

users=( User1 User2 User3 User4 )
for i in "${users[@]}"
do
   echo "$i is" $(grep -qw $i file || echo "not") "in the file"
done
1
27.01.2020, 23:12

Всего одно сканирование файла :это bash

# the array of user names
users=( User{1..23} )
# an array of grep options: ( -e User1 -e User2...)
for u in "${users[@]}"; do grep_opts+=( -e "$u" ); done
# scan the input file and save the user names that are present in the file
readarray -t users_present < <(grep -Fo "${grep_opts[@]}" input | sort -u)
# find the user names absent from the file
# this assumes there are no spaces in any of the user names.
for u in "${users[@]}"; do
     [[ " ${users_present[*]} " == *" $u "* ]] || users_absent+=( "$u" )
done
# and print out the results
printf "%s is in the file\n" "${users_present[@]}"
printf "%s is NOT in the file\n" "${users_absent[@]}"
1
27.01.2020, 23:12

Теги

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