By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,483 Members | 963 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,483 IT Pros & Developers. It's quick & easy.

Problems while reading each lines and performing operations in each line

P: 14
Hello !!
i have a text file, from where i have to perform an operation such as awk for every lines.
i can count the number of lines with cat filename | wc -l , no problem with that.

my scenario:
i take a input from a file, i count the length ofcertian paramater with substring and do the if else condition.
but i have to perform this action in every line....

a code snippet...

echo $File
#count =$( cat /home/postgres/edt1/$File-spice-7676.txt.edt1 |wc -l)
#echo $count
#while [ "$count" != $check ]
#do
#count=$count-1

for LINE in $( cat /home/postgres/edt1/$File-spice-7676.txt.edt1 )
do
v=$(awk -F "|" '{ print length($2)}' /home/postgres/edt1/$File-spice-7676.txt.edt1)

echo $v

if [ $v -lt $X ]

then

awk -F"|" '{print $1"|"substr($2,length($2)-6,length($2))"|"$3"|"$4}' /home/postgres/edt1/$File-spice-7676.txt.edt1 >> /home/postgres/edt1/test.txt
echo " inside less"
else
echo " no"
awk -F"|" '{print $1"|"$2"|"$3"|1|"$4}' /home/postgres/$File-spice-7676.txt.edt > /home/postgres/edt1/test.txt

fi
done

..........
i am not able to proceed with LINE as it thinks as a new line when it finds spaces within a line,
i tried to count the number of lines in the file and think of decresing its value evey time, but also i didnt get the result

my file /home/postgres/edt1/$File-spice-7676.txt.edt1 )
is sum wot like that...

2008-04-18 11:11:52|+9779804808287|spice-6464|16|
2008-04-18 11:11:52|spice-7676|+9779804808287|16|
2008-04-18 11:12:43|+9779804808287|spice-6464|16|
2008-04-18 11:12:43|spice-7676|+9779804808287|16|
2008-04-18 11:14:04|+9779804808287|spice-6464|16|

so how can i perform this task

thanks for any suggestion
with regards,
Creeds
Apr 29 '08 #1
Share this Question
Share on Google+
4 Replies

ashitpro
Expert 100+
P: 542
change this line:
for LINE in $( cat /home/postgres/edt1/$File-spice-7676.txt.edt1 )

to:

cat /home/postgres/edt1/$File-spice-7676.txt.edt1 | while read LINE
Apr 29 '08 #2

P: 14
hello
yeah i already did that, but i again got strucked with

if [ "$V" -gt "$X" ]
the error shows unary operator expected,
i tried with all combination like "" or "$x" or "10" etc etc,
but shows error like interger expected or unary operator error...
so hw to solve this basic problem???
with regards,
suman
Apr 30 '08 #3

ashitpro
Expert 100+
P: 542
hello
yeah i already did that, but i again got strucked with

if [ "$V" -gt "$X" ]
the error shows unary operator expected,
i tried with all combination like "" or "$x" or "10" etc etc,
but shows error like interger expected or unary operator error...
so hw to solve this basic problem???
with regards,
suman

just print values of $V and $X
to use -gt/-lt/-eq in comparison input must be integers...
Apr 30 '08 #4

P: 14
Hello
Thanks for suggestion ,
now i cud solve my problem but with a lengthy process by sendign value to a file and getting its value from there..it was lengthy but i cud do my work now still it gave me sum headache
--------------------------------
do
echo $line
V= echo $line |awk 'BEGIN{FS="|"} {print length($3)}' > /home/postgres/v.txt
#echo "THE V IS:"$V
#echo "THE V IS:"$V
Y=$(awk '{print $0}' /home/postgres/v.txt)
echo "THE Y IS:"$Y
if [ "$Y" -gt "14" ]; then
echo "greater"
echo $line |awk 'BEGIN{FS="|"} {print $1"|"$2"|"substr($3,length($3)-13,length($3))"|1|"$4}' >> /home/postgres/edt1/final.txt
else
echo "lesser"

echo $line |awk 'BEGIN{FS="|"} {print $1"|"$2"|"$3"|1|"$4}' >> /home/postgres/edt1/final1.txt
fi
done

thanks
Suman
Apr 30 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.