Sponsored Content
Top Forums Shell Programming and Scripting Print Range Only Once Per File Post 302541752 by svermill on Monday 25th of July 2011 09:30:40 PM
Old 07-25-2011
Well, this certainly has promise. My out file looks pretty good:

Code:
100                                                           ^M
 90                                                           ^M
 80                                                           ^M
 70                                                           ^M
 60                                                           ^M
 50                                                           ^M
 40      *****                                                ^M
 30      *****                         *****                  ^M
 20 **********************************************************^M
 10 **********************************************************^M
   0....5....1....1....2....2....3....3....4....4....5....5....^M
             0    5    0    5    0    5    0    5    0    5    ^M
               CPU% per second (last 60 seconds)^M
                                                              ^M
    2324333323252323332323232333232323233323232323332323232333^M
    6567054485676464636365646445667575764674656365646566678765^M
100                                                           ^M
 90                                                           ^M
 80                                                           ^M
 70                                                           ^M
 60            *                                              ^M
 50    *       *                                              ^M
 40  * * *   * *    *    *     * * * * * *   *   **  * * * ***^M
 30 ***********#**********************************************^M
 20 ##########################################################^M
 10 ##########################################################^M
   0....5....1....1....2....2....3....3....4....4....5....5....^M
             0    5    0    5    0    5    0    5    0    5    ^M
               CPU% per minute (last 60 minutes)^M
              * = maximum CPU%   # = average CPU%^M
                                                                          ^M
    3333333733364333333333553333333533373343333333353333344644484447555655^M
    7788787188682777678686297797778078668826576668686789922047709892020101^M
100                                                                       ^M
 90                                                                       ^M
 80                                    *                       *          ^M
 70        *   *                       *                       *   *      ^M
 60        *   *           *           *           *       *   *   *   *  ^M
 50        *   *          **       *   *           *       * *************^M
 40 **********************************************************************^M
 30 **********************************************************************^M
 20 ######################################################################^M
 10 ######################################################################^M
   0....5....1....1....2....2....3....3....4....4....5....5....6....6....7.^M
             0    5    0    5    0    5    0    5    0    5    0    5    0 ^M
                   CPU% per hour (last 72 hours)^M
                  * = maximum CPU%   # = average CPU%^M

