bash substring from xargs piped [duplicate] - bash

This question already has answers here:
How to process each output line in a loop?
(7 answers)
Closed 4 years ago.
Reading how to get substring in bash, I found out the following commands works:
var="/aaa/bbb/ccc/ddd"
echo ${var##*/}
Which produces "ddd"
My complete problem is to make this dinamically reading from a pipe:
I want to achieve something like this:
echo /aaa/bbb/ccc/ddd | xargs echo ${MyVar##*/}
But it is not working.
I tried to use -I option follwing way:
echo /aaa/bbb/ccc/ddd | xargs -I MyVar echo ${MyVar##*/}
And did not work either, (I think it does not interpolate it)
Any way to solve this?
Is it posible to achieve also to read substring left part, instead of right part?

You may use it like this:
echo '/aaa/bbb/ccc/ddd' | xargs -I {} bash -c 'echo "${1##*/}"' - {}
ddd
or just use awk:
echo '/aaa/bbb/ccc/ddd' | awk -F/ '{print $NF}'

You can do this :
echo '/aaa/bbb/ccc/ddd' | sed -E 's/(.*\/)(.*)/\2/g' | xargs -n 1 $1
Hope it helps!

Related

Unix Shell Script: Remove common prefix from a variable [duplicate]

This question already has answers here:
Remove a fixed prefix/suffix from a string in Bash
(9 answers)
Closed 2 years ago.
I have 2 variables one which is holding the prefix and other one the complete string.
For e.g
prefix="TEST_"
Str="TEST_FILENAME.xls"
I want the the Str to be compared against prefix and remove that common characters 'TEST_' and i want the output as FILENAME.xls. Please advise if it can be done with minimal lines of coding. Thanks a lot.
Using BASH you can do:
prefix="TEST_"
str="TEST_FILENAME.xls"
echo "${str#$prefix}"
FILENAME.xls
If not using BASH you can use sed:
sed "s/^$prefix//" <<< "$str"
FILENAME.xls
Try this:
$ Str=$(echo $Str | sed "s/^${prefix}//")
$ echo $Str
FILENAME.xls
Or using awk:
$ echo $Str | awk -F $prefix '{print $2}'
FILENAME.xls

Using a bash variable to pass multiple -e clauses to sed [duplicate]

This question already has answers here:
Why does shell ignore quoting characters in arguments passed to it through variables? [duplicate]
(3 answers)
Closed 6 years ago.
I'm creating a variable from an array which build up multiple -e clauses for a sed command.
The resulting variable is something like:
sedArgs="-e 's/search1/replace1/g' -e 's/search2/replace2/g' -e 's/search3/replace3/g'"
But when I try to call sed with this as the argument I get the error sed: -e expression #1, char 1: unknown command: ''
I've tried to call sed the following ways:
cat $myFile | sed $sedArgs
cat $myFile | sed ${sedArgs}
cat $myFile | sed `echo $sedArgs`
cat $myFile | sed "$sedArgs"
cat $myFile | sed `echo "$sedArgs"`
and all give the same error.
UPDATE - Duplicate question
As has been identified, this is a 'quotes expansion' issue - I thought it was something sed specific, but the duplicate question that has been identified put me on the right track.
I managed to resolve the issue by creating the sedArgs string as:
sedArgs="-e s/search1/replace1/g -e s/search2/replace2/g -e s/search3/replace3/g"
and calling it with:
cat $myFile | sed $sedArgs
which works perfectly.
Then I took the advice of tripleee and kicked the useless cat out!
sed $sedArgs $myFile
also works perfectly.
Use BASH arrays instead of simple string:
# sed arguments in an array
sedArgs=(-e 's/search1/replace1/g' -e 's/search2/replace2/g' -e 's/search3/replace3/g')
# then use it as
sed "${sedArgs[#]}" file
Here is no sane way to do that, but you can pass the script as a single string.
sedArgs='s/search1/replace1/g
s/search2/replace2/g
s/search3/replace3/g'
: then
sed "$sedArgs" "$myFile"
The single-quoted string spans multiple lines; this is scary when you first see it, but perfectly normal shell script. Notice also how the cat is useless as ever, and how the file name needs to be quoted, too.

how to read a value from filename and insert/replace it in the file?

I have to run many python script which differ just with one parameter. I name them as runv1.py, runv2.py, runv20.py. I have the original script, say runv1.py. Then I make all copies that I need by
cat runv1.py | tee runv{2..20..1}.py
So I have runv1.py,.., runv20.py. But still the parameter v=1 in all of them.
Q: how can I also replace v parameter to read it from the file name? so e.g in runv4.py then v=4. I would like to know if there is any one-line shell command or combination of commands. Thank you!
PS: direct editing each file is not a proper solution when there are too many files.
Below for loop will serve your purpose I think
for i in `ls | grep "runv[0-9][0-9]*.py"`
do
l=`echo $i | tr -d [a-z.]`
sed -i 's/v/'"$l"'/g' runv$l.py
done
Below command was to pass the parameter to script extracted from the filename itself
ls | grep "runv[0-9][0-9]*.py" | tr -d [a-z.] | awk '{print "./runv"$0".py "$0}' | xargs sh
in the end instead of sh you can use python or bash or ksh.

Combining linux shell sed command [duplicate]

This question already has answers here:
Combining two sed commands
(2 answers)
Closed 1 year ago.
I had three sed commands which are
sed 's/"//g'
sed 's/ *//g'
echo $1 | sed 's/.*'$2':\([^,}]*\).*/\1/'
How can I combine the above three sed commands together?
You can combine multiple commands using -e
For example :
sed -e 'command' -e 'command'
Hope this helps .
To join sed command, you can also use ; without the -e like this:
sed 'code;code;code' file
eks:
sed 's/"//g;s/ *//g' file

How to do a script in BASH which takes random text from file? [duplicate]

This question already has answers here:
What's an easy way to read random line from a file?
(13 answers)
Closed 8 years ago.
I have file like:
aaa
bbb
ccc
ddd
eee
And I want to do a script in BASH which can takes random line of this text file, and return it to me as variable or something.
I hear it can be done with some AWK.
Any ideas?
UPDATE: I now using this:
shuf -n 1 text.txt
Thanks you all for help!
I used a script like this to generate a random line from my singature-quotes file:
#!/bin/bash
QUOTES_FILE=$HOME/.quotes/quotes.txt
numLines=`wc -l $QUOTES_FILE | cut -d" " -f 1`
random=`date +%N`
selectedLineNumber=$(($random - $($random/$numLines) * $numLines + 1))
selectedLine=`head -n $selectedLineNumber $QUOTES_FILE | tail -n 1`
echo -e "$selectedLine"
I would use sed with p argument...
sed -n '43p'
where 43 could be a variable ...
i don't know much about awk but i guess you could do almost the same thing (however i don't know if awk is turing complete...)
here's a bash way, w/o any external tools
IFS=$'\n'
set -- $(<"myfile")
len=${##}
rand=$((RANDOM%len+1))
linenum=0
while read -r myline
do
(( linenum++ ))
case "$linenum" in
$rand) echo "$myline";;
esac
done <"myfile"

Resources