Pipe to command that does not accept piping - linux

I want to use MPC (CLI interface to MPD) but unfortunately to me it does not accept piping.
So something like:
ll | grep "pattern" | sed 's/this/that/' | mpc
does not work for me, nor
ll | grep "pattern" | sed 's/this/that/' | mpc -
This
MPCTMP=`ll | grep "pattern" | sed 's/this/that/'` && echo $MPCTMP
works as expected, but this:
MPCTMP=`ll | grep "pattern" | sed 's/this/that/'` && mpc $MPCTMP
does not return results, variable is not understood but this program
I'm new to Linux and could not find anything searching with Google
Thanks

Try xargs
ll | grep "pattern" | sed 's/this/that/' | xargs mpc

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

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}'"

xargs -I % /path/ %

If torrent has a problem like deleted data on hard drive in id column it has number like "ID*".
I want to filter IDs of torrents in torrent list which have a symbol "*" at the end of id(LIKE ID* or 1*,2*,25*) and delete them from torrent list.
Full command is:
/usr/bin/transmission-remote 127.0.0.1:9091 --auth ts:ts -l | grep "*" | awk '{print $1}' \
| xargs -n 1 -I % /usr/bin/transmission-remote 127.0.0.1:9091 --auth ts:ts -t% -r
I expected result:
/usr/bin/transmission-remote 127.0.0.1:9091 --auth ts:ts -t ID* -r
But something went wrong.
Transmission said that:
127.0.0.1:9091/transmission/rpc/ responded: "success"
But torrent didn't delete from list.
How I can see the final result to compare with expected?
To get IDs :
transmission-remote -l | grep '*' | awk '{print $1}' | grep -o '[0-9]*'
The full command :
transmission-remote -l | grep '*' | awk '{print $1}' | grep -o '[0-9]*' | tr "\\n" "," | xargs -n 1 -I \% transmission-remote -t \% -r
Done and done (:
With the added improvement of using "tr" to join all torrent IDs and avoid running everything in a loop ( Transmission-RPC is extremely resource intensive to call repeatedly )

How to concatenate extra value to output of one command and pass it to another Unix command

I have this command
grep 'Some pattern' location/*filename* | cut -d ' ' -f2 | cut -d '}' -f2 |
grep -v '^$' | head -1
The above command is giving me output as some file name, such as:
abc.txt
Now I want to grep another line from this file abc.txt with matching some specific pattern, so I did this
grep 'some pattern' location/*filename* | grep -iv .tar | grep -v backup |
grep -v bkp | cut -d ' ' -f2 | cut -d '}' -f2 | head -1 | xargs grep 'some pattern'
It's not working because the abc.txt file is not present in my current directory.
Can anybody tell me how to append location path to abc.txt before passing it to xargs grep command?
You can try this,
grep 'some pattern' location/*filename* | grep -iv .tar | grep -v backup |
grep -v bkp | cut -d ' ' -f2 | cut -d '}' -f2 | head -1 | xargs |
sed 's#.*#/path/to/dir/&#g' | xargs grep 'some pattern'
If cmd produces output "bar", but you want "foobar", you can simply do:
printf foo; cmd
In your case, if you want to prepend /p/a/t/h to your output, just do:
{ printf /p/a/t/h/; grep 'some pattern' filename | ... | head -1; } | xargs ...
But you're probably better off just replacing the head -1 with sed -e 1s#^#/p/a/t/h/# -e 1q
Superficially, if the first command sequence gives you the name of a file in some directory, you need to use find to get the path name to the file:
find . -type f -name $(grep 'Some pattern' location/*filename* | cut -d ' ' -f2 |
cut -d '}' -f2 | grep -v '^$' | head -1) \
-exec grep 'some pattern' {} +
You don't need a backslash after the first line (though it would do no harm); you do need the backslash after the second line. You might specify a different starting point than . for the search.
Hi everybody Thanks for your suggestion.. any how I also found some solution for my problem..
FYI
here is what I used..
echo "dev/temp/text/"$(grep 'Some pattern' location/*filename* | cut -d ' ' -f2 | cut -d '}' -f2 | grep -v '^$' | head -1) | xarg grep 'some pattern'
A couple of ideas:
... | head -1 | sed 's,^,location/,' | xargs grep 'some pattern'
or
... | head -1 | xargs -I FILE grep 'some pattern' location/FILE

How do i append some text to pipe without temporary file

I am trying to get the max version number from a directory where i have several versions of one program
for example if output of ls is
something01_1.sh
something02_0.1.2.sh
something02_0.1.sh
something02_1.1.sh
something02_1.2.sh
something02_2.0.sh
something02_2.1.sh
something02_2.3.sh
something02_3.1.2.sh
something.sh
I am getting the max version number with the following -
ls somedir | grep some_prefix | cut -d '_' -f2 | sort -t '.' -k1 -r | head -n 1
Now if at the same time i want to check it with the version number which i already have in the system, whats the best way to do it...
in bash i got this working (if 2.5 is the current version)
(ls somedir | grep some_prefix | cut -d '_' -f2; echo 2.5) | sort -t '.' -k1 -r | head -n 1
is there any other correct way to do it?
EDIT: In the above example some_prefix is something02.
EDIT: Actual Problem here is
(ls smthing; echo more) | sort
is it the best way to merge output of two commands/program for piping into third.
I have found the solution. The best way it seems is using process substitution.
cat <(ls smthing) <(echo more) | sort
for my version example
cat <(ls somedir | grep some_prefix | cut -d '_' -f2) <(echo 2.5) | sort -t '.' -k1 -r | head -n 1
for the benefit of future readers, I recommend - please drop the lure of one-liner and use glob as chepner suggested.
Almost similar question is asked on superuser.
more info about process substitution.
Is the following code more suitable to what you're looking for:
#/bin/bash
highest_version=$(ls something* | sort -V | tail -1 | sed "s/something02_\|\.sh//g")
current_version=$(echo $0 | sed "s/something02_\|\.sh//g")
if [ $current_version > $highest_version ]; then
echo "Uh oh! Looks like we need to update!";
fi
You can try something like this :
#! /bin/bash
lastversion() { # prefix
local prefix="$1" a=0 b=0 c=0 r f vmax=0
for f in "$prefix"* ; do
test -f "$f" || continue
read a b c r <<< $(echo "${f#$prefix} 0 0 0" | tr -C '[0-9]' ' ')
v=$(((a*100+b)*100+c))
if ((v>vmax)); then vmax=$v; fi
done
echo $vmax
}
lastversion "something02"
It will print: 30102

Resources