Python - how to fix this line typeerror: not enough arguments for format string - bash

From command line its working but when i include in the Python getting this
Terminal:
$ amixer -c2 | grep "Simple mixer control 'Mic',0" -A 5 | grep "Mono: " | sed -e 's/Capture /\n/g' | tail -1 | awk '{print $2}' | sed -e 's/%]//g' | sed -e 's/\[//g'
88
Python:
tmp = "2"
a = """amixer -c%s | grep "Simple mixer control 'Mic',0" -A 5 | grep "Mono: " | sed -e 's/Capture /\n/g' | tail -1 | awk '{print $2}' | sed -e 's/%]//g' | sed -e 's/\[//g'""" % tmp
print "Reply " + a
a = os.popen(a).read()
print a
Error:
Running: /var/tmp/p/test.py (Sun Dec 8 20:58:07 2013)
Traceback (most recent call last):
File "/var/tmp/p/test.py", line 2, in <module>
a = """amixer -c%s | grep "Simple mixer control 'Mic',0" -A 5 | grep "Mono: " | sed -e 's/Capture /\n/g' | tail -1 | awk '{print $2}' | sed -e 's/%]//g' | sed -e 's/\[//g'""" % tmp
TypeError: not enough arguments for format string
Execution Successful!

You see that thing in one of the sed:
... | sed -e 's/%]//g' | ...
Change that bit to this:
... | sed -e 's/%%]//g' | ...
This is how you escape a % in strings in python.

Related

How to save the output of an sh to a Groovy variable?

I need to store the output of this command into a variable:
sh "curl -s 'http://nexus-cicd.stgcloud.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xml' | grep '<version>.*</version>' | sort --version-sort | uniq | tail -n1 | sed -e 's#\\(.*\\)\\(<version>\\)\\(.*\\)\\(</version>\\)\\(.*\\)#\\3#g'"
I tried the following, but echo output null
parentLast = sh ("curl -s 'http://nexus-cicd.stgcloud.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xml' | grep '<version>.*</version>' | sort --version-sort | uniq | tail -n1 | sed -e 's#\\(.*\\)\\(<version>\\)\\(.*\\)\\(</version>\\)\\(.*\\)#\\3#g'")
echo "$parentLast"

Bash Script: Filter large files for value

I have several config files with around 20k lines each and I need to get some values from them.
I know that each of the values I need starts with a specific word "CONFNET" so I tried to get the values with a while loop, which reads every line.
But unfortunately this is extremely inefficient and slow.
Is there a better solution to this?
for filename in ~/configs/*; do
ip=$(cat $filename | strings | grep -i -A 7 "addnet_outside" | head -7 | grep "IP" | sed "s/IP//" | sed "s/=//" | sed -e 's/^[ \t]*//')
hostname=$(cat $filename | strings | grep -a "Inst:" | head -1 | sed "s/Inst://" | sed -e 's/^[ \t]*//')
while IFS= read -r line; do
object_name=$(echo $line | strings | grep "CONFNET" | sed "s/CONFNET//" | awk '{print $1}')
object_value=$(echo $line | strings | grep "CONFNET" | sed "s/CONFNET//" | awk '{print $3}' | sed -e 's/^[ \t]*//')
if [ ! -z $object_name ] && [ ! -z $object_value ]
then
echo $hostname "->" $object_name ":" $object_value
done < "$filename"
done

Using sed instead of rev

I am running a shell build step in Jenkins (Windows), but, it doesn't seem to recognize the rev command. The command I am trying to run is:
awk '/Connections/||/Endpoint/||/connections/||/endpoint/||/EndPointURI/ {print $config}' "$config" | rev | cut -c 8- | rev >temp11
I have used sed '/\n!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//' to replace each instance of rev.
I have the following:
awk '/Connections/||/Endpoint/||/connections/||/endpoint/||/EndPointURI/ {print $config}' "$config" | sed '/\n!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//' | cut -c 8- | sed '/\n!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//' >temp11
Using this method, I get the following error in the Jenkins output:
+ awk '/Connections/||/Endpoint/||/connections/||/endpoint/||/EndPointURI/ {print $config}' 'C:\Program Files (x86)\Jenkins/workspace/DeploymentFiles/BUILD.xml'
+ sed '/\n!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//'
+ cut -c 8-
sed: -e expression #1, char 9: unknown command: `\'
+ sed '/\n!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//'
sed: -e expression #1, char 9: unknown command: `\'
Build step 'Execute shell' marked build as failure
Finished: FAILURE
Not sure how to fix this or make it work.
Full script:
touch "${JENKINS_HOME}"/workspace/DeploymentFiles/UAT.properties
config=${JENKINS_HOME}/workspace/DeploymentFiles/BUILD.xml
properties=${JENKINS_HOME}/workspace/DeploymentFiles/UAT.properties
a='tibco.clientVar.'
awk '/Connections/||/Endpoint/||/connections/||/endpoint/||/EndPointURI/ {print $config}' "$config" | sed '/\n!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//' | cut -c 8- | sed '/\n!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//' >temp11
awk '/Connections/||/Endpoint/||/connections/||/endpoint/||/EndPointURI/ {getline; print $config}' "$config" |sed '/\n!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//' | cut -c 9- | sed '/\n!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//' >temp22
sed -e s/'<name>'//g -i temp11
sed -i '/^$/d' temp11
sed -e s/'<value>'//g -i temp22
sed -i '/^$/d' temp22
while read -r line
do
echo "${a}$line"
done <temp11 >temp3
sed -i '/^$/d' temp3
paste -d'=' temp3 temp22 > final
sed '/^tibco.clientVar.<checkpoint>/d' final > final.new && mv final.new final
mv final ${properties}

getting a part of the output from a sed command

I have this Command :
cat -n file.log | grep "Start new test" | tail -1 | cut -f 1 | xargs -I % sed -n %',$s/is not alive/&/p' file.log
That gives the output of the whole line :
Jan 19 23:20:33 s_localhost#file platMgt.xbin[3260]: blade 10 is not alive
Jan 19 23:20:33 s_localhost#file platMgt.xbin[3260]: blade 11 is not alive
how can I modify it to get the last part only :
blade 11 is not alive
can I modify that in a way to display :
Error:blade 11 is not alive ?
Thank you for your response
You can use cut to delimit it on the colons and then add the error message:
cat -n file.log | grep "Start new test" | tail -1 | cut -f 1 | xargs -I % sed -n %',$s/is not alive/&/p' file.log | cut -d: -f 4 | xargs -I % echo Error: %
To get the last part after colon awk is better tool:
s='Jan 19 23:20:33 s_localhost#file platMgt.xbin[3260]: blade 10 is not alive'
awk -F':' '{print "Error:" $NF}' <<< "$s"
OUTPUT:
blade 10 is not alive
EDIT: WIth your piped commands you can combine it as:
grep "Start new test" file.log|tail -1|awk -F':' '{print "Error:" $NF}'
PS: Though this whole thing is possible in awk itself.
The following obtains the the last ":" separated field with a sed command,
cat text.txt | sed 's/^.*: \([^:]*$\)/\1/g'

Bash script builds correct $cmd but fails to execute complex stream

This short script scrapes some log files daily to create a simple extract. It works from the command line and when I echo the $cmd and copy/paste, it also works. But it will breaks when I try to execute from the script itself.
I know this is a nightmare of patterns that I could probably improve, but am I missing something simple to just execute this correctly?
#!/bin/bash
priorday=$(date --date yesterday +"%Y-%m-%d")
outputfile="/home/CCHCS/da14/$priorday""_PROD_message_processing_times.txt"
cmd="grep 'Processed inbound' /home/rules/care/logs/RootLog* | cut -f5,6,12,16,18 -d\" \" | grep '^"$priorday"' | sed 's/\,/\./' | sed 's/ /\t/g' | sed -r 's/([0-9]+\-[0-9]+\-[0-9]+)\t/\1 /' | sed 's/ / /g' | sort >$outputfile"
printf "command to execute:\n"
echo $cmd
printf "\n"
$cmd
ouput:
./make_log_extract.sh command to execute: grep 'Processed inbound' /home/rules/care/logs/RootLog.log /home/rules/care/logs/RootLog.log.1
/home/rules/care/logs/RootLog.log.10
/home/rules/care/logs/RootLog.log.11
/home/rules/care/logs/RootLog.log.12
/home/rules/care/logs/RootLog.log.2
/home/rules/care/logs/RootLog.log.3
/home/rules/care/logs/RootLog.log.4
/home/rules/care/logs/RootLog.log.5
/home/rules/care/logs/RootLog.log.6
/home/rules/care/logs/RootLog.log.7
/home/rules/care/logs/RootLog.log.8
/home/rules/care/logs/RootLog.log.9 | cut -f5,6,12,16,18 -d" " | grep
'^2014-01-30' | sed 's/\,/./' | sed 's/ /\t/g' | sed -r
's/([0-9]+-[0-9]+-[0-9]+)\t/\1 /' | sed 's/ / /g' | sort
/home/CCHCS/da14/2014-01-30_PROD_message_processing_times.txt
grep: 5,6,12,16,18: No such file or directory
As grebneke comments, do not store the command and then execute it.
What you can do is to execute it but firstly print it: Bash: Print each command before executing?
priorday=$(date --date yesterday +"%Y-%m-%d")
outputfile="/home/CCHCS/da14/$priorday""_PROD_message_processing_times.txt"
set -o xtrace # <-- set printing mode "on"
grep 'Processed inbound' /home/rules/care/logs/RootLog* | cut -f5,6,12,16,18 -d\" \" | grep '^"$priorday"' | sed 's/\,/\./' | sed 's/ /\t/g' | sed -r 's/([0-9]+\-[0-9]+\-[0-9]+)\t/\1 /' | sed 's/ / /g' | sort >$outputfile"
set +o xtrace # <-- revert to normal

Resources