Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi,
I have a problem with read line.
My code is something like
while read line
do
((ncnt+=1))
result=`expr $cnt - $ncnt`echo "$line"
case "$line" in
PYFT*)
if [[ $result -ne 1 ]]
then
echo "$line2"
echo "$line1"
fi
;;
esac
done <$inputfilename >> ${BaseDir}/Temp
in the $line after while read line, some of the lines have trailing tab spaces and some does not. so when i am reading the lines and writing it to another file, I am losing them. I am not really sure which of the lines would have tab spaces and which of them does not have. So Please help.It is kind of very urgent, as we have to implement this in our business.
Thanks in advance.

First, this statement, tells me you are using a ksh/bash type shell:
((ncnt+=1))
So, why are you performing arithmetic like this:
result=`expr $cnt - $ncnt`
Why don't you use parenthesis and save the call to expr:
cnt=8
ncnt=9
result=$(($cnt+$ncnt))
echo $resultSecond, in order to hold onto the trailing tabs, you might consider changing the field separator to a character that is not used:
#!/bin/kshwhile IFS="&" read line
do
echo "$line"
done < d4.txt

Hi nails,
I am not an UNIX expert. I am actually fron TIBCO background. We have to insert header and trailer after every second trailer. You have given me the solution on this :) . My code goes,
#!/usr/bin/sh
inputfilename=$1
cnt=$(wc -l <$inputfilename)
echo $cnt
line1=`grep ^HDR<$inputfilename`
line2=`grep ^TRL<$inputfilename`
while read line
do
((ncnt+=1))
result=`expr $cnt - $ncnt`
echo "$line"
case "$line" in
PYFT*)
if [[ $result -ne 1 ]]
then
echo "$line2"
echo "$line1"
fi
;;
esac
done <$inputfilename >> outputfile.txt
exit 0My file is something like,
HDR --- ---
2HDR ---- ----
----
-----
----
PYFT --- ----
2HDR --- ---
---
---
---
PYFT -- ---
2HDR -- ---
--
---
PYFT ---- ---
TRL ----- ----
some of these lines having trailing tabs and spaces.And expected output is
HDR --- ---
2HDR ---- ----
----
-----
----
PYFT --- ----
TRL ----- ----
HDR --- ---
2HDR --- ---
---
---
---
PYFT -- ---
TRL ----- ----
HDR --- ---
2HDR -- ---
--
---
PYFT ---- ---
TRL ----- ----some how my business req does not allow me to use awk or sed. Please help.
Thanks in advance,

I have one more problem also in the above code. after the last PYFT, the last line TRL is not written from the inputfilename but the TRL and HDR are inserted even after the last PYFT. So can you please let me know where i am going wrong.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |