Read from Text1 and Write to Text2

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.640000

I 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 value

This script should be executed every 5 minutes, so what do you think about the resource utilisation?

6

2 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 > text2

You can use specifier %g if you want the format to change between %f and %e depending on the size of the exponent.

1

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.

Alexander Ross

Alexander Ross

Gaming, Esports & Interactive Media Writer

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.