Extract text in while loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract text in while loop
# 1  
Old 12-16-2009
Extract text in while loop

Hi everyone,
I have managed to extract the xml data i need from a text file by using the command below. The xml data extracted has the following form:
(el3 element is optional, and el1 and el2 may have white spaces)

Code:
<Title><Doc>MyTitle</Doc>
<El1>My el1 is that</El1>
<El2>My el2 is that</El2>
<El3>My el3 is that</El3>
<Title><Doc>MyTitle 2</Doc>
<El1>My el1 is 123</El1>
<El2>My el2 is kajaer</El2>
<Title><Doc>MyTitle 3</Doc><El1>My el1 is that</El1>
<El2>My el2 is that and that</El2>
<El3>My el3 is that and this</El3>
 
more textFile | grep pattern1 | ....... | '/<El1>|<El2>|<El3>|<Title>/' | while read output;
 
do 
echo $output | grep Title > /dev/null
if [ $? -eq 0 ]; then
echo "------------------"
echo "My Title = $output "
read output
fi 
el1=$(echo $output )
read output
el2=$(echo $output )
read output
el3=$(echo $output )
 
echo "El1 = $el1 and El2 is $el2 and El3 is $el3."
 
done

There are two problems with this code. The first one is that if the elements have white space the echo outputs (reads until white space) :
<El1>My
The second problem is how should i deal with the optionality of el3.
If the line is read every time, and the element el3 is not present , the following Title element is considered as el3.

Thanks in advance

---------- Post updated at 12:22 PM ---------- Previous update was at 11:08 AM ----------

I have managed to solve the first problem, instead of read output , i read line. Now i just want to do the following , when i read a line that does not match a specific pattern , i would like to return the cursor to it's initial position , one line up. For example:

Code:
read line
echo $line | grep el3 > /dev/null
if [ $? -eq 0 ]; then
# ----- go up one line, so as to read the line again in the next iteration 
fi

I also tried using continue in the if block but it does not work properly
Please advise

Last edited by Franklin52; 12-16-2009 at 06:29 AM.. Reason: Please use code tags
# 2  
Old 12-16-2009
Can you give us bit more information clearly
# 3  
Old 12-16-2009
Consider the following xml document:

Code:
<Title><Doc>MyTitle</Doc>
<El1>My el1 is that</El1>
<El2>My el2 is that</El2>
<El3>My el3 is that</El3>
<Title><Doc>MyTitle 2</Doc>
<El1>My el1 is 123</El1>
<El2>My el2 is kajaer</El2>
<Title><Doc>MyTitle 3</Doc><El1>My el1 is that</El1>
<El2>My el2 is that and that</El2>
<El3>My el3 is that and this</El3>

I manage to get this xml through the following command :

Code:
more textFile | grep pattern1 | ....... | '/<El1>|<El2>|<El3>|<Title>/' | while read line;

And inside the while read line :

Code:
do 
echo $line | grep Title > /dev/null
if [ $? -eq 0 ]; then
echo "------------------"
echo "My Title = $line "
read line
fi 
el1=$(echo $line )
read line
el2=$(echo $line )
read line
echo $line | grep el1  > /dev/null
 if [ $? -eq 0 ]; then
  continue
 
 fi
el3=$(echo $line )
 
echo "El1 = $el1 and El2 is $el2 and El3 is $el3."
 
done

If the el3 is missing in the xml document as it is optional , the third read line , will read the next <Title> element, and will move the cursor one line down , which is not desirable . What i want to achieve inside this block :

Code:
 
 if [ $? -eq 0 ]; then
  continue
 
 fi

is to move the read cursor one line up , so as to re-read the <Title> element in the next loop. Also i cannot use goto.
# 4  
Old 12-16-2009
Quote:
Originally Posted by nthed
Consider the following xml document:

Code:
<Title><Doc>MyTitle</Doc>
<El1>My el1 is that</El1>
<El2>My el2 is that</El2>
<El3>My el3 is that</El3>
<Title><Doc>MyTitle 2</Doc>
<El1>My el1 is 123</El1>
<El2>My el2 is kajaer</El2>
<Title><Doc>MyTitle 3</Doc><El1>My el1 is that</El1>
<El2>My el2 is that and that</El2>
<El3>My el3 is that and this</El3>

I manage to get this xml through the following command :

Code:
more textFile | grep pattern1 | ....... |


Why are you using more?

Code:
grep pattern1 textfile | ....

