Awk inside expect - linux

I'm having errors when sending this to expect
send "someagent_id=`ps aux | grep someagent| grep -v grep | awk '{print $2}'`\r"
Error:
can't read "2": no such variable
while executing
"send "someagent_id=`ps aux | grep someagent| grep -v grep | awk '{print $2}'`\r""

If all you want is the PID for some process, you don't need all this messy thing: ps aux | grep someagent| grep -v grep | awk '{print $2}'.
You can get a process PID just with pgrep:
In your case it would be:
send "someagent_id=$(pgrep SOMEAGENT)\r"

try this :
someagent_id=$(ps aux | grep someagent| grep -v grep | awk '{print $2}'\r)
send $someagent_id
`` --> previous version
$ --> new version. Both do the same thing

try this
send "someagent_id=`ps aux | grep someagent| grep -v grep | awk '{print \$2}'`\r"
and maybe better with this 1 pipe only version
send "someagent_id=`ps aux | awk '/someagent/ && ! /awk/ {print \$2}'`\r"

With ps options
ps -C someagent -o pid=
send "someagent_id=$(ps -C someagent -o pid=)"

Related

grep a word from a list of file as a result of grep before

I have a command to grep a file with fullpath that contain a "TypeId: 0", here is the command
grep -rnw /home/username/app/data/store/0/part/.mv -e "TypeId: 0" | awk -F ":" '{print $1}'
and here is the result:
/home/username/app/data/store/0/part/.mv/521/1673332792072/segmentconfig.yaml /home/username/app/data/store/0/part/.mv/521/1673333077920/segmentconfig.yaml /home/username/app/data/store/0/part/.mv/521/1673333077920/segmentconfig.yaml.old /home/username/app/data/store/0/part/.mv/515/1672993850766/segmentconfig.yaml /home/username/app/data/store/0/part/.mv/515/1672993850766/segmentconfig.yaml.old /home/username/app/data/store/0/part/.mv/703/1672987004847/segmentconfig.yaml /home/username/app/data/store/0/part/.mv/703/1672987004847/segmentconfig.yaml.old
Now I confuse how to grep "numofvertice" from each file from that list.
Anyone have an idea to solve this?
You could try this:
grep -rnw /home/username/app/data/store/0/part/.mv -e "TypeId: 0" | awk -F ":" '{print $1}'|xargs -I{} grep "numofvertice" {}
Like this (GNU grep):
<STDIN> | grep -oP '\b\S+\.yaml' | xargs cat
Or with ack:
cd /home/username/app/data/store/0/part/.mv
ack -wl -e "TypeId: 0" | xargs cat
From ack --help:
-l, --files-with-matches
Only print filenames containing matches

Searching a specific file system in bash

I have a task which asks to write a script which displays all partitions formatted with a specific file system, given as parameter.
I have written the script but when i run it it displays '0'. What am i doing wrong?
This is my code:
#!/bin/bash
n=sudo parted -l | tail -n +8 | awk '{print $5}' | wc | awk '{print $2}'
m=sudo parted -l | tail -n +8 | awk '{print $5}'
q=sudo parted -l | tail -n +8
for i in $n; do
if [ "[ $m | sed -n ip ]" = "$1" ]; then
echo "$q | sed -n ip"
fi
done
Different approach from yours, but does it do what you need?
lsblk -f | awk '$0 ~ fs {print $NF}' fs=ext2

how to get loaded kernel module file position

command :
lsmod
loaded kernel module list
command :
modinfo `lsmod | awk {'print $1'}` | awk {'print $2'}
loaded kernel module file position
I want list
KernelModule | file position
ex :
xfs | /lib/modules/3.10.0-514.el7.x86_64/kernel/fs/xfs/xfs.ko
how to get example list Command?
Try this:
lsmod | grep -v ^Module | awk '{print $1}' | xargs -n 1 -I MOD sh -c "echo -n MOD; echo -n ' | '; modinfo MOD | grep filename | awk '{print \$2}'"

What happened when redirecting output of tail -f + grep/awk?

i have a growing test.log like this:
abc ID1
aaa ID2
abb ID3
ccc ID4
and i want to save corresponding ID of ".*a.b." to log file like this
$ tail -f test.log | grep --line-buffered '.*a.*b.*' | awk '{print $2}' > a_ID.log
i tried
$ tail -f test.log | grep '.*a.*b.*'
$ tail -f test.log | grep --line-buffered '.*a.*b.*' > a.log
both work fine, but what should be done with awk?
# No output
$ tail -f test.log | awk '{print $2}'
# Obviously nothing in ID.log
$ tail -f test.log | awk '{print $2}' > ID.log
does awk have a '--line-buffered' like grep? how about sed?
Can you try
$ tail -f test.log | awk '{ print $2; fflush(); }'
from the man page it said it will flush the stdout.
This seems to work with mawk.
$ tail -f test.log | awk -W interactive '{print $2}' > ID.log
The -W interactive makes mawk write unbuffered to stdout.

perl linux command not working

cat t.incopt.02.20110221 | awk -F, '{print $1}' | sort | uniq
got unque records
but if i inserted into perl,
#FETCH_REQ_DETAILS = `cat t.incopt.02.20110221 | awk -F\, '{print $1}' \| sort \| uniq`;
if i print the above array vari, i getting entire file content, i guess the linux command not working correctly when i use inside perl,
I think you just need to enclose the command in back tick and escape only the $
#FETCH_REQ_DETAILS = `cat t.incopt.02.20110221 | awk -F, '{print \$1}' | sort | uniq;`
Try the following:
my $cmd='cat t.incopt.02.20110221 | awk -F, \'{print $1}\' | sort | uniq';
#FETCH_REQ_DETAILS = `$cmd`;

Resources