CONFIG_FILE="${HOME}/sample/seq.cfg"
    count=`awk 'END {print NR}' $CONFIG_FILE`
    echo $count
    t=1
    while [ "$t" -le "$count" ]
    do
    output_var=`awk -v v1=$t '$1 ~ /v1/ { print $1 }' $CONFIG_FILE`
    input_var=`awk -v v1=$t '$1 ~ /v1/ { print $3 }' $CONFIG_FILE`

    echo "O=" $output_var
    echo "I=" $input_var
    done

the above code is giving blank values for output_var and input_var ??
Can someone tell me why it is not assigning values to it.

the config file is as follows :

1   =   4
2   =   2
3   =   9
4   =   87
5   =   6

Recommended Answers

All 20 Replies

hi,

so much is wrong...

you should ask a web search engine 'how to read a file using the shell'

don't you have at least a pdf version of the ABS

I have used similar code.. and it was working.. the mistake is in awk command.. but m not able to find what... its just searching a pattern in file.. whats wrong with that.. ???

input_file=`awk '$1 ~ /mgr_input_file/ { print $3 }' $CONFIG_FILE`

this line is working fine.

you don't need all these awk lines.

#!/bin/bash

configFile="${HOME}/sample/seq.cfg"

while IFS=' =' read first second
do
   printf 'O = %d\nI = %d\n' $first $second
done <"$configFile"

no.. my code is to search in the file and print that word only.. c mgr_input_file is the pattern which i m searching in the file nd then want to print the corresponding column value.

ah.
you can test pattern against "$first" inside while loop.

can you provide a larger sample of the input file, and the desired output?

    seq.cfg 
    1       =       8
    2       =       2
    3       =       54
    4       =       3
    5       =       17

CONFIG_FILE="${HOME}/sample/seq.cfg"
input_file=`awk '$1 ~ /mgr_input_file/ { print $3 }' $CONFIG_FILE`
output_file=`awk '$1 ~ /mgr_output_file/ { print $3 }' $CONFIG_FILE`

ydate=`date --date=yesterday +%Y%m%d`
echo "Concatinating records of $ydate"
cat ${HOME}/InputDir/mgr`date --date=yesterday +%Y%m%d`_*.* > ${HOME}/OutputDir/mgr`date --date=yesterday +%Y%m%d`.unl 

temp=${HOME}/OutputDir/mgr`date --date=yesterday +%Y%m%d`.unl
awk -F'|' '$8 !~ /4052100/ {print}' $temp > new
mv new $temp 

awkvar=$1

count=`awk 'END {print NR}' $CONFIG_FILE`
echo $count
t=1
cat $CONFIG_FILE
while [ "$t" -le "$count" ]
do
output_var=`awk -v v1="$t" '$1 ~ /v1/ { print $1 }' $CONFIG_FILE`
input_var=`awk -v v1="$t" '$1 ~ /v1/ { print $3 }' $CONFIG_FILE`

echo v1=$v1
echo O=$output_var
echo I=$input_var
echo count=$count
echo t=$t

awk -F'|' -v in_var="$input_var" -v out_var="$output_var" -v awk_var="$awkvar" '{
   for(n=1; n<=NF; n++){
      if(n!=NF)
         fmt="%s|";
      else
        fmt="%s\n";
      if(n==out_var)printf(fmt,$in_var)

   }
}' $temp > new

cat new
mv new $temp

echo "$input_var Column printed"

t=`expr $t + 1`
((awkvar=$1-1))
done

cat $temp

here is the code i wrote.. basically i want to take some columns from a file and put it into another file with my desired sequence.. in seq.cfg file, the 1st column is the output file seq.. and coresponding column contains the column number of the input file(input file contains more columns than output file).

hi,

here are some comments:

        1 = 8 
        2 = 2 
        3 = 54
        4 = 3 
        5 = 17
    # I don't see any *mgr_(in/out)put_file* inside seq.cfg file.
        CONFIG_FILE="${HOME}/sample/seq.cfg"
        input_file=`awk '$1 ~ /mgr_input_file/ { print $3 }' $CONFIG_FILE`
        output_file=`awk '$1 ~ /mgr_output_file/ { print $3 }' $CONFIG_FILE`
        ydate=`date --date=yesterday +%Y%m%d`

    #    echo "Concatinating records of $ydate"
    #    cat ${HOME}/InputDir/mgr`date --date=yesterday +%Y%m%d`_*.* > ${HOME}/OutputDir/mgr`date --date=yesterday +%Y%m%d`.unl
    #    temp=${HOME}/OutputDir/mgr`date --date=yesterday +%Y%m%d`.unl
    #    awk -F'|' '$8 !~ /4052100/ {print}' $temp > new
    #    mv new $temp
    #<
        echo "Concatinating records of $ydate"
        temp=${HOME}/OutputDir/mgr`date --date=yesterday +%Y%m%d`.unl
        awk -F'|' '$8 !~ /4052100/ {print}' ${HOME}/InputDir/mgr`date --date=yesterday +%Y%m%d`_*.* >"$temp"
    #>

        awkvar=$1
    #    count=`awk 'END {print NR}' $CONFIG_FILE`
        echo $count
        t=1 
        cat $CONFIG_FILE
    # Do as I showed you above
    #    while [ "$t" -le "$count" ]
        do  
    #       output_var=`awk -v v1="$t" '$1 ~ /v1/ { print $1 }' $CONFIG_FILE`
    #       input_var=`awk -v v1="$t" '$1 ~ /v1/ { print $3 }' $CONFIG_FILE`
           echo v1=$v1 #Does not exist inside the shell, only inside awk
           echo O=$output_var
           echo I=$input_var
    #       echo count=$count
           echo t=$t
    # I don't see awk_var used
           awk -F'|' -v in_var="$input_var" -v out_var="$output_var" -v awk_var="$awkvar" '{
              for(n=1; n<=NF; n++){
                 if(n!=NF) fmt="%s|"; else fmt="%s\n";
                 if(n==out_var)printf(fmt,in_var) #no dollar sign to awk's variables
              }
           }' $temp > new 
           cat new 
           mv new $temp
           echo "$input_var Column printed"
    #       t=`expr $t + 1`
           ((awkvar=$1-1)) #$1 never changes, so awkvar will keep the same value.
        done
        cat $temp
