Shell Script for formatted output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell Script for formatted output
# 8  
Old 07-29-2017
Below is the out put of wrapped in CODE:

Code:
IOALPPRXXBD_ALPGLGENFAALL
06/26/2017 23:40:40
06/26/2017 23:49:19
06/27/2017 23:40:23
06/27/2017 23:48:19
06/28/2017 23:41:36
06/28/2017 23:48:47
06/29/2017 23:40:55
06/29/2017 23:50:27
07/01/2017 00:34:52
07/01/2017 00:45:50
07/04/2017 00:27:59
07/04/2017 00:36:18
07/05/2017 23:40:47
07/05/2017 23:48:56
07/06/2017 23:40:41
07/06/2017 23:50:15
07/07/2017 23:40:48
07/07/2017 23:51:10
07/10/2017 23:41:03
07/10/2017 23:54:41
07/11/2017 23:40:30
07/11/2017 23:52:19
07/12/2017 23:40:43
07/12/2017 23:48:29
07/13/2017 23:41:04
07/13/2017 23:49:59
07/14/2017 23:40:44
07/14/2017 23:50:41
07/17/2017 23:40:45
07/17/2017 23:49:50
07/18/2017 23:40:52
07/18/2017 23:48:43
07/19/2017 23:40:26
07/19/2017 23:49:10
07/20/2017 23:40:46
07/20/2017 23:50:11
07/21/2017 23:40:48
07/21/2017 23:51:21
07/24/2017 23:40:25
07/24/2017 23:49:04
07/25/2017 23:40:44
07/25/2017 23:48:21
07/26/2017 23:41:09
07/26/2017 23:48:30
07/27/2017 23:40:35
07/27/2017 23:50:31
07/28/2017 23:41:23
07/28/2017 23:51:17



But how can I display the value side by side, I am getting the data from a logfile when the job starts and job ends , so the out put should be :


Code:
Job Name                               Start Time                 End Time
IOALPPRXXBD_ALPGLGENFAALL 06/26/2017 23:40:40 06/26/2017 23:49:19
IOALPPRXXBD_ALPGLGENFAALL 06/27/2017 23:40:23 06/27/2017 23:48:19
IOALPPRXXBD_ALPGLGENFAALL 06/28/2017 23:41:36 06/28/2017 23:48:47

Any hint please ?

Last edited by Scrutinizer; 07-29-2017 at 10:54 AM.. Reason: code tags
# 9  
Old 07-29-2017
Quote:
Originally Posted by Sandeep Behera
But how can I display the value side by side, I am getting the data from a logfile when the job starts and job ends.
[...]
Any hint please ?
This is actually quite easy, you just need to think things through first. Lets go back to your original file:

Code:
06/26/2017 23:40:40       CAUAJM_I_10082 [aspsun14 connected for IOALPPRXXBD_ALPGLGENFAALL 55443.15215291.1]
 06/26/2017 23:40:40       CAUAJM_I_40245 EVENT: CHANGE_STATUS    STATUS: STARTING        JOB: IOALPPRXXBD_ALPGLGENFAALL MACHINE: aspsun14
 06/26/2017 23:40:42       CAUAJM_I_40245 EVENT: CHANGE_STATUS    STATUS: RUNNING         JOB: IOALPPRXXBD_ALPGLGENFAALL MACHINE: aspsun14
 06/26/2017 23:49:19       CAUAJM_I_40245 EVENT: CHANGE_STATUS    STATUS: SUCCESS         JOB: IOALPPRXXBD_ALPGLGENFAALL MACHINE: aspsun14        EXITCODE:  0
 06/27/2017 23:40:23       CAUAJM_I_40245 EVENT: CHANGE_STATUS    STATUS: STARTING        JOB: IOALPPRXXBD_ALPGLGENFAALL MACHINE: aspsun14
 06/27/2017 23:40:24       CAUAJM_I_10082 [aspsun14 connected for IOALPPRXXBD_ALPGLGENFAALL 55443.15236942.1]
 06/27/2017 23:40:25       CAUAJM_I_40245 EVENT: CHANGE_STATUS    STATUS: RUNNING         JOB: IOALPPRXXBD_ALPGLGENFAALL MACHINE: aspsun14
 06/27/2017 23:48:19       CAUAJM_I_40245 EVENT: CHANGE_STATUS    STATUS: SUCCESS         JOB: IOALPPRXXBD_ALPGLGENFAALL MACHINE: aspsun14        EXITCODE:  0
 06/28/2017 23:41:36       CAUAJM_I_40245 EVENT: CHANGE_STATUS    STATUS: STARTING        JOB: IOALPPRXXBD_ALPGLGENFAALL MACHINE: aspsun14
 06/28/2017 23:41:37       CAUAJM_I_10082 [aspsun14 connected for IOALPPRXXBD_ALPGLGENFAALL 55443.15258301.1]
 06/28/2017 23:41:38       CAUAJM_I_40245 EVENT: CHANGE_STATUS    STATUS: RUNNING         JOB: IOALPPRXXBD_ALPGLGENFAALL MACHINE: aspsun14
 06/28/2017 23:48:47       CAUAJM_I_40245 EVENT: CHANGE_STATUS    STATUS: SUCCESS         JOB: IOALPPRXXBD_ALPGLGENFAALL MACHINE: aspsun14        EXITCODE:  0


