Awk:String search more than one time and capture OP


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk:String search more than one time and capture OP
# 1  
Old 02-28-2015
Awk:String search more than one time and capture OP

Dear All

During one of mine script developemnt i am stuch at one sub part. Requiremnt is as below kindly help me.

IP file:
Code:
2015-02-28 10:10:15 AL M UtranCell UtranCell=RTE001X (unavailable)
2015-02-28 10:10:15 AL M UtranCell UtranCell=RTE001Y (unavailable)
2015-02-28 10:10:15 AL M UtranCell UtranCell=RTE001Z (unavailable)
2015-02-28 10:10:17 AL M UtranCell UtranCell=KDA003X (unavailable)
2015-02-28 10:10:17 AL M UtranCell UtranCell=KDA003Y (unavailable)
2015-02-28 10:10:17 AL M UtranCell UtranCell=KDA003Z (unavailable)
2015-02-28 10:12:06 AL * UtranCell UtranCell=KDA003X (unavailable)
2015-02-28 10:12:06 AL * UtranCell UtranCell=KDA003Y (unavailable)
2015-02-28 10:12:06 AL * UtranCell UtranCell=KDA003Z (unavailable)
2015-02-28 10:12:06 AL * UtranCell UtranCell=RTE001X (unavailable)
2015-02-28 10:12:06 AL * UtranCell UtranCell=RTE001Y (unavailable)
2015-02-28 10:12:06 AL * UtranCell UtranCell=RTE001Z (unavailable)
2015-02-28 10:16:50 AL M UtranCell UtranCell=RTE001X (unavailable)
2015-02-28 10:16:50 AL M UtranCell UtranCell=RTE001Y (unavailable)
2015-02-28 10:16:50 AL M UtranCell UtranCell=RTE001Z (unavailable)
2015-02-28 10:16:50 AL M UtranCell UtranCell=KDA003X (unavailable)
2015-02-28 10:16:50 AL M UtranCell UtranCell=KDA003Y (unavailable)
2015-02-28 10:16:50 AL M UtranCell UtranCell=KDA003Z (unavailable)
2015-02-28 10:18:30 AL * UtranCell UtranCell=RTE001X (unavailable)
2015-02-28 10:18:30 AL * UtranCell UtranCell=RTE001Y (unavailable)
2015-02-28 10:18:30 AL * UtranCell UtranCell=RTE001Z (unavailable)

Needed OP:

Here 1st column is site id,2ed/3rd column is down time marker "M" in input file,
4th/5th column is up time marked "*" in input file.
For 2ed occurance on KAD003 site no uptime("*") availalbe in IP file, so we need to right remark that site is still down.

Code:
RTE001X 2015-02-28 10:10:15 2015-02-28 10:12:06
RTE001Y 2015-02-28 10:10:15 2015-02-28 10:12:06
RTE001Z 2015-02-28 10:10:15 2015-02-28 10:12:06
KDA003X 2015-02-28 10:10:17 2015-02-28 10:12:06
KDA003Y 2015-02-28 10:10:17 2015-02-28 10:12:06
KDA003Z 2015-02-28 10:10:17 2015-02-28 10:12:06
RTE001X 2015-02-28 10:16:50 2015-02-28 10:18:30
RTE001Y 2015-02-28 10:16:50 2015-02-28 10:18:30
RTE001Z 2015-02-28 10:16:50 2015-02-28 10:18:30
KDA003X 2015-02-28 10:16:50 Down Down
KDA003Y 2015-02-28 10:16:50 Down Down
KDA003Z 2015-02-28 10:16:50 Down Down

Thanks in advance

Regards
Jaydeep Sadaria
# 2  
Old 02-28-2015
Try:

Code:
awk ' 
  {
    split($6,F,/=/)
    timestamp=$1 OFS $2
  }
  $4=="M" {
    if(up_records)
      for(i in DOWN) {
        print i, DOWN[i], (i in UP)?UP[i]:"Down Down"
        delete UP[i]
        delete DOWN[i]
      }
      up_records=0
      DOWN[F[2]]=timestamp
  } 
  $4=="*" {
    UP[F[2]]=timestamp
    up_records=1
  }
  END {
    for(i in DOWN) {
      print i, DOWN[i], (i in UP)?UP[i]:"Down Down"
    }
  }
' file

# 3  
Old 02-28-2015
Thanks for reply. But i am getting error in same.

However i had make some what simple by converting IP file as below by sorting and awk option.
In same i need to check if 3rd column is "M" in firest line and "*" in next line then i need both line OP in single line and if after reading "M" in third column i cant find "*" in next line just put remark"site down"

Suggest for same.

IP file:
Code:
2015-02-28 10:10:17 M KDA003X
2015-02-28 10:12:06 * KDA003X
2015-02-28 10:16:50 M KDA003X
2015-02-28 10:10:17 M KDA003Y
2015-02-28 10:12:06 * KDA003Y
2015-02-28 10:16:50 M KDA003Y
2015-02-28 10:10:17 M KDA003Z
2015-02-28 10:12:06 * KDA003Z
2015-02-28 10:16:50 M KDA003Z
2015-02-28 10:10:15 M RTE001X
2015-02-28 10:12:06 * RTE001X
2015-02-28 10:16:50 M RTE001X
2015-02-28 10:18:30 * RTE001X
2015-02-28 10:10:15 M RTE001Y
2015-02-28 10:12:06 * RTE001Y
2015-02-28 10:16:50 M RTE001Y
2015-02-28 10:18:30 * RTE001Y
2015-02-28 10:10:15 M RTE001Z
2015-02-28 10:12:06 * RTE001Z
2015-02-28 10:16:50 M RTE001Z
2015-02-28 10:18:30 * RTE001Z