1 = 8
2 = 2
3 = 54
4 = 3
5 = 17
# I don't see any *mgr_(in/out)put_file* inside seq.cfg file.
CONFIG_FILE="${HOME}/sample/seq.cfg"
input_file=`awk '$1 ~ /mgr_input_file/ { print $3 }' $CONFIG_FILE`
output_file=`awk '$1 ~ /mgr_output_file/ { print $3 }' $CONFIG_FILE`  ###I was trying to show the pattern matching thing. this line is working.

# count=`awk 'END {print NR}' $CONFIG_FILE`
echo $count
t=1
cat $CONFIG_FILE
# Do as I showed you above
# while [ "$t" -le "$count" ]
do
# output_var=`awk -v v1="$t" '$1 ~ /v1/ { print $1 }' $CONFIG_FILE`  ## the output is not coming in these lines
# input_var=`awk -v v1="$t" '$1 ~ /v1/ { print $3 }' $CONFIG_FILE`   ##

echo v1=$v1 #Does not exist inside the shell, only inside awk         ##got it.. 
echo O=$output_var
echo I=$input_var
# echo count=$count
echo t=$t

awk -F'|' -v in_var="$input_var" -v out_var="$output_var" -v awk_var="$awkvar" '{
for(n=1; n<=NF; n++){
if(n!=NF) fmt="%s|"; else fmt="%s\n";
if(n==out_var)printf(fmt,in_var) #no dollar sign to awk's variables     ##I want to use the value present in $in_var.. like awk ahould treat it as a c
                                                                        ##column..i dunt know how to do that.. i just tried. 
}
}' $temp > new
cat new
mv new $temp
echo "$input_var Column printed"
# t=`expr $t + 1`

done
cat $temp
the temp file will be like 
1|2|3|4|5|6|7|8|9|0
1|2|3|4|5|6|7|8|9|0
1|2|3|4|5|6|7|8|9|0
1|2|3|4|5|6|7|8|9|0
1|2|3|4|5|6|7|8|9|0
1|2|3|4|5|6|7|8|9|0

and if i configure seqfile like 

1=5
2=8
3=1
4=3

the out put sholud be like.: 

5|8|1|3
5|8|1|3
5|8|1|3
5|8|1|3
5|8|1|3
5|8|1|3

about line 30. I should have check your previous thread, my bad.

about your last message:
can you explain how temp file (which is the input file, right?) should produce output file, related to sed.cfg?

the 1st column in seq.cfg represents the column index in the output file. and the 3rd column represents the column index in the input file.
so the user will configure how he wants his input file.. i.e what all columns he needs and in which order. and the program will read the index number of the input file to be printed in the output file.. the output file in our case is new and input is temp.. the sample content of each file is given in previous post.

when things are clearly exposed, everything comes clear:

gawk -F'|' 'NR == FNR{
      split($0,a,"=")
      cfg[a[1]] = a[2]
      pNR=NR
   }
   NR-FNR == pNR{o = length(cfg)}
   NR > FNR{for(i=1; i<=o; i++)printf( (i == o ? "%s\n" : "%s|"),$cfg[i] )}' seq.cfg temp.input > new.output

its not giving the proper output..

... I've got temp.input

1|2|3|4|5|6|7|8|9|10
1|2|3|4|5|6|7|8|9|10
1|2|3|4|5|6|7|8|9|10
1|2|3|4|5|6|7|8|9|10
1|2|3|4|5|6|7|8|9|10
1|2|3|4|5|6|7|8|9|10
1|2|3|4|5|6|7|8|9|10
1|2|3|4|5|6|7|8|9|10
1|2|3|4|5|6|7|8|9|10
1|2|3|4|5|6|7|8|9|10

and seq.cfg

1=5
2=8
3=1
4=3

that give, using this awk command line

gawk -F'|' 'NR == FNR{ split($0,a,"="); cfg[a[1]] = a[2]; pNR=NR} NR-FNR == pNR{o = length(cfg)} NR > FNR{ for(i=1; i<=o; i++){ printf(( i == o ? "%s\n" : "%s|" ),$cfg[i])}}' seq.cfg temp.input
5|8|1|3
5|8|1|3
5|8|1|3
5|8|1|3
5|8|1|3
5|8|1|3
5|8|1|3
5|8|1|3
5|8|1|3
5|8|1|3

explain.

can u please explain the code line. what split , cfg and pNR is doing.

split is an awk function (rtfm)
cfg is an array which have first column as index, and third as value
pNR is just a variable to trace the change of file that's being read.

how are we giving input through 2 files

that's what NR and FNR are for.

ohk.. thanks a lot :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.