lines searching >>


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting lines searching >>
# 1  
Old 01-18-2009
lines searching >>

hi guys! I`ll really appreciate your help.
The situation is:
i have a log file, and i need to get the needed lines from it.
Code:
linecount=$(cat -n http.log | grep ALERT | awk '{print $1}' | wc -l)
lines=$(cat -n http.log | grep ALERT | awk '{print $1}')

1-string gets the number of found lines
2-string gets the string numbers

What I need is to make script to get next 5 lines after each found $lines. For example:
echo $lines
287 309 331
so the needed strings are 287-292, 309-314, 331-336.
my idea was to make something like:
Code:
cat -n http.log | grep ALERT | awk '{print $1}' > lines # to record needed string numbers in file
for i in $(seq 1 $linecount); do
let line$i=$(sed -n "$i" p lines) # to define each line number as variable :D i know that`s ugly
sed -n 'line$ip;line$i+1p;line$i+2p;...' http.log  # that`s the ugliest ever, and that doesn`t work. Any suggestions?
done

it would be great if someone could show a quick solution of how to do that or just some suggestions!
Thanks!

Last edited by neverhood; 01-18-2009 at 04:34 AM..
# 2  
Old 01-18-2009
so the question now is:
$i=3
$line3=331
how do i echo line$i as a variable (to get 331)?
is it possible?

Last edited by neverhood; 01-18-2009 at 05:38 AM..
# 3  
Old 01-18-2009
ok guys..i`ve made it. it`s probably the ugliest script ever, but it does what i want Smilie
Code:
#!/bin/bash
linecount=$(cat -n http.log | grep ALERT | awk '{print $1}' | wc -l)
lines=$(cat -n http.log | grep ALERT | awk '{print $1}')
cat -n http.log | grep ALERT | awk '{print $1}' > lines
#echo $lines > lines # V RYADOK, SYKA
for i in $(seq 1 $linecount); do
let line$i=$(sed -n "$i"p lines) #echo $[line$i]
sed -n "$[line$i]p;$(($[line$i]+1))p;$(($[line$i]+2))p;$(($[line$i]+3))p;$(($[line$i]+4))p" http.log | awk '{sub(/^[ \t]+/, ""); print}' >> alerts
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Searching for pattern and remove the lines

Hi , I want to remove the specific pattern and remove those lines from file using shell script. i want to remove these lines <?xml version='1.0' encoding='UTF-8'?> <row_set> </row_set> my input file has content like this. file name: sample.xml <?xml version='1.0'... (4 Replies)
Discussion started by: nukala_2
4 Replies

2. UNIX for Dummies Questions & Answers

Add lines after searching for a pattern

Hi, I have 2 files like below. File A: apple mango File B: start abc def apple ghi end start cba fed (4 Replies)
Discussion started by: jayadanabalan
4 Replies

3. Shell Programming and Scripting

Searching inverted lines

Hi fellas, I have a file like this: A_B B_D C_D D_B E_F G_H B_A F_E In other words, I have member1_member2 and member2_member1 in the same file. In the exemple aforementioned I have A_B and B_A, B_D and D_B, E_F and F_E. So, I would like to know a sript that print the lines B_A, D_B... (3 Replies)
Discussion started by: valente
3 Replies

4. Shell Programming and Scripting

searching multiple lines and replacing in shell scripting

Hi, I have a file with below contents, ssenthil = rw anilkg = rw I want to search for "ssenthil" and need to delete line 1 and 2 , if the third line starts with "" respectively and blank line immediately and third line starts with " anilkg = rw Please help me . Great day... (5 Replies)
Discussion started by: anil8103
5 Replies

5. Shell Programming and Scripting

Insert tags in lines after searching for a word

Hi, I am new to Unix and this is my first post on this forum. I am trying to convert a file into an xml. In my input I want to search for any line that starts with a 'F' and make it a tag in the xml. See below for the input and output. Input : <Payment> <REFERENCE>78</REFERENCE> F123 : ... (7 Replies)
Discussion started by: akashgov
7 Replies

6. Shell Programming and Scripting

Perl searching special words in lines

Hi , i am a new with perl, i want to made a script that find in file rows that start with specil words, as an example a line will start with" ............................................. specialword aaa=2 bbb=5 ............................................. and to put this in a new file... (3 Replies)
Discussion started by: alinalin
3 Replies

7. Shell Programming and Scripting

Searching for lines in textfiles

Hello all, I've a problem. I've two logfiles and i need to find lines in the second file by using information from the first file. First I need to extract a searchpattern from the first file. Its like abc=searchpattern&cde=. All between abc= and &cde= is the pattern I need to find in the second... (2 Replies)
Discussion started by: Avarion
2 Replies

8. UNIX for Dummies Questions & Answers

Searching for lines in a file?

What is the best way to display lines in a log file that begin with a certain string? Preferably I would like to 'print' them to a file. I guess I would use 'cat' for that? There are two types of line I would like to get at - each begins with a different two words. It would be something... (8 Replies)
Discussion started by: Sepia
8 Replies

9. Shell Programming and Scripting

Searching for lines ending with }

I'm trying to search for lines ending with "}" with the following command but am not getting any output. grep '\}$' myFile.txt I actually want to negate this (i.e. lines not ending with "}"), but I guess that should be easier once I find the command that finds it? (11 Replies)
Discussion started by: BootComp
11 Replies

10. Shell Programming and Scripting

sed searching across lines

hi, i'm making now a bash script, that runs some compiler... i want to take only errors form its output eg: output: bla bla bla ... erros is 1324546 the bla bla bla bla bla bla... ... and i want to get only erros is 1324546 the bla bla bla (11 Replies)
Discussion started by: miechu
11 Replies
Login or Register to Ask a Question