finding lines only between a certain string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting finding lines only between a certain string
# 8  
Old 10-20-2008
What's the output of head -50 /tmp/AIALARM_MSNLA_20080626.txt | cat -vet? Can you post it between [ code ] tags instead of [ quote ] tags please?
# 9  
Old 10-20-2008
oh dear im getting a wierd result. the file is filled with ^M. Im sorry ill ftp the file out again. Thanks
Code:
root@ckpgpay11core> head -50 AIALARM_MSNLA_20080626.txt | cat -vet
Mroot@ckpgpay11core>

# 10  
Old 10-20-2008
No big deal, it just means the file is in DOS/Windows format. You can use dos2unix to convert it (if available on your system), or tr -d '\r' < dosfile > unixfile.
# 11  
Old 10-29-2008
Thanks guys
# 12  
Old 10-29-2008
Dear Annihillanic,

I got the script working. Sorry but i have another question. I am making the script s little interactive, and have modified it to search for different strings everytime i run it. I cant seem to replace the variable "GAA" in the script. Any idea why? below if the output of the script when run in debug mode

Code:
root@ckpgpay11core> ./sarascript.sh
+ echo Enter input filename:
Enter input filename:
+ read inputfile
mstgb_aialarm_20081021.wri
+ echo Enter path of outputfile with name:
Enter path of outputfile with name:
+ read outputfile
/tmp/testing
+ echo Enter tag to look for:
Enter tag to look for:
+ read GAA
20081013003507
+ awk
        # new record, reset array index
        /^----/ { i=0 }
        # accumulate record contents in array
        { a[++i]=$0 }
        # matching record
        /\$GAA/ {
                # dump array contents
                for (j=1; j<=i; j++)
                        print a[j]
                # get the rest of this record
                while (getline && $0 !~ /^----/)
                        print
                # print the record terminator and reset the array
                print
                i=0
        }
 mstgb_aialarm_20081021.wri

root@ckpgpay11core> ls -la | grep testing
-rw-r--r--   1 root     other          0 Oct 30 10:47 testing

# 13  
Old 10-30-2008
perl
Code:
undef $/;
open FH,"<file";
$str=<FH>;
@arr=split(/-+/,$str);
for $key(@arr){
if($key=~/pat/){
print $key;
}
}
close FH;

# 14  
Old 10-30-2008
Because the awk script is between single quotes, shell variables are not expanded.

You can change it like this to allow it to interpolate the variable:

Code:
        /'"$GAA"'/ {

Or else use :

Code:
awk -v SEARCHSTRING="$GAA" '
        ...
        $0 ~ SEARCHSTRING {
        ...
' inputfile > outputfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Finding lines with a regular expression, replacing them with blank lines

So the tag for this forum says all newbies welcome... All I want to do is go through my file and find lines which contain a given string of characters then replace these with a blank line. I really tried to find a simple command to do this but failed. Here's what I did come up with though: ... (2 Replies)
Discussion started by: Golpette
2 Replies

2. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

3. Shell Programming and Scripting

finding least out of selected lines

Hello, I have a file, which looks like: I want to print the row containg "PRO" in second column after comparing and finding the minimum value of fifth column present in all "PRO". and likewise for every other string present in second column. I am using : filename=list... (2 Replies)
Discussion started by: CAch
2 Replies

4. Shell Programming and Scripting

Printing all lines before a specific string and a custom message 2 lines after

Hello all, I need to print all the lines before a specific string and print a custom message 2 lines after that. So far I have managed to print everything up the string, inclusively, but I can't figure out how to print the 2 lines after that and the custom message. My code thus far is:... (4 Replies)
Discussion started by: SEinT
4 Replies

5. Shell Programming and Scripting

Finding a string with another string is found

finding a string with another string is found EX: abs c/- i want to find /-, then copy abs. i know it's easy use awk, but my problem is the substr syntax. substr($2,2,2) will give me /- but the conflict is /- is not always the second characted of the second string. (11 Replies)
Discussion started by: engr.jay
11 Replies

6. Shell Programming and Scripting

Finding lines matching the Pattern and their previous lines in a file

Hi, I am trying to locate the occurences of certain pattern like 'Possible network disconnect' in a text file. I can get the actual lines matching the pttern using: grep -w 'Possible network disconnect' file_name. But I am more interested in getting the timing of these events which are... (7 Replies)
Discussion started by: sagarparadkar
7 Replies

7. Shell Programming and Scripting

Reading n number of lines after finding string

I am trying to search a file for a value: "Top 30 reject reasons" and want the next 30 lines after that and output in a text file. If I knew the line number, I can use a combination of head and tail commands to get my results, but this doesn't seem to work when I don't have a line number. I... (2 Replies)
Discussion started by: oriqin
2 Replies

8. Shell Programming and Scripting

search string in a file and retrieve 10 lines including string line

Hi Guys, I am trying to write a perl script to search a string "Name" in the file "FILE" and also want to create a new file and push the searched string Name line along with 10 lines following the same. can anyone of you please let me know how to go about it ? (8 Replies)
Discussion started by: sukrish
8 Replies

9. Emergency UNIX and Linux Support

Getting last 50 lines after finding a word

Hi All, I have a requirement as below I need find for a string in a log file and once i found that string i need to send a mail.Thsi can be done thru grep and mailx cmnd. Here once i found the string i need to send last 50 lines(up) from where string found. Actually they require to find... (9 Replies)
Discussion started by: pssandeep
9 Replies

10. Shell Programming and Scripting

help with finding a string

Hello folks I have a text file abcd.txt and has a line starting with number '8'. I have a string in this line starting at position 'a' to position 'b' also this string is a number and have to be reduced by 1. there is also a problem that it has to be padded with zeros to make the string... (2 Replies)
Discussion started by: sandyg
2 Replies
Login or Register to Ask a Question