Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
added 46 characters in body; edited tags
Source Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 266
  • I have a file "input.txt" has variables A1, A2, A3, θ, θ1 and θ2 - sample of input.txt is as follow:

#cat input1.txt

A1=5.2 A2=4.9 A3=6.1 θ=space θ1=2.5 θ2=1.2

A1=3.1 A2=5.1 A3=3.7 θ=triangle θ1=8I have a file "input.1 θ2=3txt" has variables A1, A2, A3, θ, θ1 and θ2 - sample of input.9

  • I want to create a script to run over the file input.txt - this file will be passed as a second argument, the first argument would be the value of θ

  • I created a script as follow:

{txt is as follows:

#! /bin/bash

$ cat input1.txt 

A1=5.2 A2=4.9  A3=6.1 θ=space    θ1=2.5 θ2=1.2 

A1=3.1 A2=5.1  A3=3.7 θ=triangle θ1=8.1 θ2=3.9

file=input1I want to create a script to run over the file input.txt

if grep -q $1 "$file"; then awk -F '[= ]+' '{ print $12 }' <$2 this file will be passed as a second argument, the first argument would be the value of θ

elseI created a script as follows:

echo "Not available" fi }

#! /bin/bash

file=input1.txt

if grep -q $1 "$file"; 
then
awk -F '[= ]+' '{ print $12 }' <$2

else

echo "Not available"
fi
}

But when I run this script as followfollows:

./script space input.txt   

./script space input.txt (first argument is the value of θ and second argument is the file name), the output is all the values in field 12:

#./script1 space input1.txt 1.2 3.9

$ ./script1 space input1.txt 
1.2
3.9

the output should be 1.2 only, I searched and found that I need to create a loop to read the file line by line but I can not get it to work

Any help would be appreciated, thanks.

  • I have a file "input.txt" has variables A1, A2, A3, θ, θ1 and θ2 - sample of input.txt is as follow:

#cat input1.txt

A1=5.2 A2=4.9 A3=6.1 θ=space θ1=2.5 θ2=1.2

A1=3.1 A2=5.1 A3=3.7 θ=triangle θ1=8.1 θ2=3.9

  • I want to create a script to run over the file input.txt - this file will be passed as a second argument, the first argument would be the value of θ

  • I created a script as follow:

{

#! /bin/bash

file=input1.txt

if grep -q $1 "$file"; then awk -F '[= ]+' '{ print $12 }' <$2

else

echo "Not available" fi }

But when I run this script as follow:

./script space input.txt (first argument is the value of θ and second argument is the file name), the output is all the values in field 12:

#./script1 space input1.txt 1.2 3.9

the output should be 1.2 only, I searched and found that I need to create a loop to read the file line by line but I can not get it to work

Any help would be appreciated, thanks.

I have a file "input.txt" has variables A1, A2, A3, θ, θ1 and θ2 - sample of input.txt is as follows:

$ cat input1.txt 

A1=5.2 A2=4.9  A3=6.1 θ=space    θ1=2.5 θ2=1.2 

A1=3.1 A2=5.1  A3=3.7 θ=triangle θ1=8.1 θ2=3.9

I want to create a script to run over the file input.txt - this file will be passed as a second argument, the first argument would be the value of θ

I created a script as follows:

#! /bin/bash

file=input1.txt

if grep -q $1 "$file"; 
then
awk -F '[= ]+' '{ print $12 }' <$2

else

echo "Not available"
fi
}

But when I run this script as follows:

./script space input.txt   

(first argument is the value of θ and second argument is the file name), the output is all the values in field 12:

$ ./script1 space input1.txt 
1.2
3.9

the output should be 1.2 only, I searched and found that I need to create a loop to read the file line by line but I can not get it to work.

Source Link
user156999
  • 11
  • 1
  • 1
  • 1

bash script to read first argument as input and look for variable in another file line by line

  • I have a file "input.txt" has variables A1, A2, A3, θ, θ1 and θ2 - sample of input.txt is as follow:

#cat input1.txt

A1=5.2 A2=4.9 A3=6.1 θ=space θ1=2.5 θ2=1.2

A1=3.1 A2=5.1 A3=3.7 θ=triangle θ1=8.1 θ2=3.9

  • I want to create a script to run over the file input.txt - this file will be passed as a second argument, the first argument would be the value of θ

  • I created a script as follow:

{

#! /bin/bash

file=input1.txt

if grep -q $1 "$file"; then awk -F '[= ]+' '{ print $12 }' <$2

else

echo "Not available" fi }

But when I run this script as follow:

./script space input.txt (first argument is the value of θ and second argument is the file name), the output is all the values in field 12:

#./script1 space input1.txt 1.2 3.9

the output should be 1.2 only, I searched and found that I need to create a loop to read the file line by line but I can not get it to work

Any help would be appreciated, thanks.