booyaka
November 22, 2013, 9:05am
1
Hi All,
I have to do one simple script in AWK. I must to show all users in the group and the group name is called from the keyboard.
I know that I can get groups etc. from /etc/group file.
#!/bin/bash
echo "Group name: "
read name
awk '{split($0,array,":")} {if(array[1] == \"$name\") {print array[3]}}' /etc/group
There is something wrong with the if instruction, but I don't have idea how to do is correctly.
Thank's
Yoda
November 22, 2013, 9:22am
2
Define awk variable and use it:
awk -v N="$name" '{split($0,array,":");if(array[1] == N){print array[3]}}' /etc/group
booyaka:
Hi All,
I have to do one simple script in AWK. I must to show all users in the group and the group name is called from the keyboard.
I know that I can get groups etc. from /etc/group file.
#!/bin/bash
echo "Group name: "
read name
awk '{split($0,array,":")} {if(array[1] == \"$name\") {print array[3]}}' /etc/group
There is something wrong with the if instruction, but I don't have idea how to do is correctly.
Thank's
Try
if(array[1] ~ /'$name'/)
OR
$ awk -v name=$name '{split($0,array,":");if(array[1] == name )print array[3]}' /etc/group
Subbeh
November 22, 2013, 9:25am
4
An easier way to get the group members is this:
awk -F: -v n="$name" '$1==n{print $4}' /etc/group
RudiC
November 22, 2013, 10:13am
5
grep -E "^$name:" /etc/group | { IFS=: read _ _ _ users ; echo $users; }
booyaka
November 22, 2013, 10:20am
6
Thank's. Now another problem with another Script but all the time in AWK.
So, I have to write script that will split every line which is longer than 'n' on a 'k' parts, and all of the parts must be <= n
Code, but there are some errors. What's the problem?
#!/bin/bash
echo "Max length of line: "
read n
awk '
function pomnoz(a, b){
return 1/(1/a / b);
};
{ MAX = $n };
{
LEN = length(\$0);
if(LEN % MAX != 0){
N = (LEN - (LEN % MAX)) / MAX + 1;
};
if(LEN % MAX == 0){
N = LEN / MAX;
};
for(x = 0; x < N; x++){
POS = (pomnoz(x, MAX) + 1);
STR = substr(\$0, POS, MAX);
{print STR};
}
}
' data
RudiC
November 22, 2013, 10:31am
7
Please open a new thread for a new problem, and, while doing that, explain a bit more what you want to achieve and where you come from. Actually, I'm struggling with both your request text and your code snippet. What's the role of " 'k' parts" ? What's your function doing (other than multiplying its two parameters, without div zero checking)? Please be aware that $n in an awk script does NOT refer to a shell variable n . In your above code, n is not assigned a value and thus $n refers to the entire input string.