Computing.Net > Forums > Unix > Problem with read line and trailing

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Problem with read line and trailing

Reply to Message Icon

Name: Pradeepa
Date: December 5, 2008 at 03:10:47 Pacific
OS: WINDOWS XP
CPU/Ram: 1GB
Product: Dell / DELL
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: December 6, 2008 at 12:49:20 Pacific
Reply:

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 $result

Second, in order to hold onto the trailing tabs, you might consider changing the field separator to a character that is not used:


#!/bin/ksh

while IFS="&" read line
do
echo "$line"
done < d4.txt



0

Response Number 2
Name: Pradeepa
Date: December 8, 2008 at 02:42:53 Pacific
Reply:

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 0

My 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,



0

Response Number 3
Name: Pradeepa
Date: December 9, 2008 at 01:31:00 Pacific
Reply:

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.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Problem with read line and trailing

strange problem with echo and read www.computing.net/answers/unix/strange-problem-with-echo-and-read/4618.html

Problem with TeraTerm Pro and VI www.computing.net/answers/unix/problem-with-teraterm-pro-and-vi/3661.html

reading 2 lines and comparing them www.computing.net/answers/unix/reading-2-lines-and-comparing-them/6861.html