Loop

Hi
I wrote shell script to do the flowing count from 5 and down but doesn�t work please see my program

#Bash

x=0
y=10


while  (( x < y ))
do
(( x = x + 1 ))

grep -A $x 5 numbers >>file4.txt
done

these the output

5
6
5
6
7
5
6
7
8
5
6
7
8
9
5
6
7
8
9
10
5
6
7
8
9
10

5
6
7
8
9
10

Your requirement is not so clear. Can you provide desired output?

BTW If your requirement is to generate sequence of numbers ranging from 0 - 10 in BASH. Then you can simply use a for loop

#!/bin/bash

for x in {0..10}
do
  echo $x
done

I would like to have the output like this
5
6
7
8
9

#!/bin/bash

for x in {5..9}
do
  echo $x
done

Im using grep -A

---------- Post updated at 02:25 AM ---------- Previous update was at 02:20 AM ----------

in my program grep -A $x is lasting to down lines
grep -A $x 5 numbers >>file4.txt

so I have file called number, numbers saved inside it numbers from 1 to 10
grep -A $x going to start greping from 5 to 10
but what im getting output scramble instead of
5
6
7
8
9
10

#!/bin/bash

for x in {5..9}
do
   grep -A$x PATTERN FILE
done

Note: Replace PATTERN with the pattern you want to grep and FILE with the file name you want to grep.

---------- Post updated at 15:36 ---------- Previous update was at 15:29 ----------

Please note that -A option is used to print NUM lines of trailing context after matching lines.

Is print many lines instead of one round of numbers from 5 to 10

5
6
7
8
9
10
5
6
7
8
9
10

5
6
7
8
9
10

5
6
7
8
9
10

5
6
7
8
9
10

Yes. You are getting result as per the option -A . If you want to grep and print only those lines starting with number:

#!/bin/bash
for x in {5..9}
do
    grep "^$x" numbers
done

Also break statement doesn�t work

for x in {5..9}; do grep -A $x 5 numbers >>file4.txt ; if [[ `grep 8 file4.txt` ]]; then  break ; fi; done

does break 2 work?

Sounds random, I know.

How do you know the break statement does not work?

Reading through the comments that have been posted in this thread, it seems clear to me that hadinetcat doesn't understand what the command:

grep -A x 5 num

does. Putting it in a loop is adding to the confusion, but the output found in the file file4.txt looks perfectly reasonable except that there should not be any blank lines in the output and there should be three more occurrences of the sequence:

5
6
7
8
9
10

If we look at the commands that will be executed by this loop that produce output, they are:

grep -A 1 5 numbers
grep -A 2 5 numbers
grep -A 3 5 numbers
grep -A 4 5 numbers
grep -A 5 5 numbers
grep -A 6 5 numbers
grep -A 7 5 numbers
grep -A 8 5 numbers
grep -A 9 5 numbers
grep -A 10 5 numbers

The man page for grep says the command:

grep -A x 5 numbers

will print the each line in the file numbers that contains the character 5 and print the 1st x lines from numbers that follow the line that contained the 5 . So if the contents of the file numbers is:

1
2
3
4
5
6
7
8
9
10

the 1st command in the list above will print:

5
6

the 2nd command will add:

5
6
7

the 3rd command will add:

5
6
7
8

the 4th command will add:

5
6
7
8
9

and each of the remaining six commands will add:

5
6
7
8
9
10

I suspect that the output you're showing here is a subset of the contents of file4.txt and that the output is exactly what I described above as the expected output from your script.

I'm suspecting that English is not your first language, but if what you mean by:

is that you want a script to produce the output:

5
4
3
2
1

then the easy way to do this (either with bash or with ksh) is:

for i in {5..1}
do      echo $i
done

If this is the output you want, but you insist on using grep -A on the file numbers with the contents shown before, a way to do this roughly following the format of your script would be:

x=5
y=0
while (( x > y ))
do      grep -A 0 $x$ numbers
        (( x-- ))
done

If what you wanted is the output:

5
6
7
8
9
10

and you want to use grep -A to get it, as has been shown above, no loop is required. The single command:

grep -A 5 5 numbers

is sufficient.

1 Like

Here iw what I want simple and easy
Grep Colombia from file called usa.
Stop at break point .
But doesn�t work it keep repeating the same line

x=0
y=3000
while (( x < y ))
do
grep -A $x Colombia usa  >>file4.txt  # grep Colombia from file usa
(( x++ ))
 
if [[ `grep /div file4.txt` ]] #stop at break point
then
echo "break"
break
fi
done 

---------- Post updated at 01:38 AM ---------- Previous update was at 12:46 AM ----------

Usa file format

<h2>Colombia</h2>
<table class='alt_rows_stat_table dual_border_data_table full_width index_table market_sortable_table' id='world_index_CO_table'>
<thead>
<tr class='data_header'>
<th class='first name'>
<span onclick='sortTable(this, 0)'>Index Name</span>
</th>
<th class='value'>
<span onclick='sortTable(this, 1)'>Value</span>
</th>
<th class='value_change'>
<span onclick='sortTable(this, 1)'>Change</span>
</th>
<th class='percent_change'>
<span onclick='sortTable(this, 1)'>% Change</span>
</th>
<th class='time last'>Time</th>
</tr>
</thead>
<tbody>
<tr class='odd'>
<td class='name'><a href="/quote/COLCAP:IND" title="Colombia COLCAP Index (COLCAP:IND)">Colombia COLCAP Index</a></td>
<td class='value'>1,794.70</td>
<td class='value_change down'>-2.23</td>
 
<td class='percent_change down'>-0.12%</td>
 
<td class='time last'>15:15:19</td>
</tr>
<tr class='even'>
<td class='name'><a href="/quote/COL20:IND" title="Columbia COL20 Index (COL20:IND)">Columbia COL20 Index</a></td>
<td class='value'>1,324.20</td>
<td class='value_change down'>-2.55</td>
 
<td class='percent_change down'>-0.19%</td>
 
<td class='time last'>15:15:19</td>
</tr>
<tr class='odd'>
<td class='name'><a href="/quote/IGBC:IND" title="Indice General de la Bolsa de Valores de Colombia (IGBC:IND)">Indice General de la Bolsa de Valores de Colombia</a></td>
 
a</a></td>
<td class='value'>14,389.56</td>
<td class='value_change down'>-25.28</td>
 
<td class='percent_change down'>-0.18%</td>
 
<td class='time last'>15:18:29</td>
</tr>
</tbody>
</table>
</div>

But the problem here it keep repeating the lines

In message #12 in this thread, I explained (in detail) why grep -A is not an appropriate tool to do what you seem to be trying to do. (It provides multiple copies of some lines because you have a loop and each time through the loop provides a copy of some of the data you want, but apparently not all of what you want.)

If what you want in the example you gave in your message #13 in this thread is the output that you have shown with the heading "Usa file format", there are several easy ways to get it; none of them involve using grep -A (although I could use two invocations of grep -n, some arithmetic in ksh, and then an invocation of grep -A to print what you want). Two much more efficient ways to get what you want (assuming that the data you want to extract is in a file named usa ) are:

sed -n '/Columbia/,/\/div/p' usa

and

awk '/Columbia/,/\/div/' usa

Hi Don Cragun

Thank you so much, sed did the trick as you provide to me..

My best regards
Hadi