Of this file you only need the lines containing "STATUS: STARTING" and some ending notification. This could perhaps be "STATUS: SUCCESS", but somehow i don't believe that all jobs end that way - what other possible ending codes do you have, because your script will need to cover them too.

Let us go on, assuming for the moment that the only endng condition is "SUCCESS", this can be corrected later.

Next we make up some "rules" what to do with the respective lines, because at some point we need to read our input line by line:

- When we encounter a "STARTING" line we need to remember two things, the job name and the timestamp.

- When we encounter a "SUCCESS"-line we need to read also two things: the job name and the timestamp. But we don't need to store ("remember") them: the job name should be searched in our list of remembered (=started) jobs. If it is found, the stored starting time, the end time and the job name is printed.

The last point begs two questions: what are we going to do if we encounter a job with a start but no end? And what are we going to do with jobs with an end but no start?

Here is the skeleton of a shell script that implements what i said above. Neither is it very clever nor very mature, its intention to make it obvious how to implement common reasing like above into code. It also won't take the raised questions into account and implicitly assume that all jobs end with success and every starting job also ends and vice versa.

We start by filtering and displaying only the lines we are interested in:

Code:
#! /bin/ksh

typeset infile="/path/to/some/file"      # file we read from
typeset cond=""                               # condition of the job, "STARTING" or "SUCCESS"
typeset name=""                              # jobs name

grep -e "STATUS: SUCCESS" -e "STATUS: STARTING" "$infile" |\
while read junk junk junk junk junk junk cond junk name junk ; do
     echo "name is: $name   condition is: $cond"
done

exit 0

Now let that run and watch if: a) the lines are filtered correctly and b) the names and conditions are displayed correctly. You can let the shell split the input lines and use different variables to distribute the split input to (all the ones i am not interested in i name "junk" out of habit), but it is easy to get the number of fields wrong in long lines. So try and verify it before going on. It is good practice make sure everything is correct so far before going on.

Next thing is to implement the "rules" we identified above. First the STARTING-lines:

We need to remember the date and time, so two of the fields not interested in before are now in a variable (junk->date, junk->time).

Code:
#! /bin/ksh

typeset infile="/path/to/some/file"      # file we read from
typeset date=""                               # jobs date
typeset time=""                               # jobs tme
typeset cond=""                               # condition of the job, "STARTING" or "SUCCESS"
typeset name=""                              # jobs name
count=1                                          # counter for the storage arrays
# aname[]                                      # these arrays hold the remembered jobs
# atime[]

grep -e "STATUS: SUCCESS" -e "STATUS: STARTING" "$infile" |\
while read date time junk junk junk junk cond junk name junk ; do

     echo "name is: $name   condition is: $cond  time is: $date $time"       # we leave that in for now, to see what the script works on

     case $cond in
          STARTING)
               typeset aname[$count]="$name"
               typeset atime[$count]="$date $time"
               (( count += 1 ))
               ;;

     esac
done

exit 0

You see we just add to the arrays whe we find a new job, all very easy. The next rule is a a little trickier. Upon encountering such a line we need to search our array for the respective job entry, then print both starting and ending times:

Code:
#! /bin/ksh

typeset    infile="/path/to/some/file"      # file we read from
typeset    date=""                               # jobs date
typeset    time=""                               # jobs tme
typeset    cond=""                               # condition of the job, "STARTING" or "SUCCESS"
typeset    name=""                              # jobs name
typeset -i count=1                               # counter for the newest element of the storage arrays
typeset -i i=1                                     # counter for searching the storage arrays
# aname[]                                      # these arrays hold the remembered jobs
# atime[]

