0

I'm trying to figure out how to check if the name of a file exists in a .txt document.

I have the file list.txt which contains:

001 jane jane_profile_07272018.doc 002 kwood kwood_profile_04022017.doc 003 pchow pchow_profile_05152019.doc 004 janez janez_profile_11042019.doc 005 jane jane_pic_07282018.jpg 006 kwood kwood_pic_04032017.jpg 007 pchow pchow_pic_05162019.jpg 008 jane jane_contact_07292018.csv 009 kwood kwood_contact_04042017.csv 010 pchow pchow_contact_05172019.csv 

I need to check whether the files related to "jane" exist in the filesystem or not.

While trying to figure it out I wanted to just check if I could make it work by only searching for the .doc but I can't figure out the if expression to use.

files=$(grep "jane " list.txt | cut -d " " -f3) if [ -e *.doc "$files" ]; then echo "File exists" fi 

This gives the error: "main.sh: line 5: [: jane_profile_07272018.doc: binary operator expected"

I've spent an embarrassing amount of time trying to figure this out so any help is much appreciated.

EDIT: With some help from a friend we figured it out and this code works best for what I'm trying to do.

#!/bin/bash > oldFiles.txt files=$(grep " jane " list.txt | cut -d' ' -f3) for f in $files; do if [ -e $f ]; then echo $f >> oldFiles.txt; fi done 

4 Answers 4

1

This might help with bash and a regex:

while read -r x y z; do [[ "$z" =~ jane.*\.doc$ ]] && [[ -e "$z" ]] && echo "$z exists"; done < list.txt 

This loop reads lines from file list.txt and splits its three columns to variables x, y and z. Variable z always contains the filename. [[ "$z" =~ jane.*\.doc$ ]] checks if filename contains string jane and ends ($) with .doc (jane.*\.doc$ is a regex). If this check was successful it checks if filename exists. If filename exists then it output some text.

With if, then and fi:

while read -r x y z; do if [[ "$z" =~ jane.*\.doc$ ]] && [[ -e "$z" ]]; then echo "$z exists" fi done < list.txt 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! It works but I don't fully understand it so unfortunately I can't modify it to solve my issue. I need to be able to check if any/all of the files I have (one .doc and one .csv) exists in the list.txt.
1
# filter lines that second column is jane # print third column awk '$2 == "jane"{print $3}' list.txt| # for each third column execute bash # inside bash check if first argument exists # if it does, print some text xargs -n1 -d'\n' bash -c '[[ -e "$1" ]] && echo "$1" exists' _ 

Comments

0

With some help from a friend we figured it out and this code works for what I'm trying to do.

#!/bin/bash > oldFiles.txt files=$(grep " jane " list.txt | cut -d' ' -f3) for f in $files; do if [ -e $f ]; then echo $f >> oldFiles.txt; fi done 

Comments

0
#!/bin/sh next() { [[ $(wc -l stack | cut -d' ' -f1) -gt 0 ]] && main end } main() { line=$(sed -n '1p' stack) [[ -s "${line}" ]] && echo "File exists." sed -i '1d' stack next } end() { rm -v ./stack exit 0 } sed -n '/jane/p' list.txt | cut -d' ' -f3 > stack next 

There are three functions here, defined first. The first two commands which actually run first are at the bottom; first the stack is generated with sed and cut, and then the next function is called.

The next function uses wc to see if the line number in the stack file is greater than zero. If it is, main is called. If not, end is called.

If main is called, the first line in the file is assigned to the line variable, and is then tested to see if that filename exists, and echoes if it does. The top line in the file is then deleted by sed, (which reduces the line count of the file by 1) and the script then runs the next function again.

When the line length of the stack file reaches zero, end is called, which deletes the empty stack file, and exits with an error code of zero, indicating success.

1 Comment

Thank you! If you could submit the clearer version I would appreciate it a lot. This is for an exercise I'm doing so it doesn't have to be super efficient as long as I can understand it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.