Unable to get value from file - bash

I try to get the value of "length" of the first line of different text files but I am getting this error :
")syntax error: invalid arithmetic operator (error token is "
Here is the first line of one file :
><some info> <some info> | <other info> | <otherinfo> [Source:xxxx/xxxx;xxxx:xxxx] | <some info> | length=2812
So I want to get the value 2812. After many tests, the best I can get is this :
for file in ./*.txt
do
LLINE=$(head -n 1 "$file" | awk -F "length=" '{print $NF}')
echo "${LLINE}."
if [[ "${LLINE}" -le 1500 ]];
then
<some code>
else
<some code>
fi
done
My output is :
2812
")syntax error: invalid arithmetic operator (error token is "
376
")syntax error: invalid arithmetic operator (error token is "
...
What is the issue ?

maybe grep would be better for LLINE?
grep -Eo '[0-9]{1,4}'

Finally found with all your help !
This was DOS line endings error since those files were generated by someone using windows.
I just added a sed command to get rid of this :
LLINE=$(head -n 1 "$file" | awk -F "length=" '{print $NF}' | sed $'s/\r//')

Related

Excecuting shell script from chef recipe

I have to execute a script from recipe based on the result which I have to get from the ubuntu node. I have to get the version of agent running and based on that I have to execute script. Below is the condition in recipe which I am running.
notifies :run, 'bash[uninstall CloudPassage]', :immediate
only_if { Mixlib::ShellOut.new("dpkg -s cphalo | grep Version | awk '{print $2}'" -lt "3.9.5").run_command.success? }
but it is giving a syntax. can anyone help me with the syntax to get version number from ubuntu node.
Syntax error:
FATAL: Cookbook file recipes/default.rb has a ruby syntax error:
FATAL: /home/ubuntu/chef-repo/cookbooks/cloudPassage/recipes/default.rb:27:syntax error, unexpected tSTRING_BEG, expecting keyword_do or '{' or '('
FATAL: ...rsion | awk '{print $2}'" -lt "3.9.5").run_command.success? }
FATAL: ... ^
FATAL: /home/ubuntu/chef-repo/cookbooks/cloudPassage/recipes/default.rb:27: syntax error, unexpected ')', expecting '}'
FATAL: ... awk '{print $2}'" -lt "3.9.5").run_command.success? }
FATAL: ...
The correct-er way to write this:
only_if { Gem::Requirement.create('< 3.9.5').satisfied_by?(Gem::Version.create(shell_out!('dpkg -s cphalo').stdout[/^Version: (.*)$/, 1])) }
Or something like that.
This script checks if cphalo version is less than (-lt ) 3.9.5.
get the line with cphalo reading Version:
"dpkg -s cphalo | grep Version"
then pipe it to awk and print second field ($2)
| awk '{print $2}'"
the result is compared to "3.9.5" thanks to -lt
">result<" -lt "3.9.5"
which returns true or false.
But you must not break your string like you do with internal double quotes. try the below:
" $(dpkg -s cphalo | grep Version | awk '{print $2}') -lt 3.9.5 "
or if square braces are needed:
" [ $(dpkg -s cphalo | grep Version | awk '{print $2}') -lt 3.9.5 ] "

How to redirect grep to a while loop

Hi I have the following bash script code
group2=0
while read -r line
do
popAll=$line | cut -d "{" -f2 | cut -d "}" -f1 | tr -cd "," | wc -c
if [[ $popAll = 0 ]]; then
group2 = $((group2+2));
else
group2 = $((group2+popAll+1));
fi
done << (grep -w "token" "$file")
and I get the following error:
./parsingTrace: line 153: syntax error near unexpected token `('
./parsingTrace: line 153: `done << (grep -w "pop" "$file")'
I do not want to pipe grep to the while, because I want variable inside the loop to be visible outside
The problem is in this line:
done << (grep -w "token" "$file")
# ^^
You need to say < and then <(). The first one is to indicate the input for the while loop and the second one for the process substitution:
done < <(grep -w "token" "$file")
# ^ ^
Note however that there are many others things you want to check. See the comments for a discussion and paste the code in ShellCheck for more details. Also, by indicating some sample input and desired output I am sure we can find a better way to do this.

for-loop not working: trying to turn a ls into an array

I'm receiving a snytax error when I run the following code:
#!/bin/bash
for i in (`ls *.nexus`);
do
awk 'NR >5' /path/to/nexus_files/$i | tr -d "'" | tr " " "\n" | sed 's/uce/>uce/g' > /path/to/fasta_files/${i}.fasta
done
error:
-bash: syntax error near unexpected token `(
when I remove parentheses:
-bash: syntax error near unexpected token 'awk'
In your simple example, you can do w/o the ls command
for i in *.nexus ; do
awk ...
done

syntax error: operand expected (error token is " ")

syntax error: operand expected (error token is " ")
I'm getting this syntax error with my current code:
log= who | grep $1 | cut -c 30-31,33-34
echo $log
time= date | cut -c 12-13,15-16
echo $time
on=$(($time - $log))
echo $on
If I remember correctly, " " stands for null. Why am I getting this?
Remove the space which was just after to = symbol and put the command inside $(), so that it would parse.
log=$(who | grep $1 | cut -c 30-31,33-34)
And,
time=$(date | cut -c 12-13,15-16)

bash scripting, how to parse string separated with :

I have lines that look like these
value: "15"
value: "20"
value: "3"
I am getting this as input pipe after grepping
... | grep value:
What I need is a simple bash script that takes this pipe and produce me the sum
15 + 20 + 3
So my command will be:
... | grep value: | calculate_sum_value > /tmp/sum.txt
sum.txt should contain a single number which is the sum.
How can I do with bash? I have no experience with bash at all.
You could try awk. Something like this should work
... | grep value: | awk '{sum+=$2}END{print sum}'
And you could possibly avoid grep alltogether like this
.... | awk '/^value:/{sum+=$2}END{print sum}'
Update:
You can add the " character as a field seperator with the -F option.
... | awk -F\" '/^value:/{sum+=$2}END{print sum}'
My first try was to grab the stuff on the right of the colon and let bash sum it:
$ sum=0
$ cat sample.txt | while IFS=: read key value; do ((sum += value)); done
bash: ((: "15": syntax error: operand expected (error token is ""15"")
bash: ((: "20": syntax error: operand expected (error token is ""20"")
bash: ((: "3": syntax error: operand expected (error token is ""3"")
0
So, have to remove the quotes. Fine, use a fancy Perl regex to extract the first set of digits to the right of the colon:
$ cat sample.txt | grep -oP ':\D+\K\d+'
15
20
3
OK, onwards:
$ cat sample.txt | grep -oP ':\D+\K\d+' | while read n; do ((sum+=n)); done; echo $sum
0
Huh? Oh yeah, running while in a pipeline puts the modifications to sum in a subshell, not in the current shell. Well, do the echo in the subshell too:
$ cat sample.txt | grep -oP ':\D+\K\d+' | { while read n; do ((sum+=n)); done; echo $sum; }
38
That's better, but still the value is not in the current shell. Let's try something trickier
$ set -- $(cat sample.txt | grep -oP ':\D+\K\d+')
$ sum=$(IFS=+; bc <<< "$*")
$ echo $sum
38
And yes, UUOC, but it's a placeholder for whatever the OP's pipeline was.

Resources