I have a list of groups and I need to extract users by knowing partially memberOf value
Example:
# for group AAA
ldapsearch -w V1ZEYK -D "cn=XXXXXX,ou=Service Users,ou=User Accounts,dc=uuu,dc=yyy,dc=xxx,dc=net" -H ldaps://<link>:<port> -b "dc=uuu,dc=yyy,dc=xxx,dc=net" -s sub memberOf="CN=AAA,OU=Groups,DC=uuu,DC=yyy,DC=xxx,DC=net" | grep "cn:"
# returns "cn: 12345"
# for group BBB
... -s sub memberOf="CN=BBB,DC=uuu,DC=yyy,DC=xxx,DC=net" | grep "cn:"
# returns nothing, meaning memberOf DC part is different that I dont know of
How should I pass partial filter so the search could return user cns?
Is there a way (and should I) pass wildcard filters for flags -D and -b?
Tried:
... -s sub memberOf="CN=BBB*"...
... -s sub memberOf="*CN=BBB*"...
# returns nothing
The LDAP specification do not allow substring searches of Distinguished Names.
(like "CN=BBB,DC=uuu,DC=yyy,DC=xxx,DC=net")
I think you will need to write some code.
Related
In bash shell scripting, I have users.txt file.
sort -t: -k 3n users.txt gives me
gg:g#g.g:
ff:f#f.f:5
gdr:d#sd.ds:5
g:w#f.com:8
r:w#d.com:85
qaws:aaa#d.com:789
qaz:a#s.com:789
vcf:d#c.com:855
op:popo#ppoo.po:6465
zx:a#c.com:7845
az:a#aa.com:7894
w:a#a.com:7894
h:g#g.com:7899
lp:l#l.com:8888
t:s#s.com:85474
dssdfsdf:fssdf#mbhkj.sdf:654654
u:f#g.comn:6546546
a:a#a.com:7894561
tt:t#t.t:1234567890
This is ok. But I need 3rd column which contain only phone number in reverse order i.e. descending order. How to achieve that?
I need like
tt:t#t.t:1234567890
a:a#a.com:7894561
u:f#g.comn:6546546
dssdfsdf:fssdf#mbhkj.sdf:654654
t:s#s.com:85474
lp:l#l.com:8888
and so on...
You could add an r (short for "reverse") to the order column specification:
$ sort -t: -k 3nr users.txt
# Here ---------^
There are supposed to be 2 files. One file has list of users and the other files has list of passwords.
User file has 5 users and password file also has 5 passwords. What I have to do here is create a new file and pair each user with all the available passwords in password file. For example: user 1 is to be paired with password 1, 2,3,4,5. user 2 is to be paired with password 1,2,3,4,5 and so on.
My question is:
1. What loop should be used here?
2. My head says for pairing we need to use nested for loop. is it correct?
3. I can somehow imagine the 1st part of copying the contents but I'm not able to picture how to pair them. So I need help and suggestions.
EDIT:
Sample input are the 2 files named Username and Password.
Username file:
User1
User2
..
User5
Password file:
Pass1
Pass2
..
Pass5
Expected output is:
User1-Pass1
User1-Pass2
..
User1-Pass5
User2-Pass1
User2-Pass2
..
User2-Pass5
and so on till we reach User5-Pass5
Thanks.
Ad 1., 2. Yes, nested loop required.
Let's expect one user per line and one password per line, then code would be:
> concat # delete file content
for usr in `cat file_with_users`
do
for pwd in `cat file_with_passwords`
do
echo "$usr-$pwd" >> result_file
done
done
If you write input sample and expected output, I can write something more specific.
always there is a better way
$ join -t, -j 99 users pwords | cut -d, -f2-
for example
$ echo u{1..5} | tr ' ' '\n' > users
$ echo p{1..5} | tr ' ' '\n' > pwords
$ join -t, -j 99 users pwords | cut -d, -f2-
u1,p1
u1,p2
u1,p3
u1,p4
u1,p5
u2,p1
u2,p2
...
u5,p4
u5,p5
for - delimiter change to join -t- -j 99 users pwords | cut -d- -f2-
This is using a fictional column (99th) to create a join between each rows, which is called cross-product. Since the key is missing in the first position of the output we need to cut it out at the end.
Do either of these help:
grep -Fwf file1 file2 or
awk 'NR==FNR{A[$1];next}$1 in A' file1 file2
I have two files: one contains a list of AP names, and another list contains AP names again, but this time the controller IP for each AP is listed before the AP's name.
File 1:
AP1
AP2
Ap3
AP4
Ap5
Ap6
...
File 2:
1.1.1.1,Ap1
2.2.2.2,Ap2
3.3.3.3,Ap3
4.4.4.4,Ap4
6.6.6.6,Ap6
...
How can I match up the names from file 1 with the names in file 2 so that the output resembles the following?
1.1.1.1,Ap1
2.2.2.2,Ap2
3.3.3.3,Ap3
4.4.4.4,Ap4
IP Not Found,Ap5
6.6.6.6,Ap6
I was thinking that I could use the comm command, but I do not know of a good way to only compare the names and not the IPs. I could also just grep for every single name, but that would take forever (there are around 8,000 AP names).
The join command will do the job (note that both files have to be sorted by AP name first; your example data already is, but if your real-world data isn't, run the first file through sort -f, and the second file through sort -f -t , -k 2).
join -i -t , -1 1 -2 2 -a 1 -o 2.1,1.1 -e "IP Not Found" file1.txt file2.txt
-i means ignore case, -t , means fields are separated by commas, -1 1 means join on the first (only) field of the first file; -2 2 means join on the second field of the second file. -a 1 means include rows from the first file that don't have any matches. -o 2.1,1.1 specifies the output format: the first field of the second file (IP), then the first field of the first file (AP). -e "IP Not Found" means to output "IP Not Found" in place of an empty field.
This will output
1.1.1.1,AP1
2.2.2.2,AP2
3.3.3.3,Ap3
4.4.4.4,AP4
IP Not Found,Ap5
6.6.6.6,Ap6
This awk snippet should do it:
awk 'BEGIN{FS=","}
(FNR==NR){a[tolower($2)]=$0}
(FNR!=NR){if (a[tolower($1)]!="")
print a[tolower($1)]
else
print "IP Not Found," $1}' file2.txt file1.txt
producing in your case:
1.1.1.1,Ap1
2.2.2.2,Ap2
3.3.3.3,Ap3
4.4.4.4,Ap4
IP Not Found,Ap5
6.6.6.6,Ap6
I'm trying to retrieve a memory value from file, and compare it to reference value. But one thing at a time....
I've attempted using set/source/grep/substring to variable but non of them actually worked. Then I found a way to do it using a for loop (see code).
The issue: I'm receiving the entire string from the file, but I can't manage to get rid of the last character in it.
#!/bin/bash
#source start_params.properties
#mem_val= "$default.default.minmaxmemory.main"
#mem_val= grep "default.default.minmaxmemory.main" start_params.properties
for mLine in $(grep 'default.default.minmaxmemory.main' start_params.properties)
do
echo "$mLine"
done
echo "${mLine:4:5}" # didn't get rid of the last `m` in `-max4095m`
v1="max"
v2="m"
echo "$mLine" | sed -e "s/.*${v1}//;s/${v2}.*//" #this echo the right value.
The loop iterates twice:
First output: default.default.minmaxmemory.main=-min512m
Second output: -max4096m
Then the sed command output is 4096,but how can I change the last line in the code S.T. it'll store the value in a variable?
Thank you for your suggestions,
You could use grep to filter the max part and then another a grep -o to extract the numbers:
echo "$mLine" | grep "$max" | grep -o '[[:digit:]]*'
$ sed '/max[0-9]/!d; s/.*max//; s/m//' start_params.properties
4096
remove lines not matching max[0-9]
remove first part of line until max
remove final m
Example file.txt:
100 foo
2 bar
300 tuu
When using sort -k 1,1 file.txt, the order of lines will not change, though we are expecting :
2 bar
100 foo
300 tuu
How to sort a field consisting of numbers based on the absolute numerical value?
Take a peek at the man page for sort...
-n, --numeric-sort
compare according to string numerical value
So here is an example...
sort -n filename
If you are sorting strings that are mixed text & numbers, for example filenames of rolling logs then sorting with sort -n doesn't work as expected:
$ ls |sort -n
output.log.1
output.log.10
output.log.11
output.log.12
output.log.13
output.log.14
output.log.15
output.log.16
output.log.17
output.log.18
output.log.19
output.log.2
output.log.20
output.log.3
output.log.4
output.log.5
output.log.6
output.log.7
output.log.8
output.log.9
In that case option -V does the trick:
$ ls |sort -V
output.log.1
output.log.2
output.log.3
output.log.4
output.log.5
output.log.6
output.log.7
output.log.8
output.log.9
output.log.10
output.log.11
output.log.12
output.log.13
output.log.14
output.log.15
output.log.16
output.log.17
output.log.18
output.log.19
output.log.20
from man page:
-V, --version-sort
natural sort of (version) numbers within text
Well, most other answers here refer to
sort -n
However, I'm not sure this works for negative numbers. Here are the results I get with sort version 6.10 on Fedora 9.
Input file:
-0.907928466796875
-0.61614990234375
1.135406494140625
0.48614501953125
-0.4140167236328125
Output:
-0.4140167236328125
0.48614501953125
-0.61614990234375
-0.907928466796875
1.135406494140625
Which is obviously not ordered by numeric value.
Then, I guess that a more precise answer would be to use sort -n but only if all the values are positive.
P.S.: Using sort -g returns just the same results for this example
Edit:
Looks like the locale settings affect how the minus sign affects the order (see here). In order to get proper results I just did:
LC_ALL=C sort -n filename.txt
You have to use the numeric sort option:
sort -n -k 1,1 File.txt
Use sort -n or sort --numeric-sort.
You must do the following command:
sort -n -k1 filename
That should do it :)
Use sort -nr for sorting in descending order. Refer
Sort
Refer the above Man page for further reference
echo " Enter any values to sorting: "
read n
i=0;
t=0;
echo " Enter the n value: "
for(( i=0;i<n;i++ ))
do
read s[$i]
done
for(( i=0;i<n;i++ ))
do
for(( j=i+1;j<n;j++ ))
do
if [ ${s[$i]} -gt ${s[$j]} ]
then
t=${s[$i]}
s[$i]=${s[$j]}
s[$j]=$t
fi
done
done
for(( i=0;i<n;i++ ))
do
echo " ${s[$i]} "
done