How to test if string exists in file with Bash shell?

匿名 (未验证) 提交于 2019-12-03 01:57:01

问题:

I have a file that contains directory names:

my_list.txt :

/tmp /var/tmp 

I'd like to check in Bash before I'll add a directory name if that name already exists in the file.

回答1:

grep -Fxq "$FILENAME" my_list.txt 

The exit status is 0 (true) if the name was found, 1 (false) if not, so:

if grep -Fxq "$FILENAME" my_list.txt then     # code if found else     # code if not found fi 

Here are the relevant sections of the man page for 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. 


回答2:

Regarding the following solution:

grep -Fxq "$FILENAME" my_list.txt 

In case you are wondering (as I did) what -Fxq means in plain English F affects how PATTERN is interpreted (fixed string instead of a regex), x match whole line, q shhhhh... minimal printing

From the man file:

-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.) 


回答3:

Three methods in my mind:

1) Short test for a name in a path (I'm not sure this might be your case)

ls -a "path" | grep "name" 


2) Short test for a string in a file

grep -R "string" "filepath" 


3) Longer bash script using 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 

This should be quicker if you have to test multiple string on a file content using a loop for example changing the regex at any cicle.



回答4:

Simpler way:

if grep "$filename" my_list.txt > /dev/null then    ... found else    ... not found fi 

Tip: send to /dev/null if you want command's exit status, but not outputs.



回答5:

If I understood your question correctly, this should do what you need.

  1. you can specifiy the directory you would like to add through $check variable
  2. if the directory is already in the list, the output is "dir already listed"
  3. if the directory is not yet in the list, it is appended to my_list.txt

In one line: check="/tmp/newdirectory"; [[ -n $(grep "^$check\$" my_list.txt) ]] && echo "dir already listed" || echo "$check" >> my_list.txt



回答6:

If you just want to check the existence of one line, you do not need to create a file. E.g.,

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   


回答7:

My version using fgrep

  FOUND=`fgrep -c "FOUND" $VALIDATION_FILE`   if [ $FOUND -eq 0 ]; then     echo "Not able to find"   else     echo "able to find"        fi   


回答8:

Easiest and simplest way would be:

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 will return the count of how many times the string occurs in the file.



回答9:

grep -E "(string)" /path/to/file || echo "no match found" 

-E option makes grep use regular expressions



回答10:

A grep-less solution, works for me:

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 

based on: https://stackoverflow.com/a/229606/3306354



回答11:

if grep -q "$Filename$" my_list.txt    then      echo "exist" else       echo "not exist" fi 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!