Im trying to get the frist two line of a file as a variables.
input file
/tmp/tmp.txt
1226037463l
1226037422ll
while read -r LINE
do
if [ $start=0 ];
then
FRIST=`echo $LINE | awk -F"|" 'NR==1''{print $1}'`
echo $FRIST
SECOND=`echo $LINE | awk -F"|" 'NR==2''{print $1}'`
echo $SECOND
done < /tmp/temp.txt
But the output is only
1226037463l
Only the frist line is stored in FRIST .. the SECOND is empty.
Where I m I going wrong ?
while read -r LINE
do
if [ $start=0 ];
then
FRIST=`echo $LINE | awk -F"|" 'NR==1''{print $1}'`
echo $FRIST
SECOND=`echo $LINE | awk -F"|" 'NR==2''{print $1}'`
echo $SECOND
fi
done < /tmp/temp.txt
I'm pretty new to shell scripting.. can any one help
OK I got it
FRIST=`echo | awk 'NR==1 {print;exit}' /tmp/temp.txt`
echo $FRIST
SECOND=`echo | awk 'NR==2 {print;exit}' /tmp/temp.txt`
echo $SECOND
But Can I convert these variables to integer and do arthematic operations ??
FIRST=`awk 'BEGIN{FS="|";OFS=" "}NR==1{print $1}' /temp/temp.txt`
SECOND=`awk 'BEGIN{FS="|";OFS=" "}NR==2{print $1}' /temp/temp.txt`
Im not able to perform arthematic operation on these ..
FRIST VALUE IS 1226045443l
SECOND VALUE IS 1226045402l
Error Im getting
expr: non-numeric argument
my code
str= `expr $FRIST + $SECOND`
echo $str
I tried below also
str= `expr $FRIST + $SECOND | bc -l`
echo $str
#! /bin/bash
FIRST=`awk 'BEGIN{FS="|";OFS=" "}NR==1{print $1}' /temp/temp.txt`
# NOT FIRST=1226045443l BUT FIRST=1226045443
SECOND=`awk 'BEGIN{FS="|";OFS=" "}NR==2{print $1}' /temp/temp.txt`
# NOT SECOND=1226045402l BUT SECOND=1226045402
((str=$FIRST+$SECOND)) #or str=$((FIRST+SECOND))
echo $str
or
#! /bin/bash
FIRST=`awk 'BEGIN{FS="|";OFS=" "}NR==1{print $1}' /temp/temp.txt`
SECOND=`awk 'BEGIN{FS="|";OFS=" "}NR==2{print $1}' /temp/temp.txt`
let str=$FIRST+$SECOND
echo $str
Try
#!bin/ksh
> awk 'NR<=2{print $1}' FS='|' ORS=' ' kk |read FIRST SECOND
> ress=$(( FIRST + SECOND ))
> echo $res
Thank you very much for you time..
First script executes with out any error, but in the output just blank line.
Second script throws the following error
"rpomon.sh: 14: let: not found"
Im pasting my code below ... Can you tell me where Im going wrong.
My script :
#!/bin/sh
FRIST=`echo | awk 'NR==1 {print;exit}' /tmp/temp.txt`
echo $FRIST
SECOND=`echo | awk 'NR==2 {print;exit}' /tmp/temp.txt`
echo $SECOND
((str=$FIRST-$SECOND))
echo $str
Output:
1226048263l
1226048222l
#!bin/ksh
> awk 'NR<=2{print $1}' FS='|' ORS=' ' kk |read FIRST SECOND
> ress=$(( FIRST + SECOND ))
> echo $res
I tried this .. the output is as follows
1226048503l
1226048462l
rpomon.sh: 19: Illegal number: 1226048462l
#! /bin/bash
FIRST=`awk 'BEGIN{FS="|";OFS=" "}NR==1{print $1}' /temp/temp.txt`
SECOND=`awk 'BEGIN{FS="|";OFS=" "}NR==2{print $1}' /temp/temp.txt`
let str=$FIRST+$SECOND
echo $str
I presume.. This following is the script you meant by ??
#!/bin/sh
awk 'NR<=2{print $1}' FS='|' OFS=' ' /tmp/temp.txt |read FIRST SECOND
echo $FRIST
echo $SECOND
ress=$(( FIRST + SECOND ))
echo $res
The above gives the following output
rpomon.sh: 21: =0: not found
Sorry .. That was typo... The output of the script is blank... This any thing on to the screen
not :
#!/bin/sh
FRIST=`echo | awk 'NR==1 {print;exit}' /tmp/temp.txt`
echo $FRIST
SECOND=`echo | awk 'NR==2 {print;exit}' /tmp/temp.txt`
echo $SECOND
((str=$FIRST-$SECOND))
echo $str
but :
#! /bin/bash
FIRST=`awk 'BEGIN{FS="|";OFS=" "}NR==1{print $1}' /temp/temp.txt`
# NOT FIRST=1226045443l BUT FIRST=1226045443
SECOND=`awk 'BEGIN{FS="|";OFS=" "}NR==2{print $1}' /temp/temp.txt`
# NOT SECOND=1226045402l BUT SECOND=1226045402
((str=$FIRST+$SECOND)) #or str=$((FIRST+SECOND))
echo $str
i am using bash
also works in sh
Ok.. I understand I messed up ...
I started clean again .. Below is the script
/usr/local/mano$ cat rpomon.sh
FIRST=`awk 'BEGIN{FS="|";OFS=" "}NR==1{print $1}' /tmp/temp.txt`
SECOND=`awk 'BEGIN{FS="|";OFS=" "}NR==2{print $1}' /tmp/temp.txt`
echo $FIRST
echo $SECOND
((str=$FIRST-$SECOND))
echo $str
Ouput :
1226050123l
1226050082l
Only FIRST and SECOND is getting printed .. Im not seeing str$ getting printed.
Klashxx
November 6, 2008, 4:31am
17
Umm.. typo,use:
#!/bin/ksh
awk 'NR<=2{print $1}' FS='|' ORS=' ' /tmp/temp.txt |read FIRST SECOND
echo $FIRST
echo $SECOND
res=$(( FIRST + SECOND ))
echo $res
oh i'm sorry
i mix up | and l
FIRST=`awk 'BEGIN{FS="l";OFS=" "}NR==1{print $1}' /tmp/temp.txt`
SECOND=`awk 'BEGIN{FS="l";OFS=" "}NR==2{print $1}' /tmp/temp.txt`
Klashxx
November 6, 2008, 4:35am
19
This a useless use of awk.
Im very sorry.. for that typo
I copy pasted the above program..
The following is output :
/usr/local/mano$ sh rpomon.sh
0
FIRST and SECOND ... were not printed.. the result was printed.