The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
ls while read loop - internal read picking up wrong input dkieran Shell Programming and Scripting 2 05-14-2007 12:02 PM
How to read a line and put it into 3 variables Mandab Shell Programming and Scripting 8 04-20-2007 09:50 AM
is there any way to read a line twice in KSH Srini75 Shell Programming and Scripting 6 01-24-2006 09:14 PM
How to read from a file line by line and do stuff spaceship Shell Programming and Scripting 4 03-17-2005 05:47 PM
read vs line kishorebabu Shell Programming and Scripting 2 10-31-2003 06:53 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-20-2005
Registered User
 

Join Date: Sep 2005
Posts: 5
Wink read line and read next

hi,
there is a text like this in text.txt

DETAILS (
USERID,
USERNM
)

how to find the 'DETAILS (' and start to read next line until meet the char ')', when read next line i need to put the 'USERID' into a variable name to be use later

thanks a lot ...please help
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 09-20-2005
reborg's Avatar
Administrator
 
Join Date: Mar 2005
Location: Ireland
Posts: 3,503
Well what have you tried so far?
Reply With Quote
  #3 (permalink)  
Old 09-20-2005
Registered User
 

Join Date: Sep 2005
Posts: 5
below is what i tried so far, any good idea
hasDetail='FALSE'
exec 4<$1
while read eachline <&4
do

if [ "$eachline" == ")" ]
then
hasDetail='FALSE'
fi
if [ "$hasDetail" == "TRUE" ]
then
varnm=`echo $eachline | cut -f1 -d,`
repnm=P`echo $varnm | cut -c 2-`
sed 's/$varnm/$repnm/g' $1 > $1.$$
sed 's/$repnm/$repnm/g' $1.$$> $1
fi
if [ "$eachline" == "DETAIL (" ]
then
hasDetail='TRUE'
fi
done
Reply With Quote
  #4 (permalink)  
Old 09-20-2005
RishiPahuja's Avatar
Registered User
 

Join Date: Apr 2005
Location: Bangalore, India
Posts: 203
Thumbs up

Quote:
Originally Posted by ariuscy
below is what i tried so far, any good idea
hasDetail='FALSE'
exec 4<$1
while read eachline <&4
do

if [ "$eachline" == ")" ]
then
hasDetail='FALSE'
fi
if [ "$hasDetail" == "TRUE" ]
then
varnm=`echo $eachline | cut -f1 -d,`
repnm=P`echo $varnm | cut -c 2-`
sed 's/$varnm/$repnm/g' $1 > $1.$$
sed 's/$repnm/$repnm/g' $1.$$> $1
fi
if [ "$eachline" == "DETAIL (" ]
then
hasDetail='TRUE'
fi
done

Change the place of
Code:
if [ "$eachline" == "DETAIL (" ] 
	then 
		hasDetail='TRUE'
	fi
This should be the first check to be done.

I dont find the approach you are using reliable, it will fail in the scenario where the data file has unmatched ")"

rishi
Reply With Quote
  #5 (permalink)  
Old 09-20-2005
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,643
If you are looking to extract only USERID check this out.

Code:
sh-2.05b$ echo "DETAILS (
USERID,
USERNM
)" | sed  -n -e '/DETAILS.*/{
n
s#\(.*\)\,#\1#p
}'
It will extract and give out USERID

vino
Reply With Quote
  #6 (permalink)  
Old 09-21-2005
Registered User
 

Join Date: Aug 2005
Posts: 3
Smile

j=0;
foreach $curline (<FILE NAME>)
{

if j=0
{
next if($cur_line !~ /DETAILS\(/);
j=1;
next;
}

if($cur_line !~ /\)/);
{
.
. put your logic here...
.
}
else
{
exit 1
}

Last edited by rishchand; 09-21-2005 at 01:49 AM. Reason: here is the program in perl try this out...
Reply With Quote
  #7 (permalink)  
Old 09-21-2005
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,490
one more way ...

echo "DETAILS (
USERID,
USERNM
)" | sed -ne 's/,$//p'
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 05:16 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0