AWK: Grep Pattern and print help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK: Grep Pattern and print help
# 8  
Old 02-27-2012
Does the code I already gave you work or not?

What would this 'results line' look like? What data would you want printed on it? How would you calculate this data from previous data?

Don't tell me how you want to do it, tell me what your data looks like and what your output looks like... You've posted 3 versions of what they might look like now, some of which still have obvious typos, and none of which agree with each other.

You also didn't answer any questions I actually asked...
This User Gave Thanks to Corona688 For This Post:
# 9  
Old 02-28-2012
Sorry about not answering your questions. Its works but did not output in a row as desired. I will post the outcome of the last script. Thanks.

---------- Post updated 02-28-12 at 08:19 AM ---------- Previous update was 02-27-12 at 11:11 PM ----------

Sorry about not answering your questions. Its works but did not output in a row as desired. I will post the outcome of the scripts provided. Thanks.

Code:
awk '/^Line:/ {print ; getline ; print; getline ; print}' log1.txt
Line: 100
FSP: 10
LSP: 200
Line: 200
FSP: 20
LSP: 300
Line: 300
FSP: 10
LSP: 100

more log1.txt

Line: 100
FSP: 10
LSP: 200

Line: 200
FSP: 20
LSP: 300

Line: 300
FSP: 10
LSP: 100

more log1.scr

BEGIN { FS="\t"; OFS="\t" }
{
        # Replace the various has/and/width and their spaces with a single tab.
        gsub(/ *(has|and|width) */, "\t");
        # Eliminate some more extra spaces.
        gsub(/ *: */, ":");

        # Now that we have them arranged in sane columns:
        PF=""
        for(N=1; N<=NF; N++)
        {
                # Split NR:1 into A[1]="NR", A[2]="1"
                split($N, A, ":");

                # Print row of titles if this is the first line
                if(NR == 1)
                {
                        printf(PF "%s", A[1]);
                        PF=FS;
                }

                # Set the column to the value and nothing else
                $N=A[2];
        }

        if(NR==1)       printf("\n");

        # Print the row of data for every single line
} 1

awk -f log1.scr log1.txt

Line
100
10
200

200
20
300

300
10
100


Last edited by rtsiahaan; 02-28-2012 at 01:19 AM..
# 10  
Old 02-28-2012
Your input data doesn't resemble anything you posted, then.

If you won't post any of it unmangled, can you at least attach some of it?
# 11  
Old 02-28-2012
Thanks for your response.

Sorry I was just trying to simplified the log file. The actual log file is attached ( again truncated with 2 dataset only and I have >> 300 dataset). The key pattern I wanted to capture are line name,ORIGINAL TAPE INPUT, number of traces, sample rate, end time, first trace number , last trace number, first shotpoint and last shotpoint. The output desired is row for each dataset analysis.

Code:
line name = 76-145-294
ORIGINAL TAPE INPUT 856342
number of traces = 4812
trace header length = 240
number of samples = 2400
start time = 0 msec
end time = 4798 msec
sample rate = 2 msec
sample format = 1   (IBM Real)
trace number extracted from byte number 21, format Integer 4-Byte
  first trace number = 4155
  last trace number = 6375
  trace number increment = 5
shotpoint number extracted from byte number 17, format Integer 4-Byte
  first shotpoint = 424
  last shotpoint = 669
  shotpoint increment = 1
x coordinate extracted from byte number 73, format Integer 4-Byte
  first x coordinate =         65.00
  last x coordinate =        378.00
y coordinate extracted from byte number 77, format Integer 4-Byte
  first y coordinate =       3739.00
  last y coordinate =      57428.00
3D line number extracted from byte number 9, format Integer 4-Byte
  smallest 3D line value = 171001
  largest 3D line value = 174020
3D trace number extracted from byte number 21, format Integer 4-Byte
  smallest 3D trace value = 4155
  largest 3D trace value = 6375
minimum amplitude in file = -529.406
maximum amplitude in file = 529.375



line name = 76-145-294
ORIGINAL TAPE INPUT 856341
number of traces = 4800
trace header length = 240
number of samples = 2400
start time = 0 msec
end time = 4798 msec
sample rate = 2 msec
sample format = 1   (IBM Real)
trace number extracted from byte number 21, format Integer 4-Byte
  first trace number = 6380
  last trace number = 8695
  trace number increment = 5
shotpoint number extracted from byte number 17, format Integer 4-Byte
  first shotpoint = 638
  last shotpoint = 858
  shotpoint increment = -1
x coordinate extracted from byte number 73, format Integer 4-Byte
  first x coordinate =        340.00
  last x coordinate =        561.00
y coordinate extracted from byte number 77, format Integer 4-Byte
  first y coordinate =      50609.00
  last y coordinate =      98810.00
3D line number extracted from byte number 9, format Integer 4-Byte
  smallest 3D line value = 174005
  largest 3D line value = 177027
3D trace number extracted from byte number 21, format Integer 4-Byte
  smallest 3D trace value = 6380
  largest 3D trace value = 8695
minimum amplitude in file = -529.406
maximum amplitude in file = 529.375

# 12  
Old 02-28-2012
This is indeed extremely different, since all the data you posted before had all the values on one line...

You still haven't explained the kind of summary you want after all the other data.

Working on it.
# 13  
Old 02-28-2012
The desired output would be as follows:

76-145-294 856342 4812 2 4798 4155 6375 424 669
76-145-294 856341 4800 2 4798 6380 8695 638 858

Thanks
# 14  
Old 02-28-2012
Unless I misunderstood you you also wanted some sort of summary after all lines are processed but gave no details.

Code:
$ cat tape.awk

