read line and read next


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers read line and read next
# 1  
Old 09-20-2005
Hammer & Screwdriver read line and read next

hi, Smilie
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 Smilie
# 2  
Old 09-20-2005
Well what have you tried so far?
# 3  
Old 09-21-2005
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
# 4  
Old 09-21-2005
MySQL

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 ")" Smilie

rishi
# 5  
Old 09-21-2005
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
# 6  
Old 09-21-2005
Bug

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 05:49 AM.. Reason: here is the program in perl try this out...
# 7  
Old 09-21-2005
one more way ...

echo "DETAILS (
USERID,
USERNM
)" | sed -ne 's/,$//p'
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Bash] Read History function & Read Arrowkeys

Hi. How can I create a history function? (By "read" command or so) & How can I configure a read command so that the arrow keys are not displayed so funny? (^[[A) Thanks in advance. (4 Replies)
Discussion started by: sinnlosername
4 Replies

2. Shell Programming and Scripting

How to read file line by line and compare subset of 1st line with 2nd?

Hi all, I have a log file say Test.log that gets updated continuously and it has data in pipe separated format. A sample log file would look like: <date1>|<data1>|<url1>|<result1> <date2>|<data2>|<url2>|<result2> <date3>|<data3>|<url3>|<result3> <date4>|<data4>|<url4>|<result4> What I... (3 Replies)
Discussion started by: pat_pramod
3 Replies

3. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

4. Shell Programming and Scripting

how to read the contents of two files line by line and compare the line by line?

Hi All, I'm trying to figure out which are the trusted-ips and which are not using a script file.. I have a file named 'ip-list.txt' which contains some ip addresses and another file named 'trusted-ip-list.txt' which also contains some ip addresses. I want to read a line from... (4 Replies)
Discussion started by: mjavalkar
4 Replies

5. Shell Programming and Scripting

Help with Bash piped while-read and a read user input at the same time

Hi I am new to writing script and want to use a Bash Piped while-read and read from user input. if something happens on server.log then do while loop or if something happend on user input then do while loop. Pseudocode something like: tail -n 3 -f server.log | while read serverline || read... (8 Replies)
Discussion started by: MyMorris
8 Replies

6. UNIX for Dummies Questions & Answers

When reading a csv file, counter to read 20 lines and wait for minute then read next 20 till end

Hello All, i am a newbie and need some help when reading a csv file in a bourne shell script. I want to read 10 lines, then wait for a minute and then do a reading of another 10 lines and so on in the same way. I want to do this till the end of file. Any inputs are appreciated ... (3 Replies)
Discussion started by: victor.s
3 Replies

7. Shell Programming and Scripting

BASH: Break line, read, break again, read again...

...when the lines use both a colon and commas to separate the parts you want read as information. The first version of this script used cut and other non-Bash-builtins, frequently, which made it nice and zippy with little more than average processor load in GNOME Terminal but, predictably, slow... (2 Replies)
Discussion started by: SilversleevesX
2 Replies

8. Shell Programming and Scripting

Read Embedded Newline characters with read (builtin) in KSH93

Hi Guys, Happy New Year to you all! I have a requirement to read an embedded new-line using KSH's read builtin. Here is what I am trying to do: run_sql "select guestid, address, email from guest" | while read id addr email do ## Biz logic goes here done I can take care of any... (6 Replies)
Discussion started by: a_programmer
6 Replies

9. Shell Programming and Scripting

bash: read file line by line (lines have '\0') - not full line has read???

I am using the while-loop to read a file. The file has lines with null-terminated strings (words, actually.) What I have by that reading - just a first word up to '\0'! I need to have whole string up to 'new line' - (LF, 10#10, 16#A) What I am doing wrong? #make file 'grb' with... (6 Replies)
Discussion started by: alex_5161
6 Replies

10. UNIX for Advanced & Expert Users

read() wont allow me to read files larger than 2 gig (on a 64bit)

Hi the following c-code utilizing the 'read()' man 2 read method cant read in files larger that 2gig. Hi I've found a strange problem on ubuntu64bit, that limits the data you are allowed to allocate on a 64bit platform using the c function 'read()' The following program wont allow to allocate... (14 Replies)
Discussion started by: monkeyking
14 Replies
Login or Register to Ask a Question