Search code examples
linuxbashshbash4

How can i read specific line and specific field using Bash?


I have this file and i want to only get the value of testme= So that i can do another action. But this throws lots of lines and actually cant yet make it work.

1. test.sh

#!/bin/bash
for i in $(cat /var/tmp/test.ini); do
  # just one output i need: value1
  grep testme= $i 
done

2. /var/tmp/test.ini

; comments
testme=value1
; comments
testtwo=value2

Solution

  • How about

    #!/bin/bash
    
    grep 'testme=' /var/tmp/test.ini | awk -F= '{ print  $2 }'
    

    or alternatively just using bash

    #!/bin/bash
    
    regex='testme=(.*)'
    
    for i in $(cat /var/tmp/test.ini);
    do
        if [[ $i =~ $regex ]];
        then
            echo ${BASH_REMATCH[1]}
        fi
    done