Check that 2 arrays of file names are the same
Clash 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.
bash filenames array
|
show 7 more comments
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.
bash filenames array
3
Usinggrep
ordiff
could 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 offile
? 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
|
show 7 more comments
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.
bash filenames array
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
bash filenames array
edited Nov 29 at 18:22
asked Nov 29 at 11:58
Jim
390213
390213
3
Usinggrep
ordiff
could 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 offile
? 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
|
show 7 more comments
3
Usinggrep
ordiff
could 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 offile
? 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 diff
could help. Add sample data to your post for more infos.– Michael D.
Nov 29 at 12:15
Using
grep
or diff
could 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
|
show 7 more comments
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)
How do I initialize the arrays? Also the 2<
in thediff
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 how3<
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 bit3<
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 ifdiff
us using the outputs ofgrep
as files and says can't open them
– Jim
Nov 30 at 8:51
add a comment |
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)
How do I initialize the arrays? Also the 2<
in thediff
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 how3<
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 bit3<
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 ifdiff
us using the outputs ofgrep
as files and says can't open them
– Jim
Nov 30 at 8:51
add a comment |
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)
How do I initialize the arrays? Also the 2<
in thediff
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 how3<
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 bit3<
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 ifdiff
us using the outputs ofgrep
as files and says can't open them
– Jim
Nov 30 at 8:51
add a comment |
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)
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)
answered Nov 29 at 15:30
glenn jackman
49.7k569106
49.7k569106
How do I initialize the arrays? Also the 2<
in thediff
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 how3<
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 bit3<
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 ifdiff
us using the outputs ofgrep
as files and says can't open them
– Jim
Nov 30 at 8:51
add a comment |
How do I initialize the arrays? Also the 2<
in thediff
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 how3<
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 bit3<
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 ifdiff
us using the outputs ofgrep
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
3
Using
grep
ordiff
could 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