How can i run last command for every matched line in awk?
ypcat passwd | awk -F":" '/:John / { system("last" $1) }'
I'm trying to execute the last command for every user that is named John but It does not print anything.
Insert a whitespace after last:
ypcat passwd | awk -F":" '/:John / {system("last " $1)}'
Your approach is wrong, awk is not shell. awk is designed to manipulate text not to call other tools from, that is what a shell is for This may be what you want, depending on what last is/does:
ypcat passwd | awk -F":" '/:John /{print $1}' | xargs last
or:
ypcat passwd | awk -F":" '/:John /{print $1}' | xargs -n 1 last
Related
i have "handler: xyz.lambda_handler" text in one file and i want "xyz.lambda_handler" i.e text present next to "handler:" as output using shell script, how can i do this.
I have tried
awk -F '${handler}' '{print $1}' filename | awk '{print $2}
grep handler filename
command but not getting correct output
as mentioned in qtn.
I combined two commands and i got my answer
grep Handler: filename | awk -F '${handler}' '{print $1}' | awk '{print $2}'
grep givepattern givefilename | awk -F '${givepattern}' '{print $1}' | awk '{print $2}'
It's grep, not greap. To print only the matched parts of a matching line, use option -o.
grep -o xyz.lambda_handler filename
This gives me the duplicates and the number of times it is repeated
$ awk -F "\"*,\"*" '{print $2}' file.csv | sort | uniq -c | sort -nr | head -n 2
4 12345
3 56789
What I then want to do is add the first column (3+4). I can do this if i write the output above to a file test. I can do this as follows:
$ awk -F" " '{print $1}' test
4
3
$ awk -F" " '{print $1}' test | paste -sd+
4+3
$ awk -F" " '{print $1}' test | paste -sd+ | bc
7
But I want to be able to do this in 1 line, and ideally don't want to write to a file, would like to understand why the following does not work
awk -F "\"*,\"*" '{print $2}' file.csv | sort | uniq -c | sort -nr | head -n 2 | awk -F" " '{print $1}' | paste -sd+ | bc
My 2nd awk seems to not like the input.
Can anyone advise how I do this, and what I am doing wrong?
EDIT1 - file.csv looks like:
"Date","Number"
"2015-11-01","12345"
"2015-11-01","12345"
"2015-11-01","12345"
"2015-11-01","12345"
"2015-11-01","56789"
"2015-11-01","56789"
"2015-11-01","56789"
awk to the rescue!
... | sort -nr | awk 'NR<=2{sum+=$1} END{print sum}'
you can pick the first two rows and summation in awk as well.
how can i make a loop for users of server using shell script
i wrote this code ..
#!/bin/bash
b=`awk -F: '{ print $1 }' /etc/passwd | sort`
for $b in /home/$b/ /home/$b/ /home/$b/
echo "$b"
done
i want to loop all users and show its
the users like in file
/etc/passwd
like :
root,admin,cpanel,adm,mysql,user1,user2,user3,user4,user5
i want the output :
/home/adm
/home/root
/home/admin
/home/mysql
/home/user1
/home/user2
and thanks
To print /home/ before each user name:
$ awk -F: ' { print "/home/"$1 }' /etc/passwd | sort
/home/avahi
/home/backup
/home/bin
/home/clamav
/home/colord
/home/daemon
[...snip...]
If you want actual home directories, you can find them in field 6:
$ awk -F: ' { print $6 }' /etc/passwd | sort
/dev/null
/home/john1024
/nonexistent
/root
You can also access the sixth field using cut:
cut -d: -f6 /etc/passwd | sort
Printing multiple directories
As per the revised question in the comments:
$ awk -F: ' { p="/home/"$1; printf "%s\n%s\n%s\n",p"/www",p"/ftp",p"/etc" }' /etc/passwd | sort
/home/avahi/etc
/home/avahi/ftp
/home/avahi/www
/home/backup/etc
/home/backup/ftp
/home/backup/www
[...snip...]
I am trying to open some files as awk's output; the command is:
grep "formatDate\s=" "js/components/" | awk '{print $1}' | awk -F ":" '/1/ {print $1}'
and it (seems to) work correctly.
If I try to open that output as vim's tabs, like this:
vim -p ${ grep "formatDate\s=" "js/components/" | awk '{print $1}' | awk -F ":" '/1/ {print $1}' }
then I get:
-bash: ${ grep "formatDate\s=" "js/components/" | awk '{print $1}' | awk -F ":" '/1/ {print $1}' }: bad substitution
Any help? Thanks.
The way to execute a command is $(), whereas you are using ${}.
Hence, this should work:
vim -p $(grep "formatDate\s=" "js/components/" | awk '{print $1}' | awk -F ":" '/1/ {print $1}')
I have a file with very simple syntax:
cat /tmp/test
ARCH=""prtconf -b | awk '/^name:/ {print $2}'
I tried to grep it:
cat /tmp/test | grep "prtconf -b | awk '/^name:/ {print $2"
ARCH=""prtconf -b | awk '/^name:/ {print $2}'
Let's make grep string a little longer, add } to the end:
cat /tmp/test | grep "prtconf -b | awk '/^name:/ {print $2"}
Nothing found
Why when I add } to the end of the line grep stop working?
OS is Solaris 10U11
$2 refers to command-line parameter so here it will substitute blank character in a patter. So you'l need to escape $ by slash like \$
cat /tmp/test | grep "prtconf -b | awk '/^name:/ {print \$2}"
Without adding } in your patter it was working because it was matching actual pattern as prtconf -b | awk '/^name:/ {print for your input. But if you add } in your patter then it will try to match prtconf -b | awk '/^name:/ {print } (which isn't there in your file so it won't show output.)