user3407570
user3407570

Reputation: 125

Cut is working column wise in bash

I am trying to extract information and send it to a DB from a text file which has these lines

EndDate=2014-04-08;Time=00:00:49;ID=1144877;AppName=Ad (ba);MetricName=EndUser1;average_network_time=0
EndDate=2014-05-08;Time=00:00:50;ID=1144878;AppName=Ad (qa);MetricName=EndUser/Apdex;score=1.0;s=0;t=0;f=0

The script I have written is:

var1="EndUser"
var2="EndUser/Apdex"
var6="path to file"

for line in $var6
do

EndDate=`cut -d';' -f1 $line | cut -d'=' -f2`
echo $EndDate
Time=`cut -d';' -f2 $line | cut -d'=' -f2`
echo $Time
ID=`cut -d';' -f3 $line | cut -d'=' -f2`
echo $ID
AppName=`cut -d';' -f4 $line | cut -d'=' -f2`
echo $AppName
MetricName=`cut -d';' -f5 $line | cut -d'=' -f2`
echo $MetricName

if [ "$MetricName" == "$var1" ];then
echo "in If" 
average_network_time=`cut -d';' -f6 $line | cut -d'=' -f2`
echo "avg=$average_network_time"
echo "('$EndDate','$Time','$ID','$AppName','$MetricName','$average_network_time');"
mysql -h servername -u ID -p'pass' name -e "insert into check_copy(EndDate,Time,ID,AppName,MetricName,average_network_time) values ('$EndDate','$Time','$ID','$AppName','$MetricName','$average_network_time');"
fi

if [ "$MetricName" == "$var2" ];then
echo "in If2" 
score=`cut -d';' -f6 $line | cut -d'=' -f2`
echo $score
s=`cut -d';' -f7 $line | cut -d'=' -f2`
t=`cut -d';' -f8 $line | cut -d'=' -f2`
f=`cut -d';' -f9 $line | cut -d'=' -f2`
mysql -h servername -u ID -p'pass' name -e "insert into check_copy(EndDate,Time,ID,AppName,MetricName,score,s,t,f) values ('$EndDate','$Time','$ID','$AppName','$MetricName','$score','$s','$t','$f');"
fi

The output I get is:

2014-04-08 2014-05-08
00:00:49 00:00:50
1144877 1144878
Ad (ba) Ad (qa)
EndUser1 EndUser/Apdex

Expected ooutput:

2014-04-08 00:00:49 1144877 Ad (ba) EndUser1 0
2014-05-08 00:00:50 1144878 Ad (qa) EndUser/Apdex 1.0 0 0 0

My problem: It is not getting into the loop itself. May be because it is doing it column wise I want it to print avg netwrok time value,score,s,t,f for its respective metric names.

Upvotes: 0

Views: 123

Answers (1)

clement
clement

Reputation: 3375

If file has

EndDate=2014-04-08;Time=00:00:49;ID=1144877;AppName=Ad (ba);MetricName=EndUser;average_network_time=0
EndDate=2014-05-08;Time=00:00:50;ID=1144878;AppName=Ad (qa);MetricName=EndUser/Apdex;score=1.0;s=0;t=0;f=0

The following sed command

sed -n -r -e '/MetricName=EndUser;/{s/^EndDate=(.*);Time=(.*);ID=(.*);AppName=(.*);MetricName=(EndUser);average_network_time=(.*)$/\1 \2 \3 \4 \5 \6/gp}'  -e '/MetricName=EndUser\/Apdex/{s/^EndDate=(.*);Time=(.*);ID=(.*);AppName=(.*);MetricName=(EndUser\/Apdex);score=(.*);s=(.*);t=(.*);f=(.*)$/\1 \2 \3 \4 \5 \6 \7 \8 \9/gp}' file

will give

2014-04-08 00:00:49 1144877 Ad (ba) EndUser 0
2014-05-08 00:00:50 1144878 Ad (qa) EndUser/Apdex 1.0 0 0 0

Works with GNU sed version 4.2.1. You can generate the mysql query directly by modifying the replacement part in s/pattern/replacement/pg

Upvotes: 1

Related Questions