Quote:
Code:
'/<El1>|<El2>|<El3>|<Title>/' | while read line;

And inside the while read line :

Code:
do 
echo $line | grep Title > /dev/null


Don't use grep to check a string; use case:

Code:
case $line in
     *Title*) echo "My Title = $line " ;;
esac

Quote:
Code:
if [ $? -eq 0 ]; then
echo "------------------"
echo "My Title = $line "
read line
fi 
el1=$(echo $line )


Why are you using command substitution instead of a direct assignment?

Code:
el1=$line

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

For loop or while loop from a text file

Hi all, i developed a script to measure the uptime of a process in a Solaris 10/11 environments. All is well, but i came across a situation where there are multiple processes of the same name. Basically i have the following result file: beVWARS 13357 19592122 beVWARS 14329 19591910... (4 Replies)
Discussion started by: nms
4 Replies

2. Shell Programming and Scripting

Extract text between two strings

Hi, I have a text like these: ECHO "BEGGINING THE SHELL....." MV FILE1 > FILE2 UNIQ_ID=${1} PARTITION_1=`${PL}/Q${CON}.KSH "SELECT ....." PARTITION_2=`${PL}/Q${CON}.KSH "SELECT ........" ${PL}/Q${CON}.KSH "CREATE ...." IF .... ....... I would like to extract only text that only... (4 Replies)
Discussion started by: mierdatuti
4 Replies

3. Shell Programming and Scripting

How to extract an ipaddress and use it in for loop in expect script?

set fid set content close $fid ## Split into records on newlines set records send "records splited\n" send $records\n set a "test\n" send $a foreach rec $records { ## Split into fields on colons set fields #set fields puts $fields for {set x 1} {$x < 4} {incr x} {... (1 Reply)
Discussion started by: rachanaPatel
1 Replies

4. Shell Programming and Scripting

Extract text from string

Dear community, I know, this is very stupid question, but I'm scratching my head to find a solution. I have a variable like this: var=" INFO : ABCDEFG"Now I need to remove the leading spaces and output the result like: echo "FIELD1 ; FIELD2 ; $RESULT ; FIELD4" ... (17 Replies)
Discussion started by: Lord Spectre
17 Replies

5. Shell Programming and Scripting

Extract text between two strings

Hi I have something like this: EXAMPLE 1 CREATE UNIQUE INDEX "STRING_1"."STRING_2" ON "BOSNI_CAB_EVENTO" ("CD_EVENTO" , "CD_EJECUCION" ) PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 5242880 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT) TABLESPACE "DB1000_INDICES_512K"... (4 Replies)
Discussion started by: chrispaz81
4 Replies

6. Shell Programming and Scripting

loop and extract matching filename

i am unable to exact matching filename in the loop. Filename.in file contains Customer_Product_ Information_Customer_Product_ sale_Product_ /home_dir contains files CUST_INFO_990090_1111.csv "1","Customer Product Detail","1000","salary" "1","Information Customer Product... (2 Replies)
Discussion started by: onesuri
2 Replies

7. Shell Programming and Scripting

extract text from a file

I have been reading several posts regarding how to extract text from a file, but none of those have helped me for what I need. This is my problem: I need to extract the text after my pattern So my line is: 485.74 6589.5 Log likelihood: 1485.79 My pattern is 'Log likelihood:' and I need... (2 Replies)
Discussion started by: loperam
2 Replies

8. Shell Programming and Scripting

Extract value from a text

Hi all, my problem is extract a value from a text, i mean, I have this text: > ala Nr of active alarms are: 16 ================================================================================================ Sever Specific Problem Cause Mo-Reference... (15 Replies)
Discussion started by: marimovo
15 Replies

9. Shell Programming and Scripting

Extract particular text

I executed a following sed command => echo "a/b/c/d/e/f/g/h" | sed 's/\/*$//g' a/b/c/d/e/f/g Now what if I want to extract "g" from "a/b/c/d/e/f/g/h" . That is second last string using SED. (4 Replies)
Discussion started by: Shell_Learner
4 Replies

10. Programming

c program to extract text between two delimiters from some text file

needa c program to extract text between two delimiters from some text file. and then storing them in to diffrent variables ? text file like 0: abc.txt ========= aaaaaa|11111111|sssssssssss|333333|ddddddddd|34343454564|asass aaaaaa|11111111|sssssssssss|333333|ddddddddd|34343454564|asass... (7 Replies)
Discussion started by: kukretiabhi13
7 Replies
Login or Register to Ask a Question