Ich habe eine Datei, die Verzeichnisnamen enthält:
my_list.txt
:
/tmp
/var/tmp
Ich möchte in Bash einchecken, bevor ich einen Verzeichnisnamen hinzufüge, wenn dieser Name bereits in der Datei vorhanden ist.
grep -Fxq "$FILENAME" my_list.txt
Der Exit-Status ist 0 (wahr), wenn der Name gefunden wurde, 1 (falsch), falls nicht, also:
if grep -Fxq "$FILENAME" my_list.txt
then
# code if found
else
# code if not found
fi
Hier sind die relevanten Abschnitte von der Manpage für grep
:
grep [options] PATTERN [FILE...]
-F, --fixed-strings
Interpret PATTERN as a list of fixed strings, separated by new-
lines, any of which is to be matched.
-x, --line-regexp
Select only those matches that exactly match the whole line.
-q, --quiet, --silent
Quiet; do not write anything to standard output. Exit immedi-
ately with zero status if any match is found, even if an error
was detected. Also see the -s or --no-messages option.
In Bezug auf die folgende Lösung:
grep -Fxq "$FILENAME" my_list.txt
Falls Sie sich fragen (wie ich), was -Fxq
im einfachen Englisch bedeutet:
F
: Beeinflusst, wie PATTERN interpretiert wird (feste Zeichenfolge anstelle eines regulären Ausdrucks)x
: Übereinstimmung der gesamten Zeileq
: Shhhhh ... minimales DruckenAus der man-Datei:
-F, --fixed-strings
Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched.
(-F is specified by POSIX.)
-x, --line-regexp
Select only those matches that exactly match the whole line. (-x is specified by POSIX.)
-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. Also see the -s or --no-messages option. (-q is specified by
POSIX.)
Drei Methoden in meinem Kopf:
1) Kurzer Test für einen Namen in einem Pfad (Ich bin nicht sicher, ob dies der Fall ist)
ls -a "path" | grep "name"
2) Kurzer Test für eine Zeichenfolge in einer Datei
grep -R "string" "filepath"
3) Längeres Bash-Skript mit Regex:
#!/bin/bash
declare file="content.txt"
declare regex="\s+string\s+"
declare file_content=$( cat "${file}" )
if [[ " $file_content " =~ $regex ]] # please note the space before and after the file content
then
echo "found"
else
echo "not found"
fi
exit
Dies sollte schneller sein, wenn Sie mehrere Zeichenketten in einer Datei testen möchten Inhalt mithilfe einer Schleife, beispielsweise zum Ändern der regulären Ausdrücke in einem beliebigen Element.
Einfacher Weg:
if grep "$filename" my_list.txt > /dev/null
then
... found
else
... not found
fi
Tipp: Senden Sie an /dev/null
, wenn Sie den Exit-Status des Befehls wünschen, jedoch keine Ausgaben.
Der einfachste und einfachste Weg wäre:
isInFile=$(cat file.txt | grep -c "string")
if [ $isInFile -eq 0 ]; then
#string not contained in file
else
#string is in file at least once
fi
grep -c gibt die Anzahl zurück, wie oft die Zeichenfolge in der Datei vorkommt.
Wenn ich Ihre Frage richtig verstanden habe, sollte dies das tun, was Sie brauchen.
In einer Zeile : check="/tmp/newdirectory"; [[ -n $(grep "^$check\$" my_list.txt) ]] && echo "dir already listed" || echo "$check" >> my_list.txt
grep -E "(string)" /path/to/file || echo "no match found"
Die Option -E bewirkt, dass grep reguläre Ausdrücke verwendet
Wenn Sie nur das Vorhandensein einer Zeile überprüfen möchten, müssen Sie keine Datei erstellen. Z.B.,
if grep -xq "LINE_TO_BE_MATCHED" FILE_TO_LOOK_IN ; then
# code for if it exists
else
# code for if it does not exist
fi
Meine Version mit fgrep
FOUND=`fgrep -c "FOUND" $VALIDATION_FILE`
if [ $FOUND -eq 0 ]; then
echo "Not able to find"
else
echo "able to find"
fi
Eine grep-lose Lösung funktioniert für mich:
MY_LIST=$( cat /path/to/my_list.txt )
if [[ "${MY_LIST}" == *"${NEW_DIRECTORY_NAME}"* ]]; then
echo "It's there!"
else
echo "its not there"
fi
basierend auf: https://stackoverflow.com/a/229606/3306354
grep -Fxq "String to be found" | ls -a