I'm just slowly coming up to speed on basic awk, though, and know nothing about ex or even heredocs. I was hoping to use some of what I've learned of awk to add some additional detail (such as printing the second field of the 'hostname Switch1' line from the in file or the in file name itself (which equals the hostname, actually, so it's six of one/half dozen of the other)). Is that same sort of thing possible here? Is it as easy as invoking "FILENAME" in awk?

Thank so much!

---------- Post updated at 01:04 PM ---------- Previous update was at 12:47 PM ----------

Just realized too that I don't seem to be able to do *.txt as my in file. I have to repeat this over a couple of hundred .txt files on an ongoing basis. Is there an equivalent to that with this approach?

Thanks again!

---------- Post updated at 03:06 PM ---------- Previous update was at 01:04 PM ----------

I was thinking about this over lunch. Maybe the range approach is not what I want. Maybe I want to match the first line that begins with "100" and print that line. Print every line thereafter until I reach the first line that ends with "CPU%." Print that line too. Continue to print every line until I once again find a line that ends with "CPU%." Print that line and exit, move to next file. I wonder if that's actually simpler than trying to solve this with the range function?

---------- Post updated at 05:59 PM ---------- Previous update was at 03:06 PM ----------

So the simplest - and in retrospect best - solution was simply to print the range in between the two 'show' commands in the input files:

Code:
	ls *.txt | xargs awk '/show proc cpu hist/,/show run/'

All that I need to do now is how to figure out how to print everything in between those two lines but not including those two lines. And I need to get the host or file name printed for each iteration (which I'm pretty confident I can figure out).

---------- Post updated at 07:30 PM ---------- Previous update was at 05:59 PM ----------

OK, I just need one final piece. I realize that this is probably very inelegant, but it almost works and I've once again lost an entire day to a single script!

Code:
#!/bin/ksh
ls *.txt | xargs awk '
  		  
    FILENAME != last {
    fn = FILENAME;
    gsub( ".txt", "", fn );
    last = FILENAME;
    }
        
    /show proc cpu hist/,/show run/ { print $0 };
  		
' | awk '!/show proc cpu hist/&&!/show run/' > "CPU History"

The above includes a piece given to me by agama yesterday to strip off the ".txt" extension from FILENAME. However, I haven't been able to figure out how to use what's left in the way that I want. Prior to each block of CPU history, I'd like to print "FILENAME:" beforehand. In yesterdays' case, I was building a .csv, so I wanted the filename on every single line of output. In this case I'm not building a CSV and will import the text directly into a report. So I'd like to figure out how to print the filename plus ":" once and then the CPU history block. The closest I've come was to actually awk out $2 of 'hostname Switch1' but that comes in the input file after the CPU history block, so it was printing below instead of above. And for some reason my ":" was actually printing on a new line below the hostname?? Is this more difficult than it sounds or should I be able to accomplish this easily?

Thanks.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Need to print file names in a certain date range using ls

I need to list only file names of a specific date from the command ls -lt. :confused: (2 Replies)
Discussion started by: Shamwari
2 Replies

2. Shell Programming and Scripting

print range between two patterns if it contains a pattern within the range

I want to print between the range two patterns if a particular pattern is present in between the two patterns. I am new to Unix. Any help would be greatly appreciated. e.g. Pattern1 Bombay Calcutta Delhi Pattern2 Pattern1 Patna Madras Gwalior Delhi Pattern2 Pattern1... (2 Replies)
Discussion started by: joyan321
2 Replies

3. Shell Programming and Scripting

How to print range of a line?

Hi , I have a file content like following. In few lines I have fields with '"' and in few not. "Country" character varying(150), "Region" character varying(60), "Total Page Requests" numeric, "Pages Served" numeric, "Ad request" numeric, Ads Served ... (5 Replies)
Discussion started by: sussus2326
5 Replies

4. Shell Programming and Scripting

Print pattern range to a new file

Hi Everyone! I really appreciate all of your help, I'm learning so much, can't wait until I get good enough to start answering questions! I have a problem ... from one large file, I'd like to create multiple new files for each pattern block beginning with /^ISA/ ending with /^IEA/ ... (2 Replies)
Discussion started by: verge
2 Replies

5. Shell Programming and Scripting

How to print first matching range in awk?

following is input - <Schema> <schema_name>admin</schema_name> <Version>1.1</Version> <schema_name>admin</schema_name> <Version>1.2</Version> </Schema> ... (12 Replies)
Discussion started by: thearpit
12 Replies

6. Shell Programming and Scripting

awk to print range of fields

Hi file.in and file.out are in csv format. the code I have now is, cat file.in | awk -F"," '!($1$2$3$4$5$6$7$8 in a){a;print $0}' > file.out Here, I am printing entire line using $0. however, I want to print $1 to $150 and it should be in csv format. Cut -d is not good in performace.... (3 Replies)
Discussion started by: krishnix
3 Replies

7. Shell Programming and Scripting

Print range of numbers

Hi I am getting an argument which specifies the range of numbers. eg: 7-15 Is there a way that i can easily (avoiding loop) print the range of number between and including the specified above. The above example should translate to 7,8,9,10,11,12,13,14,15 (3 Replies)
Discussion started by: tostay2003
3 Replies

8. UNIX for Dummies Questions & Answers

How to print specific range using awk?

I want to print specific range of rows and then its columns using awk command. lets say if a file contain 15 line then i need to print line 5 to 9. awk '{print;for( NR>=5&&NR<=9); do; print "<tr>\n<td>"$1"</td><td>"$2"</td><td>"$3"</td>\n</tr" done}' xyz.csv not sure what's wrong... (9 Replies)
Discussion started by: amyt1234
9 Replies

9. Shell Programming and Scripting

awk to print out lines that do not fall between range in file

In the awk below I am trying to print out those lines in file2 that are no between $2 and $3 in file1. Both files are tab-delimeted and I think it's close but currently it is printeing out the matches. The --- are not part of the files they are just to show what lines match or fall into the range... (6 Replies)
Discussion started by: cmccabe
6 Replies

10. UNIX for Beginners Questions & Answers

Advise on how to print range of lines above and below a number?

Hi, I have attached an output file which is some kind of database file mapping. It is basically like an allocation mapping of a tablespace and its datafile/s. The output is generated by the SQL script that I found from 401 Authorization Required Excerpts of the file are as below: ... (2 Replies)
Discussion started by: newbie_01
2 Replies
All times are GMT -4. The time now is 09:05 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy