beginer
beginer

Reputation: 1

bash - parsing file and getting variables

I've searched and searched on here for an answer, and I think the combination of being a noob at bash, not knowing the right way to do this, and not searching for the right keywords has meant I can't quite get over the last hurdle.

I am looking to write a script doing some checks on our Netapp System. And I have some difficulties to fulfill the 2 first variables (VAR1 & VAR2)... I tried with cut and awk command but all my tries were unsuccessfully.

I have a text file (LUNS) and it looks like :

     #SAN1; SAN2
    abc1:abc1N
    abc2:abc2N 
    a3:a3N 
    abcde4:abcde4N

all the lines are text and numeric stuff.

for each line of the tom file, I would like to do:

What I did :

    cat tom |grep -v "#" |while read ligne ; do 
    VAR1=awk -F ";" '{print $1}'
    VAR2=awk -F ";" '{print $1}'
    specific command to get the numeric VAR3 & VAR4 values.
    #and run the comparaison  like : 
    if [ "$VAR3" -ne "$VAR4" ] 
    then
    echo "PROBLEM"
            if [ "$VAR4" -lt "$VAR3" ] ; then echo $VAR4; rm $VAR4; else echo "call support"; fi
    else 
    echo "Everything is OK"
    fi  

my awk is not working. i am open to any modification in the config file, or in my way to get the VAR1 & VAR2 variables

Can I have your input on, please?

Upvotes: 0

Views: 1295

Answers (3)

user3442743
user3442743

Reputation:

No need to use awk

Could rewrite the loop like

#!/bin/bash

while read line;do
    [[ "$line" =~ ^#.*$ ]] && continue
    VAR1=${line%:*}
    VAR2=${line#*:}
    echo $VAR1
    echo $VAR2
done < test

Upvotes: 0

byrnedo
byrnedo

Reputation: 1465

For one thing, you need to do

VAR1=$(echo $ligne|awk -F ";" '{print $1}')
VAR2=$(echo $ligne|awk -F ";" '{print $2}')

Upvotes: 0

nu11p01n73R
nu11p01n73R

Reputation: 26667

You should be using

command subtitution $(some command) eg a=$(ls)

OR

quoting mechanism

so for awk to work use either

VAR1=$(awk -F ";" '{print $1}')
VAR2=$(awk -F ";" '{print $1}')

or

VAR1=`awk -F ";" '{print $1}'`
VAR2=`awk -F ";" '{print $1}'`

Upvotes: 0

Related Questions