This question already has answers here:
How to use sed/grep to extract text between two words?
(14 answers)
Closed 2 years ago.
I have a string like this:
line="06:13:41.817 INFO ProgressMeter - Traversal complete. Processed 46942 total variants in 2.2 minutes."
I want to extract 46942 and put in a variable.
I tried this, but it only removes the Processed and total. How do I do it?
Var=$(echo $line | sed -e 's/Processed\(.*\)total/\1/')
Use parameter expansion:
line="06:13:41.817 INFO ProgressMeter - Traversal complete. Processed 46942 total variants in 2.2 minutes."
line=${line#*'Processed '} # Remove from left up to "Processed ".
line=${line%' total'*} # Remove from from " total" up to the end.
Related
This question already has answers here:
How to reverse a list of words in a shell string?
(18 answers)
Closed 3 years ago.
I'm trying to write a bash script to swap the words entered by the user.
For example: hello stack overflow
Output: overflow stack hello
User can enter any number of words.
try this:
read -ra line # read into array
i=${#line[#]} # determine length of array
for i in $(seq $((i-1)) -1 0); do # loop in reverse order
echo -n "${line[i]} " # echo entry at i-position without linefeed
done
echo # linefeed
input
this is a test
output
test a is this
Take a look at the following article explaining the use of "read" in bash:
http://landoflinux.com/linux_bash_scripting_read.html
If you know ahead of time the number of words you will be switching, a simple solution could be something like this. Each word is assigned to a variable specified in your read command:
#!/bin/bash
echo "Enter two words: "
read one two
echo "first word: $one"
echo "second word: $two"
If you need to reverse a list of words in one string, you can take a look at this answer:
How to reverse a list of words in a shell string?
This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Copying a Bash array fails
(2 answers)
Closed 3 years ago.
I'm new to shell script and am having an issue with the shuf function.
This is my code
declare -a myarray=( 'A' 'B' 'C' 'D' 'E' 'F' )
myarray = $(shuf -e "${myarray[#]}")
echo "$myarray"
I make an array containing the six characters. I then shuffle them randomly, and print them out. My issue is that if I were to add another line, for example
echo ${myarray[2]}
This doesn't actually print the randomly sorted character in the 3rd position. Instead, it will always print 'C'. How can I actually save the sorted array? Do I need to make another array?
Thank you very much
Arrays in bash are defined with (). Bash is not statically typed, so setting myarray equal to some output of characters will do just that, making it a string you can echo with echo $myarray to see the full output.
You need to wrap your output in parens to make it clear to bash that your new myarray should also be an array:
myarray=($(shuf -e "${myarray[#]}"))
This question already has answers here:
Insert line after match using sed
(8 answers)
Closed 4 years ago.
I want to add the words "Getting started" to two markdown files (so the extension is .md). They are named:
* installing-disqus.md
* installing-google-analytics.md
I would like to populate that word right after the line "Tags: " so the outcome would be "Tags: Getting started"
In Bash, what command would I write. I am thinking it would look something like this:
echo "Getting started" >> *installing* *Tags:*
You could use sed to do a find and replace. Since you want the words added after Tags: (assumming there is only one such line) you could run:
sed -i “s/Tags:/Tags:<your text here>/g” <filename>
-i means that it will do the changes in the file
s/ means it will do a substitution
/
/g do this substitution in the whole file
This question already has answers here:
How do I iterate over a range of numbers defined by variables in Bash?
(20 answers)
Closed 4 years ago.
I have a list of files to put through a for loop. They are named
FA2_00032.png, FA2_00033.png, etc
I have variables $imID which contains FA2 string, $startFrame which contains the start frame (e.g. 00034) and $endFrame which contains to end frame (e.g. 00894).
I have managed to get the list of relevant files using:
eval echo ${imageID}_{${startFrame}..${endFrame}}.png;
This outputs
FA2_00034 FA2_00035 FA2_00036 etc
But now I need to pass this to the for loop. That is,
for file in *the above list*
where *the above list* is the block quoted list above. $file should contain FA2_00034, FA2_00035, etc for use in the for loop.
Use a C-style for loop, not eval+{...}, for tasks like this. printf can be used to assemble the file name from the current integer. The syntax 10#$foo ensures that the value of $foo is treated as a decimal (not an octal) number if it includes leading 0s.
imageID=FA2
startFrame=00034
endFrame=00894
for ((i=10#startFrame; i<=10#$endFrame; i++)); do
printf -v file '%s_%05d.png' "$imageID" "$i"
echo "Operating on $file" >&2
done
This question already has answers here:
How do I get bc(1) to print the leading zero?
(13 answers)
Closed 8 years ago.
I am summing two small decimal numbers contained in two variables:
SEEupper=`expr $SEEmedian+$SEEthre | bc`
but since the result is a number smaller than 1, like 0.XXXX, the output is: '.XXXX'.
Is there any way to have an output with the '0' before the dot and the decimals?
workaround: ... | sed -e "s|^\.|0.|"
if you can use python:
SEEupper=`python -c "print $SEEmedian +$SEEthre"`
Yes, with the internal bash command printf:
printf "%g" $SEEupper