Check that 2 arrays of file names are the same

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












So I have the following:



cat file | grep -E regex 


which gives a list of lines

Now I also have a for loop the produces a list of files

So it is like I have 2 arrays.

But how do I check in bash if both arrays have the same content? My bash version does not support associative arrays (4.2).

Should I find another way other than bash or is it doable with bash easily too?



Update

I go over for i in foo/bar/* and record the paths so I essentially I have somewhere:



foo/bar/a/b 
foo/bar/c/d
foo/bar/e etc


So I essentially just want to re-run the for loop and check that I get exactly the same outcome.










share|improve this question



















  • 3




    Using grep or diffcould help. Add sample data to your post for more infos.
    – Michael D.
    Nov 29 at 12:15






  • 2




    filenames can span more than one line, unfortunately. Do you have any insight or control into what generates the contents of file? Or can you declare an assumption/restriction that your filenames will never have newlines in them?
    – Jeff Schaller
    Nov 29 at 13:39










  • @JeffSchaller: Yes no new lines in file names
    – Jim
    Nov 29 at 15:06










  • Some sample data / directory structure might help understand your request, as "a list of lines" does not necessarily equal "a list of files".
    – RudiC
    Nov 29 at 17:45






  • 1




    @JeffSchaller:I updated post
    – Jim
    Nov 29 at 18:22














up vote
0
down vote

favorite












So I have the following:



cat file | grep -E regex 


which gives a list of lines

Now I also have a for loop the produces a list of files

So it is like I have 2 arrays.

But how do I check in bash if both arrays have the same content? My bash version does not support associative arrays (4.2).

Should I find another way other than bash or is it doable with bash easily too?



Update

I go over for i in foo/bar/* and record the paths so I essentially I have somewhere:



foo/bar/a/b 
foo/bar/c/d
foo/bar/e etc


So I essentially just want to re-run the for loop and check that I get exactly the same outcome.










share|improve this question



















  • 3




    Using grep or diffcould help. Add sample data to your post for more infos.
    – Michael D.
    Nov 29 at 12:15






  • 2




    filenames can span more than one line, unfortunately. Do you have any insight or control into what generates the contents of file? Or can you declare an assumption/restriction that your filenames will never have newlines in them?
    – Jeff Schaller
    Nov 29 at 13:39










  • @JeffSchaller: Yes no new lines in file names
    – Jim
    Nov 29 at 15:06










  • Some sample data / directory structure might help understand your request, as "a list of lines" does not necessarily equal "a list of files".
    – RudiC
    Nov 29 at 17:45






  • 1




    @JeffSchaller:I updated post
    – Jim
    Nov 29 at 18:22












up vote
0
down vote

favorite









up vote
0
down vote

favorite











So I have the following:



cat file | grep -E regex 


which gives a list of lines

Now I also have a for loop the produces a list of files

So it is like I have 2 arrays.

But how do I check in bash if both arrays have the same content? My bash version does not support associative arrays (4.2).

Should I find another way other than bash or is it doable with bash easily too?



Update

I go over for i in foo/bar/* and record the paths so I essentially I have somewhere:



foo/bar/a/b 
foo/bar/c/d
foo/bar/e etc


So I essentially just want to re-run the for loop and check that I get exactly the same outcome.










share|improve this question















So I have the following:



cat file | grep -E regex 


which gives a list of lines

Now I also have a for loop the produces a list of files

So it is like I have 2 arrays.

But how do I check in bash if both arrays have the same content? My bash version does not support associative arrays (4.2).

Should I find another way other than bash or is it doable with bash easily too?



Update

I go over for i in foo/bar/* and record the paths so I essentially I have somewhere:



foo/bar/a/b 
foo/bar/c/d
foo/bar/e etc


So I essentially just want to re-run the for loop and check that I get exactly the same outcome.







bash filenames array






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 29 at 18:22

























asked Nov 29 at 11:58









Jim

390213




390213







  • 3




    Using grep or diffcould help. Add sample data to your post for more infos.
    – Michael D.
    Nov 29 at 12:15






  • 2




    filenames can span more than one line, unfortunately. Do you have any insight or control into what generates the contents of file? Or can you declare an assumption/restriction that your filenames will never have newlines in them?
    – Jeff Schaller
    Nov 29 at 13:39










  • @JeffSchaller: Yes no new lines in file names
    – Jim
    Nov 29 at 15:06










  • Some sample data / directory structure might help understand your request, as "a list of lines" does not necessarily equal "a list of files".
    – RudiC
    Nov 29 at 17:45






  • 1




    @JeffSchaller:I updated post
    – Jim
    Nov 29 at 18:22












  • 3




    Using grep or diffcould help. Add sample data to your post for more infos.
    – Michael D.
    Nov 29 at 12:15






  • 2




    filenames can span more than one line, unfortunately. Do you have any insight or control into what generates the contents of file? Or can you declare an assumption/restriction that your filenames will never have newlines in them?
    – Jeff Schaller
    Nov 29 at 13:39










  • @JeffSchaller: Yes no new lines in file names
    – Jim
    Nov 29 at 15:06










  • Some sample data / directory structure might help understand your request, as "a list of lines" does not necessarily equal "a list of files".
    – RudiC
    Nov 29 at 17:45






  • 1




    @JeffSchaller:I updated post
    – Jim
    Nov 29 at 18:22







3




3




Using grep or diffcould help. Add sample data to your post for more infos.
– Michael D.
Nov 29 at 12:15




Using grep or diffcould help. Add sample data to your post for more infos.
– Michael D.
Nov 29 at 12:15




2




2




filenames can span more than one line, unfortunately. Do you have any insight or control into what generates the contents of file? Or can you declare an assumption/restriction that your filenames will never have newlines in them?
– Jeff Schaller
Nov 29 at 13:39




filenames can span more than one line, unfortunately. Do you have any insight or control into what generates the contents of file? Or can you declare an assumption/restriction that your filenames will never have newlines in them?
– Jeff Schaller
Nov 29 at 13:39












@JeffSchaller: Yes no new lines in file names
– Jim
Nov 29 at 15:06




@JeffSchaller: Yes no new lines in file names
– Jim
Nov 29 at 15:06












Some sample data / directory structure might help understand your request, as "a list of lines" does not necessarily equal "a list of files".
– RudiC
Nov 29 at 17:45




Some sample data / directory structure might help understand your request, as "a list of lines" does not necessarily equal "a list of files".
– RudiC
Nov 29 at 17:45




1




1




@JeffSchaller:I updated post
– Jim
Nov 29 at 18:22




@JeffSchaller:I updated post
– Jim
Nov 29 at 18:22










1 Answer
1






active

oldest

votes

















up vote
0
down vote













Assuming your arrays are named "array1" and "array2", bash code to check for equality:



equal=true
if [[ $#array1[@] -ne $#array2[@] ]]; then
equal=false
else
while IFS= read -r elem1 <&3; IFS= read -r elem2 <&4; do
if [[ "$elem1" != "$elem2" ]]; then
equal=false
break
fi
done 3< <(printf "%sn" "$array1[@]" | sort)
4< <(printf "%sn" "$array2[@]" | sort)
fi
if $equal; then
echo arrays have the same contents
else
echo arrays have different contents
fi


This will be (probably "much") slower than calling out to external tools.



diff <(grep -E regex file1) <(grep -E regex file2)





share|improve this answer




















  • How do I initialize the arrays? Also the 2 < in the diff snippet are they right?
    – Jim
    Nov 29 at 15:58











  • Also what is 3 and 4?
    – Jim
    Nov 29 at 16:46










  • "3" and "4" are file descriptors: I want to be able to read from 2 different files in the same loop, so I red them from different file descriptors. Note how 3< after the loop corresponds with the <&3 in one of the read commands.
    – glenn jackman
    Nov 30 at 0:17











  • The 2 < are correct: this bit 3< is the redirection and this bit <(...) is a process substitution
    – glenn jackman
    Nov 30 at 0:18











  • I can't make that part work. I get an error as if diff us using the outputs of grep as files and says can't open them
    – Jim
    Nov 30 at 8:51










Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f484893%2fcheck-that-2-arrays-of-file-names-are-the-same%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













Assuming your arrays are named "array1" and "array2", bash code to check for equality:



equal=true
if [[ $#array1[@] -ne $#array2[@] ]]; then
equal=false
else
while IFS= read -r elem1 <&3; IFS= read -r elem2 <&4; do
if [[ "$elem1" != "$elem2" ]]; then
equal=false
break
fi
done 3< <(printf "%sn" "$array1[@]" | sort)
4< <(printf "%sn" "$array2[@]" | sort)
fi
if $equal; then
echo arrays have the same contents
else
echo arrays have different contents
fi


This will be (probably "much") slower than calling out to external tools.



diff <(grep -E regex file1) <(grep -E regex file2)





share|improve this answer




















  • How do I initialize the arrays? Also the 2 < in the diff snippet are they right?
    – Jim
    Nov 29 at 15:58











  • Also what is 3 and 4?
    – Jim
    Nov 29 at 16:46










  • "3" and "4" are file descriptors: I want to be able to read from 2 different files in the same loop, so I red them from different file descriptors. Note how 3< after the loop corresponds with the <&3 in one of the read commands.
    – glenn jackman
    Nov 30 at 0:17











  • The 2 < are correct: this bit 3< is the redirection and this bit <(...) is a process substitution
    – glenn jackman
    Nov 30 at 0:18











  • I can't make that part work. I get an error as if diff us using the outputs of grep as files and says can't open them
    – Jim
    Nov 30 at 8:51














up vote
0
down vote













Assuming your arrays are named "array1" and "array2", bash code to check for equality:



equal=true
if [[ $#array1[@] -ne $#array2[@] ]]; then
equal=false
else
while IFS= read -r elem1 <&3; IFS= read -r elem2 <&4; do
if [[ "$elem1" != "$elem2" ]]; then
equal=false
break
fi
done 3< <(printf "%sn" "$array1[@]" | sort)
4< <(printf "%sn" "$array2[@]" | sort)
fi
if $equal; then
echo arrays have the same contents
else
echo arrays have different contents
fi


This will be (probably "much") slower than calling out to external tools.



diff <(grep -E regex file1) <(grep -E regex file2)





share|improve this answer




















  • How do I initialize the arrays? Also the 2 < in the diff snippet are they right?
    – Jim
    Nov 29 at 15:58











  • Also what is 3 and 4?
    – Jim
    Nov 29 at 16:46










  • "3" and "4" are file descriptors: I want to be able to read from 2 different files in the same loop, so I red them from different file descriptors. Note how 3< after the loop corresponds with the <&3 in one of the read commands.
    – glenn jackman
    Nov 30 at 0:17











  • The 2 < are correct: this bit 3< is the redirection and this bit <(...) is a process substitution
    – glenn jackman
    Nov 30 at 0:18











  • I can't make that part work. I get an error as if diff us using the outputs of grep as files and says can't open them
    – Jim
    Nov 30 at 8:51












up vote
0
down vote










up vote
0
down vote









Assuming your arrays are named "array1" and "array2", bash code to check for equality:



equal=true
if [[ $#array1[@] -ne $#array2[@] ]]; then
equal=false
else
while IFS= read -r elem1 <&3; IFS= read -r elem2 <&4; do
if [[ "$elem1" != "$elem2" ]]; then
equal=false
break
fi
done 3< <(printf "%sn" "$array1[@]" | sort)
4< <(printf "%sn" "$array2[@]" | sort)
fi
if $equal; then
echo arrays have the same contents
else
echo arrays have different contents
fi


This will be (probably "much") slower than calling out to external tools.



diff <(grep -E regex file1) <(grep -E regex file2)





share|improve this answer












Assuming your arrays are named "array1" and "array2", bash code to check for equality:



equal=true
if [[ $#array1[@] -ne $#array2[@] ]]; then
equal=false
else
while IFS= read -r elem1 <&3; IFS= read -r elem2 <&4; do
if [[ "$elem1" != "$elem2" ]]; then
equal=false
break
fi
done 3< <(printf "%sn" "$array1[@]" | sort)
4< <(printf "%sn" "$array2[@]" | sort)
fi
if $equal; then
echo arrays have the same contents
else
echo arrays have different contents
fi


This will be (probably "much") slower than calling out to external tools.



diff <(grep -E regex file1) <(grep -E regex file2)






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 29 at 15:30









glenn jackman

49.7k569106




49.7k569106











  • How do I initialize the arrays? Also the 2 < in the diff snippet are they right?
    – Jim
    Nov 29 at 15:58











  • Also what is 3 and 4?
    – Jim
    Nov 29 at 16:46










  • "3" and "4" are file descriptors: I want to be able to read from 2 different files in the same loop, so I red them from different file descriptors. Note how 3< after the loop corresponds with the <&3 in one of the read commands.
    – glenn jackman
    Nov 30 at 0:17











  • The 2 < are correct: this bit 3< is the redirection and this bit <(...) is a process substitution
    – glenn jackman
    Nov 30 at 0:18











  • I can't make that part work. I get an error as if diff us using the outputs of grep as files and says can't open them
    – Jim
    Nov 30 at 8:51
















  • How do I initialize the arrays? Also the 2 < in the diff snippet are they right?
    – Jim
    Nov 29 at 15:58











  • Also what is 3 and 4?
    – Jim
    Nov 29 at 16:46










  • "3" and "4" are file descriptors: I want to be able to read from 2 different files in the same loop, so I red them from different file descriptors. Note how 3< after the loop corresponds with the <&3 in one of the read commands.
    – glenn jackman
    Nov 30 at 0:17











  • The 2 < are correct: this bit 3< is the redirection and this bit <(...) is a process substitution
    – glenn jackman
    Nov 30 at 0:18











  • I can't make that part work. I get an error as if diff us using the outputs of grep as files and says can't open them
    – Jim
    Nov 30 at 8:51















How do I initialize the arrays? Also the 2 < in the diff snippet are they right?
– Jim
Nov 29 at 15:58





How do I initialize the arrays? Also the 2 < in the diff snippet are they right?
– Jim
Nov 29 at 15:58













Also what is 3 and 4?
– Jim
Nov 29 at 16:46




Also what is 3 and 4?
– Jim
Nov 29 at 16:46












"3" and "4" are file descriptors: I want to be able to read from 2 different files in the same loop, so I red them from different file descriptors. Note how 3< after the loop corresponds with the <&3 in one of the read commands.
– glenn jackman
Nov 30 at 0:17





"3" and "4" are file descriptors: I want to be able to read from 2 different files in the same loop, so I red them from different file descriptors. Note how 3< after the loop corresponds with the <&3 in one of the read commands.
– glenn jackman
Nov 30 at 0:17













The 2 < are correct: this bit 3< is the redirection and this bit <(...) is a process substitution
– glenn jackman
Nov 30 at 0:18





The 2 < are correct: this bit 3< is the redirection and this bit <(...) is a process substitution
– glenn jackman
Nov 30 at 0:18













I can't make that part work. I get an error as if diff us using the outputs of grep as files and says can't open them
– Jim
Nov 30 at 8:51




I can't make that part work. I get an error as if diff us using the outputs of grep as files and says can't open them
– Jim
Nov 30 at 8:51

















draft saved

draft discarded
















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f484893%2fcheck-that-2-arrays-of-file-names-are-the-same%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown






Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay