How i can parse the following function command line:
The separtor is (\s-\w\s) like -c or -d or -n
C:/my app/bin/Reader.dll -n Proc_20ms -c C:/Users/Braun/Desktop/test.csv -t Continue the simulation from the first line of the csv-file -j none -V errors and warnings (default) -d ,
to:
Match1: C:/my app/bin/Reader.dll
Match2: -n
Match3: Proc_20ms
Match4: -c
Match5: C:/Users/Braun/Desktop/test.csv
Match6: -t
Match7: Continue the simulation from the first line of the csv-file
Match8: -j
Match9: none
Match10: -V
Match11: errors and warnings (default)
Match12: -d
Match13: ,
Thanks.
Just put -\w inside a capturing group and then use this regex in re.split function. capturing group is necessary, so that it would keep the delimiter (ie, only the chars present inside the capturing group).
>>> s = 'C:/my app/bin/Reader.dll -n Proc_20ms -c C:/Users/Braun/Desktop/test.csv -t Continue the simulation from the first line of the csv-file -j none -V errors and warnings (default) -d ,'
>>> for i in re.split(r'\s(-\w)\s', s):
print(i)
C:/my app/bin/Reader.dll
-n
Proc_20ms
-c
C:/Users/Braun/Desktop/test.csv
-t
Continue the simulation from the first line of the csv-file
-j
none
-V
errors and warnings (default)
-d
,
Related
I am trying to write a shell script that reads a file line by line and executes a command with its arguments taken from the space-delimited fields of each line.
To be more precise, I need to download a file from an URL which is given in the second column to the path given in the first column using wget. But I don't know how to load this file and get the values in script.
File.txt
file-18.log https://example.com/temp/file-1.log
file-19.log https://example.com/temp/file-2.log
file-20.log https://example.com/temp/file-3.log
file-21.log https://example.com/temp/file-4.log
file-22.log https://example.com/temp/file-5.log
file-23.pdf https://example.com/temp/file-6.pdf
Desired output is
wget url[1] -o url[0]
wget https://example.com/temp/file-1.log -o file-18.log
wget https://example.com/temp/file-2.log -o file-19.log
...
...
wget https://example.com/temp/file-6.pdf -o file-23.pdf
Use read and a while loop in bash to iterate over the file line-by-line and call wget on each iteration:
while read -r NAME URL; do wget "$URL" -o "$NAME"; done < File.txt
Turning a file into arguments to a command is a job for xargs:
xargs -a File.txt -L1 wget -o
xargs -a File.txt: Extract arguments from the File.txt file.
-L1: Pass all arguments from 1 line to the command.
wget -o Pass arguments to the wget command.
You can count, using a for loop and the output of seq like so:
In bash, you can add numbers using $((C+3)).
This will get you:
COUNT=6
OFFSET=18
for C in `seq "$((COUNT-1))"`; do
wget https://example.com/temp/file-${C}.log -o file-$((C+OFFSET-1)).log
done
wget https://example.com/temp/file-${COUNT}.pdf -o file-$((COUNT+OFFSET-1)).pdf
Edit: Sorry, I misread your question. So if you have a file with the file mappings, you can use awk to get the URL and the FILE and then do the download:
cat File.txt | while read L; do
URL="$(echo "${L}" | awk '{print $1}'"
FILE="$(echo "${L}" | awk '{print $2}'"
wget "${URL}" -o "${FILE}"
done
I am trying to fetch the no. of rows for a particular ID using kubectl but instead getting some extra data.
Command:
kubectl exec abc-db-0 -n cicd --kubeconfig /root/admin.conf -- bash -c "psql -U postgres -d db -f /tmp/queryInstanceId.sql -v v1=full_test | grep [0-9]"
Actual Output of above command:
Defaulting container name to abc-db.
Use 'kubectl describe pod/abc-db-0 -n cicd' to see all of the containers in this pod.
(0 rows)
Expected Output:
(0 rows)
Could anyone please let me know what I am doing wrong here?
Note:
The first 2 lines always comes when we login to the DB manually but in output I only want (0 rows)
The first two lines are output by kubectl exec because the Pod has multiple containers. It is sort of a warning that it picked the first one, which might not be the one you wanted use.
You can specify the target container in your command (-c containername):
kubectl exec abc-db-0 -n cicd --kubeconfig /root/admin.conf -c abc-db -- bash -c "psql -U postgres -d db -f /tmp/queryInstanceId.sql -v v1=full_test | grep [0-9]"
Or you can redirect the standard error with kubectl ... 2>/dev/null (os specific):
kubectl exec abc-db-0 -n cicd --kubeconfig /root/admin.conf -c -- bash -c "psql -U postgres -d db -f /tmp/queryInstanceId.sql -v v1=full_test | grep [0-9]" 2>/dev/null
This question already has answers here:
While loop stops reading after the first line in Bash
(5 answers)
Closed 1 year ago.
I thought that my problem is trivial, but I cannot figure out, why my scripts only performing once in array.
I have a jenkins job (bash script). This job gathering hostnames and sends ssh commands, through script, using gathered info:
rm /tmp/hosts
docker exec -t tmgnt_consul_1 consul members -status=alive | grep -v Node | awk '{print $1}' | cut -d : -f1 >> /tmp/hosts
sed -i '/someunnecessaryinfo/d' /tmp/hosts
echo >> /tmp/hosts
shopt -s lastpipe
while IFS= read -r line; do
echo "host is >>$line<<";
url="http://111.111.111.111:8500/v1/catalog/nodes"
term_IP=`curl -s $url | jq -r --arg Node "${line}" '.[] | select(.Node == "'${line}'" )|.Address' --raw-output`
echo $term_IP
sudo bash -x /home/rtm/t_mgnt/check_fw $term_IP
done < /tmp/hosts
Second script:
#!/bin/bash
term_IP=$1
sudo sshpass -p 'some.pass' ssh -o StrictHostKeyChecking=no user#$term_IP "sudo test -d /root/nv9"
if [ $? != 0 ]; then
sudo sshpass -p 'some.pass' \
scp -n -o StrictHostKeyChecking=no -r /home/rtm/t_mgnt/nv9 user#$term_IP:
sudo sshpass -p 'some.pass' \
ssh -n -o StrictHostKeyChecking=no user#$term_IP "sudo mv nv9 /root/"
sudo sshpass -p 'some.pass' \
ssh -n -o StrictHostKeyChecking=no user#$term_IP "sudo dpkg -i /root/nv9/libudev0_175-0ubuntu9_amd64.deb"
sudo sshpass -p 'some.pass' \
ssh -n -o StrictHostKeyChecking=no user#$term_IP "sudo /root/nv9/DetectValidator"
else
sudo sshpass -p 'some.pass' \
ssh -n -o StrictHostKeyChecking=no user#$term_IP "sudo /root/nv9/DetectValidator"
fi
The job is working fine, and returns correct values, but only for the first element of array.
PS - I already searched through this and other sites, and - following answer didn't help me - Shell script while read line loop stops after the first line (already "ssh -n -o").
Perhaps you can point me, what I missed.
Possibly this ssh call eats your input:
sudo sshpass -p 'some.pass' ssh -o StrictHostKeyChecking=no user#$term_IP "sudo test -d /root/nv9"
^^^
Try adding -n.
I am using execute_process() function in cmake.
message(" FLAGS = ${FLAGS}")
message(" SCATTERFILE = ${SCATTERFILE}")
set ( EXECUTE_COMMAND "arm-none-eabi-gcc ${FLAGS} -E -P -x c-header ${SCATTERFILE} -o ~/ttt.ld" )
message("EXECUTE_COMMAND = ${EXECUTE_COMMAND}")
execute_process(COMMAND ${EXECUTE_COMMAND} RESULT_VARIABLE rv )
Everything is displayed perfectly as a result of message() command, but it causes errors when it is run while parsing cmake. I think the FLAGS variable is not expanding as expected while parsing. When I run the same EXECUTE_COMMAND which is displayed as a result of message command in the terminal it runs perfectly. what could be the issue ?
Edit:
I have removed ${FLAGS} from
set ( EXECUTE_COMMAND "arm-none-eabi-gcc ${FLAGS} -E -P -x c-header ${SCATTERFILE} -o ~/ttt.ld" )`
now I am using
set ( EXECUTE_COMMAND "arm-none-eabi-gcc -E -P -x c-header ${SCATTERFILE} " )
The output is:
EXECUTE_COMMAND arm-none-eabi-gcc -E -P -x c-header ~/scatterFile.scatter rv: No such File or directory.
If I simply enter this command on the terminal,
arm-none-eabi-gcc -E -P -x c-header ~/scatterFile.scatter
it executes and gives the expected results.
The problem is you're trying to execute a program named "arm-none-eabi-gcc -E -P -x c-header ~/scatterFile.scatter rv". Notice the syntax of execute_process():
COMMAND <cmd1> [args1...]
To make it even clearer, the documentatin could actually write it as:
COMMAND cmd1 [arg1 [arg1 ...]]
CMake expects the command name as one CMake argument and each command-line argument as another separate CMake argument. You're enclosing everything in quotes, however, which turns it into one CMake argument (containing lots of spaces). Change your code as follows:
set (EXECUTE_COMMAND arm-none-eabi-gcc ${FLAGS} -E -P -x c-header ${SCATTERFILE} -o ~/ttt.ld)
execute_process(COMMAND ${EXECUTE_COMMAND} RESULT_VARIABLE rv)
I have seen something like in a lab for writing Unix shell in C.
make[1]: Entering directory `/a/cs.amu.edu/p/i/i/labs/lab/src'
./driver.pl -t t1.txt -s ./tsh -a "p"
1) what does it mean?
I have a homework that lists it.
I simply typed
make
and
./driver.pl -t t1.txt -s ./tsh -a "p"
and things worked for me, but when i typed
make[1]: Entering directory `/a/cs.amu.edu/p/i/i/labs/lab/src'
./driver.pl -t t1.txt -s ./tsh -a "p"
into linux terminal it gave something like this:
loop$
what is the difference between make and make[1]:....?
2) Also hw asks me to submit by this command:
cp tsh.c $(HANDINDIR)/$(TEAM)-$(VERSION)-tsh.c
where HANDINDIR gives a path, similar to /a/cs.amu.edu/p/i/i/labs/handin.
If i simply type this command will i be able to handin the work?
Any help is appreciated
make[1]: Entering directory `/a/cs.amu.edu/p/i/i/labs/lab/src'
./driver.pl -t t1.txt -s ./tsh -a "p"
The first line isn't a command. It's just an informational message that make is changing its current directory. Also, Makefiles can call other Makefiles, and the [1] indicates how many levels deep that is - again, that's just informational.
To get the same effect from a prompt, you would need to type:
cd /a/cs.amu.edu/p/i/i/labs/lab/src
./driver.pl -t t1.txt -s ./tsh -a "p"