BEGIN {
        FS=" = ";       OFS="\t"
        # Load the list of fields you want
        while(getline<"fields.txt")
        {
                FIELD[$1]=$2
                FIELD[++N]=$2
        }
        # Remove the two lines below to not print a header line
        for(N=1; FIELD[N]; N++) $N=FIELD[N]
        print
}

# Delete needless extra spaces
{ gsub(/[ \t][ \t]+/, "");      sub(/^ +/, ""); }

# Shoehorn that one funny line into the format of the rest
/ORIGINAL TAPE INPUT/ { sub(/INPUT/, "INPUT = ");       }

# If it matches one of the fields we want, grab it.
FIELD[$1] {
        T=FIELD[$1]
        split($2, A, " ");


        # If we already have one of those, print and start over
        if(V[T])
        {
                for(N=1; FIELD[N]; N++)
                {
                        $N=V[FIELD[N]];
                        delete V[FIELD[N]]
                }
                NF=(N-1)
                print
        }

        V[T]=A[1];
}

END {
        if(V[FIELD[1]])
        {
                for(N=1; FIELD[N]; N++) $N=V[FIELD[N]];
                print
        }
}

$ cat fields.txt

line name = LINE-NAME
ORIGINAL TAPE INPUT = OTI
number of traces = NOT
sample rate = SR
end time = ET
first trace number = FTN
last trace number = LTN
first shotpoint = FSP
last shotpoint = LSP

$ awk -f tape.awk data
LINE-NAME       OTI     NOT     SR      ET      FTN     LTN     FSP     LSP
76-145-294      856342  4812    2       4798    4155    6375    424     669
76-145-294      856341  4800    2       4798    6380    8695    638     858

$

This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Recursively grep for a pattern and print that whole word

Hello Forum Members, I am trying to write a script for a requirement where i have to recursively search for a pattern and replace it with the new string in run time from user inputs grep -ohr "]*.xyz.com]*" $HOME/source/group/ | sort | uniq > $HOME/output.txt while read -r -u9 line; ... (4 Replies)
Discussion started by: raokl
4 Replies

2. AIX

Grep a pattern and print following n lines

Hi all, I am struck with the below requirement. I need to grep a particular pattern in a file and then print next n lines of it for further processing. I have used the below code grep -A 3 "pattern" filename But it is throwing error as below. grep: illegal option -- A Can... (14 Replies)
Discussion started by: ssk250
14 Replies

3. Shell Programming and Scripting

sed and awk usage to grep a pattern 1 and with reference to this grep a pattern 2 and pattern 3

Hi , I have a file where i have modifed certain things compared to original file . The difference of the original file and modified file is as follows. # diff mir_lex.c.modified mir_lex.c.orig 3209c3209 < if(yy_current_buffer -> yy_is_our_buffer == 0) { --- >... (5 Replies)
Discussion started by: breezevinay
5 Replies

4. Shell Programming and Scripting

How to print the lines between the pattern using awk/grep/sed?

Hi, I need a help to search a pattern and print the multiple lines between them. Input file: Tue May 29 12:30:33 EDT 2012:threadWebContainer : 357:com.travimp.hotelierlinks.abba.service.RequestHandler.requestService(String, ITICSDataSet): hotelCancelReservation request: ... (4 Replies)
Discussion started by: aroragaurav.84
4 Replies

5. Shell Programming and Scripting

grep based on pattern in a line and print the column before that

$ cat file.log Message Number = : Sending message 10:50:16^|^reqhdr.dummyid^=^02^|^reqhdr.timezone^=^GMT+05:30^|^DUMMYREQUEST^=^BH||||||||||||||||||$BD|OL|C|V||DummyAcctNo|02||24/12/2011|ST_DDM|DDM||||||||reqUUID110612105016$BT||||||||||||||||||$] Length I have the above line in the... (4 Replies)
Discussion started by: kalidass
4 Replies

6. Shell Programming and Scripting

Print the above and below lines for the grep pattern.

Hi, i would like to get the above and below lines of the grep pattern . For ex : file as below: chk1- aaaa 1-Nov chk2 -aaaa ########## chk1-bbbbbb 1-Nov chk2-bbbbbb ######### my search pattern is date : 1-Nov i need the o/p as below chk1- aaaa 1-Nov (6 Replies)
Discussion started by: expert
6 Replies

7. Solaris

Using grep to print just the pattern match

Hi all, Is it possible for grep to output just the pattern match and not the whole line when it comes across a match? I know you can adjust the number of trailing or leading lines that are printed, but am yet to find anything that outputs just the pattern match. Cheers, Tim (5 Replies)
Discussion started by: muzzaw
5 Replies

8. UNIX for Dummies Questions & Answers

Lynx Grep Pattern Match 2 conditions Print from Start to End

I am working on a scraping project and I am stuck at this tiny grep pattern match. Sample text : FPA List. FPA List. FPA List. FPA List. FPA List. FPA List. FPA List. FPA List. ABC Personal Planning Catherine K. Wat Cath Wat Catherine K. Wat Catherine K. Wat IFRAME:... (8 Replies)
Discussion started by: kkiran
8 Replies

9. Shell Programming and Scripting

Use to awk to match pattern, and print the pattern

Hi, I know how to use awk to search some expressions like five consecutive numbers, , this is easy. However, how do I make awk print the pattern that is been matched? For example: input: usa,canada99292,japan222,france59664,egypt223 output:99292,59664 (6 Replies)
Discussion started by: grossgermany
6 Replies

10. Shell Programming and Scripting

Grep for a pattern and print entire record

Hi friends, This is my very first post on forum, so kindly excuse if my doubts are found too silly. I am trying to automate a piece of routine work and this is where I am stuck at the moment-I need to grep a particular ID through a file containing many records(which start with <LRECORD> and end... (6 Replies)
Discussion started by: faiz1985
6 Replies
Login or Register to Ask a Question