Regards
Jaydeep Sadaria
# 4  
Old 02-28-2015
Try also (using part of Scrutinizer's approach):
Code:
awk     '               {split($6, F, /=/)
                         SITE=F[2]
                         timestamp=$1 OFS $2
                        }
         $4=="M"        {DOWN[SITE]=timestamp
                        }
         $4=="*"        {print SITE, DOWN[SITE], timestamp
                         delete DOWN[SITE]
                        }
         END            {for(i in DOWN) {print i, DOWN[i], "Down Down"}
                        }
        ' file
KDA003X 2015-02-28 10:10:17 2015-02-28 10:12:06
KDA003Y 2015-02-28 10:10:17 2015-02-28 10:12:06
KDA003Z 2015-02-28 10:10:17 2015-02-28 10:12:06
RTE001X 2015-02-28 10:10:15 2015-02-28 10:12:06
RTE001Y 2015-02-28 10:10:15 2015-02-28 10:12:06
RTE001Z 2015-02-28 10:10:15 2015-02-28 10:12:06
RTE001X 2015-02-28 10:16:50 2015-02-28 10:18:30
RTE001Y 2015-02-28 10:16:50 2015-02-28 10:18:30
RTE001Z 2015-02-28 10:16:50 2015-02-28 10:18:30
KDA003X 2015-02-28 10:16:50 Down Down
KDA003Y 2015-02-28 10:16:50 Down Down
KDA003Z 2015-02-28 10:16:50 Down Down

# 5  
Old 03-06-2015
Quote:
Originally Posted by jaydeep_sadaria
Thanks for reply. But i am getting error in same.
What error are you getting?
# 6  
Old 03-06-2015
Getting AWK syntex error.

Regards
Jaydeep
# 7  
Old 03-06-2015
Quote:
Originally Posted by jaydeep_sadaria
Getting AWK syntex error.

Regards
Jaydeep
would it be too much to ask for the exact error?
Thanks for 'same'.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk help string capture

Dear All My input file as under. From it I want op fine as mention below. Kindly help. I use below code but not help. code: awk -F" " '{print $2}' ip file: "BSCJNGR_IPA17_C" 030 131207 1305 RXOCF-353 PBD011_BGIL BOTH AC FAULTY "BSCJNGR_IPA17_C" 991 131207 1637 RXOCF-224 NAV001_BGIL ... (5 Replies)
Discussion started by: jaydeep_sadaria
5 Replies

2. Shell Programming and Scripting

awk : Search for text between two time frame (12 hours)

I have created the script to grep the errors from weblogic logs files and redirecting output to file.txt ...From file.txt I'm using awk command to collect the past 20 mins output...The script running from cron every 15 mins... The script working well... Now the challenges, I'm trying to use... (27 Replies)
Discussion started by: zenkarthi
27 Replies

3. Shell Programming and Scripting

Search string within a file and list common words from the line having the search string

Hi, Need your help for this scripting issue I have. I am not really good at this, so seeking your help. I have a file looking similar to this: Hello, i am human and name=ABCD. How are you? Hello, i am human and name=PQRS. I am good. Hello, i am human and name=ABCD. Good bye. Hello, i... (12 Replies)
Discussion started by: royzlife
12 Replies

4. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

5. UNIX for Dummies Questions & Answers

Converting string date time to unix time in AWK

I'd like to convert a date string in the form of sun aug 19 09:03:10 EDT 2012, to unixtime timestamp using awk. I tried This is how each line of the file looks like, different date and time in this format Sun Aug 19 08:33:45 EDT 2012, user1(108.6.217.236) all: test on the 17th ... (2 Replies)
Discussion started by: bkkid
2 Replies

6. Shell Programming and Scripting

awk search between 2 digits a string

I would like to search between two a string. I thought this would be easy. The is always at the beginning of a line. The code: gawk '/^/{d=$1},/searchstring/,/^(d+1)/' or gawk '/^/,/searchstring/,/^/' did not return the desired result. inputfile.txt 999 some text searchstring some... (6 Replies)
Discussion started by: sdf
6 Replies

7. Shell Programming and Scripting

awk/sed string search and replace

Need help with either sed or awk to acheive the following file1 ----- In the amazon forest The bats eat all the time... mon tue wed they would eat berries In the tropical forest The bats eat all the time... on wed bats eat nuts In the rain forest The bats eat all the time... on... (2 Replies)
Discussion started by: jville
2 Replies

8. Shell Programming and Scripting

Awk - find string, search lines below string for other strings

What's the easiest way to search a file for a specific string and then look for other instances after that? I want to search for all Virtual Hosts and print out the Server Name and Document Root (if it has that info), while discarding the rest of the info. Basically my file looks like this: ...... (6 Replies)
Discussion started by: Mbohmer
6 Replies

9. Shell Programming and Scripting

Awk command for search a string in lspv

I want to know wich hdisk have only one pvid and also display hdisk with two pvid. hdisk1 00c3fcd4e516183f testvg active hdisk2 00c3fcd4e516189b testvg active hdisk3 00c3fcd4e51618ec testvg ... (1 Reply)
Discussion started by: khalidou13
1 Replies

10. Shell Programming and Scripting

search string during a specific time frame

Can someone please help me with searching a string during a specific time frame. Below is the format of the time from my log file. "GET /AAM2009_wherewereheaded.wmv HTTP/1.1" 200 52307085 The search string I need is "AAM2009_wherewereheaded.wmv" I need to search the number of... (1 Reply)
Discussion started by: tadi18
1 Replies
Login or Register to Ask a Question