grep -e "STATUS: SUCCESS" -e "STATUS: STARTING" "$infile" |\
while read date time junk junk junk junk cond junk name junk ; do

     echo "name is: $name   condition is: $cond  time is: $date $time"       # we leave that in for now, to see what the script works on

     case $cond in
          STARTING)
               typeset aname[$count]="$name"
               typeset atime[$count]="$date $time"
               (( count += 1 ))
               ;;

          SUCCESS)
               (( i = 1 ))
               while [ $i -lt ${#aname[@]} ] ; do               # walk though the array
                    if [ "${aname[$i]}" = "$name" ] ; then     # we found the corresponding entry
                         print "${aname[$i]} \t${atime[$i]} \t $date $time"
                    else
                         (( i += 1 ))
                    fi
               done
               ;;

     esac
done

exit 0

Again, this is not meant to be put in production as it is. But analysing how we arrived at it and how it works should give you the idea how to create a proper script for your purpose.

I hope this helps.

bakunin
These 2 Users Gave Thanks to bakunin For This Post:
# 10  
Old 08-11-2017
Sorry for the late response as I am out for some personal emergency. Thanks for your help, I am reviewing this now and will update you soon. Thanks again.

---------- Post updated at 07:31 AM ---------- Previous update was at 07:03 AM ----------

Of this file you only need the lines containing "STATUS: STARTING" and some ending notification. This could perhaps be "STATUS: SUCCESS", but somehow i don't believe that all jobs end that way - what other possible ending codes do you have, because your script will need to cover them too.

Let us go on, assuming for the moment that the only endng condition is "SUCCESS", this can be corrected later.

Next we make up some "rules" what to do with the respective lines, because at some point we need to read our input line by line:

- When we encounter a "STARTING" line we need to remember two things, the job name and the timestamp.

- When we encounter a "SUCCESS"-line we need to read also two things: the job name and the timestamp. But we don't need to store ("remember") them: the job name should be searched in our list of remembered (=started) jobs. If it is found, the stored starting time, the end time and the job name is printed.

The last point begs two questions: what are we going to do if we encounter a job with a start but no end? And what are we going to do with jobs with an end but no start?

Here is the skeleton of a shell script that implements what i said above. Neither is it very clever nor very mature, its intention to make it obvious how to implement common reasing like above into code. It also won't take the raised questions into account and implicitly assume that all jobs end with success and every starting job also ends and vice versa.

We start by filtering and displaying only the lines we are interested in:

++++++++++++++++++++++++++
++++++++++++++++++++++++++

Your above reply is very much correct and matches the exact requirement.

I tried the above script but it searches STARTING and SUCCESS for all job, whereas I need for specific job.
Also could you please tell what junk means ?
Can I use job name as $1 (1st arguement), so that it can changes as per requirement



Code:
#! /bin/ksh

name=$1

typeset    infile="/home/sbehera/out/event_demon.ACE*"      # file we read from
typeset    cond="SUCCESS"                               # condition of the job, "STARTING" or "SUCCESS"
typeset -i count=1                               # counter for the newest element of the storage arrays
typeset -i i=1                                     # counter for searching the storage arrays

echo $infile $cond $name $count $i


zgrep ""STATUS: SUCCESS"  "STATUS: STARTING"" "$infile" |\
while read date time junk junk junk junk cond junk name junk ; do

     echo "name is: $name   condition is: $cond  time is: $date $time"       # we leave that in for now, to see what the script works on


case $cond in
          STARTING)
               typeset aname[$count]="$name"
               typeset atime[$count]="$date $time"
               (( count += 1 ))
               ;;

          SUCCESS)
               (( i = 1 ))
               while [ $i -lt ${#aname[@]} ] ; do               # walk though the array
                    if [ "${aname[$i]}" = "$name" ] ; then     # we found the corresponding entry
                         print "${aname[$i]} \t${atime[$i]} \t $date $time"
                    else
                         (( i += 1 ))
                    fi
               done
               ;;

     esac
done

exit 0




Output:

$ sh run.sh IOXXXXXMO_XXXILEO
/home/sbehera/out/event_demon.ACE 
/home/sbehera/out/event_demon.ACE.07092017.gz 
/home/sbehera/out/event_demon.ACE.07102017.gz 
/home/sbehera/out/event_demon.ACE.07112017.gz 
/home/sbehera/out/event_demon.ACE.07122017.gz 
/home/sbehera/out/event_demon.ACE.07132017.gz 
/home/sbehera/out/event_demon.ACE.07142017.gz 
/home/sbehera/out/event_demon.ACE.08102017.gz SUCCESS IOXXXXXMO_XXXILEO 1 1
SUCCESS  STATUS:.gz: No such file or directory
STARTING.gz: No such file or directory
/home/sbehera//out/event_demon.ACE*.gz: No such file or directory


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 08-11-2017 at 08:54 AM..
# 11  
Old 08-11-2017
Some comments:
- yes, you can introduce $1 as the job name to be discriminated in the input stream. Use if - then - else constructs for the evaluation.
- don't double-use variables (here: name) as it leads at least to confusion if not malfunction of the script.
- the inconsistent / faulty double quoting in the zgrep command might lead to the "No such file or directory" error. On top, I suspect DOS line terminators It causes "SUCCESS STATUS" to be the second parameter and thus interpreted as a file name.
- junk is a dummy variable to read and discard unneeded fields in the input line.


EDIT: It is not the DOS line terminator problem; corrected in above

Last edited by RudiC; 08-11-2017 at 09:28 AM..
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Formatted output in PERL

Hi Techies, I'm a newbie to PERL, Please help me with following problem. I have an input text file like below cat Input.txt 418673132,P 492538858,P 384535478,P 521522357,I 529435679,I 183617024,P 184414408,I 735510689,P 736238343,I 411642045,I 412690979,I 104232783,I (2 Replies)
Discussion started by: mahi_mayu069
2 Replies

2. Shell Programming and Scripting

Shell Script Problems, Lose formatting when copy pasting from formatted file.

Hello, I'm having trouble with formatting some text via the terminal. I can get it perfectly formatted, but when I try and copy paste the text from the output file it loses it's formatting. Very frustrating! Basically I have 7 files (data data2 data3 data4 data5 data6 data7) containing a... (13 Replies)
Discussion started by: facetoe
13 Replies

3. UNIX for Dummies Questions & Answers

Request for Formatted Output

Can you please tell me how to just get only the output of dealers I & V information along with their subtotals in the next line of the file and create a new file, The dealer position along with corresponding totals may change everyday to any position above or below in the file, please help Thanks (2 Replies)
Discussion started by: Ariean
2 Replies

4. Shell Programming and Scripting

output - tab formatted - awk

Dear All, Good Day. I would like to hear your suggestions for the following problem: I have a file with 5 columns with some numbers in 16 lines as shown below. Input file: Col 1 Col 2 Col 3 Col 4 Col 5 12 220 2 121 20 234 30 22 9... (3 Replies)
Discussion started by: Fredrick
3 Replies

5. Shell Programming and Scripting

Formatted output of shell script

Hello, I am working on one script which is giving output as a pipe "|" separated abcd | efgh | 20090745 abcdefgh | efg | 20090622 Can any one please help me i want it to be formatted as pipe will be aligned, or the output looks like a table. (2 Replies)
Discussion started by: vikash_k
2 Replies

6. Shell Programming and Scripting

cut - columns with formatted Output

Hi I have the input file as below ***TEST10067 00567GROSZ 099 00567CTCTSDS90 ***TEST20081 08233GROZWEWE 00782GWERW899 ***TEST30088 08233GROZWEWE 00782GWERW899 I am finding the lines starting with *** and outputing as below TEST10067 TEST20081 TEST30088 I need a space between TEST1... (9 Replies)
Discussion started by: dhanamurthy
9 Replies

7. Shell Programming and Scripting

Formatted Output

Hi I have the following lines in a file SWPRC000001NOT STATED 1344 SWPRC000001NOT STATED 1362 SWPRC000001NOT STATED 1418 SWPRC000001NOT STATED 1436 SWPRC000001NOT STATED ... (6 Replies)
Discussion started by: dhanamurthy
6 Replies

8. Shell Programming and Scripting

Formatted output - awk

Hi I have the following records in a file SABN YOURTUBE 000514 7256 SACN XYOUDSDF 000514 7356 SADN KEHLHRSER 000514 7656 SAEN YOURTUBE 000514 7156 SAFN YOURTUBE 000514 7056 I need to put this in the format like this printf '%s %-50s %6s %-6s\n' I am not going to read individual... (3 Replies)
Discussion started by: dhanamurthy
3 Replies

9. Shell Programming and Scripting

formatted output with commas

var=12345 echo $var >>>12345 printf "%8.1f \n" $var >>> 12345.0 How to get this as 12,345? I suppose I could break into sections by dividing by 1000 or 1000000. But, is the a trick to this? (4 Replies)
Discussion started by: joeyg
4 Replies

10. Shell Programming and Scripting

Formatted output in KSH

Hi, Is there some way to get formatted output in ksh? Something like a properly alligned tabular format. I tried adding '\t' to echo statements, but it doesn't come properly alligned 'hello' A simple Hello 'helloworld' A helloworld statement I need the second coloumn to... (1 Reply)
Discussion started by: psynaps3
1 Replies
Login or Register to Ask a Question