I'm new to shell scripting.
I would like to write a script to read from text1 file and get a value from this file.
This is the content of the text file:
L_T4000-Level3-Ouerdia 6679088203.600000 1523.208000
L_T4000-Level3-Ouerdiaa 8230141220.800000 1526.263200
L_T4000-nodeATI_Wardiaa 444039671.536000 4091589798.080000
L_T4000-node_TIS 6663189651.680000 2080241494.000000
L_T4000-node_TISa 6636796103.440000 2044580242.080000
L_International_node-globe 115911592756.879990 22433604426.723553
L_CRS_X-node06788 4801933455.200000 1329.232178
L_CRS_X-node06852 7100165206.800000 1630.996089
L_CRS_X-node06852a 7841650889.760000 1176446198.640000I want to get the first value from the line with L_International_node-globe and format this value to float, and write the new value to text file2
In this example the output should be the value '115911592756,879990' and this value should be stored in another text file.
What do you think about this code:
#!/bin/bash
text1="/path/to/filename1"
text2="/path/to/filename2"
var=0
grep 'L_International_node-globe' text1|tr -s ' ' '\t'|cut -f 2 | sed 's/\./,/' > $var
$var=$var/1000000000 #the result should be in Gigabits
echo $var > text2 #on every execution text2 should have a new valueThis script should be executed every 5 minutes, so what do you think about the resource utilisation?
62 Answers
Assuming by "floating point" you mean [-]d.ddde±dd "scientific notation" you could use awk
awk '/L_International_node-globe/ {printf("%e\n", $2)}' text1 > text2You can use specifier %g if you want the format to change between %f and %e depending on the size of the exponent.
Original answer
The following command line should do what you want,
grep 'L_International_node-globe' text1|tr -s ' ' '\t'|cut -f 2 > text2- print the line with the keyword (and value) you want from the file
text1 - convert space separation to TAB
- print the second field (first field after the keyword)
- redirect the output to the file
text2
I am not sure what you mean by float. The shell will print the character string, and various programs can interpret it as a number in float format.
Edit 1
OK, you want a comma as decimal separator. That is done with sed substitution.
grep 'L_International_node-globe' text1|tr -s ' ' '\t'|cut -f 2 | sed 's/\./,/' > text2Edit 2
In response to your suggested shellscript I suggest the following modified version,
#!/bin/bash
if [ $# -ne 2 ]
then echo " Usage: $0 <path/infile> <path/outfile> Example: $0 text1 text2" exit
fi
# extracting the value
var=$(grep 'L_International_node-globe' "$1"|tr -s ' ' '\t'|cut -f 2)
echo "debug1: $var"
# install and use 'bc'
# calculation; 'scale' sets the number of decimals in the output (truncated)
var=$(echo "scale=3
$var/1000000000" | bc) #the result should be in Gigabits
echo "debug2: $var"
# conversion to comma as decimal separator
var=${var/./,}
echo "debug3: $var"
echo "$var" > "$2" # after every execution the output file should have a new valueExamples with data from your original question,
$ ./scriptname Usage: ./scriptname <path/infile> <path/outfile> Example: ./scriptname text1 text2
$ ./script-name text1 text2 ; echo '-----------';cat text2
debug1: 115911592756.879990
debug2: 115.911
debug3: 115,911
-----------
115,911 1