How to grep/sed selected data from a command or file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to grep/sed selected data from a command or file?
# 1  
Old 02-22-2016
Display How to grep/sed selected data from a command or file?

Below is the output of a DB2 command. Now I have 2 requirements...

Code:
Database Partition 0 -- Database TESTDB1 -- Active Standby -- Up 213 days 02:33:07 -- Date 02/22/2016 17:04:50

HADR Information:
Role          State                SyncMode HeartBeatsMissed   LogGapRunAvg (bytes)
Standby     Peer                 Nearsync   0                        0

ConnectStatus   ConnectTime                                        Timeout
Connected         Fri Oct 23 10:33:33 2015 (1445610813)    120

ReplayOnlyWindowStatus ReplayOnlyWindowStartTime             MaintenanceTxCount
Inactive                        N/A                                              0

LocalHost                                LocalService
10.180.46.52                             DB2_xxxdhadr_se

RemoteHost                               RemoteService      RemoteInstance
10.180.46.53                             DB2_xxxdhadr_pr    wcm1qa1

PrimaryFile  PrimaryPg  PrimaryLSN
S0001601.LOG 559        0x0000000616E4C44E

StandByFile  StandByPg  StandByLSN         StandByRcvBufUsed
S0001601.LOG 559        0x0000000616E4C44E 0%

1) How to grep/sed a particular data from above command to get below output...

Code:
Role     SyncMode    LogGapRunAvg (bytes)  ConnectStatus  ReplayOnlyWindowStatus ReplayOnlyWindowStartTime
Standby  Nearsync    0                     Connected      Inactive               N/A

2) Say, If the status of ConnectStatus other than Connected then need to send an alert to team.

is there a way to get the desired output with our running the same command multiple times or will it be a good idea to execute the command once and get the output to a file and then grep/sed the desired columns...

Last edited by Don Cragun; 02-22-2016 at 10:58 PM.. Reason: Add CODE and ICODE tags.
# 2  
Old 02-23-2016
That's not that easy as your fields seem to conatin field delimiters as well. In the LogGapRunAvg (bytes) case we're lucky in that it's the last filed on a line. You can define the fields to be extracted by naming them in the HD variable. Try
Code:
awk '
NR == 1 {for (HN = n = split (HD, T); n>0; n--) FL[T[n]]
         print HD
        }

L       {for (i=1; i<=CN; i++) printf "%s%s", $X[i], FS
         delete X
         CN = L = 0
        }

        {for (i=1; i<=NF; i++) if ($i in FL)    {X[++CN] = i
                                                 L = 1
                                                }
        }

END     {printf RS
        }
' HD="Role SyncMode LogGapRunAvg ConnectStatus ReplayOnlyWindowStatus ReplayOnlyWindowStartTime" file
Role SyncMode LogGapRunAvg ConnectStatus ReplayOnlyWindowStatus ReplayOnlyWindowStartTime
Standby Nearsync 0 Connected Inactive N/A

This User Gave Thanks to RudiC For This Post:
# 3  
Old 02-23-2016
Thx. for your response... I will apply and let you know the results...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to fetch only selected data in CSV

Hi Team, I m getting my script commands output like given below GETA-TILL-INF; U-UU-YRYT-NOD-6002 2015-05-14 THU 19:44:10 C2221 RETRIEVE TILL INFORMATION : COMPLD ---------------------------------------------------------------------- CONNECT_CARD_ID ... (9 Replies)
Discussion started by: Ganesh Mankar
9 Replies

2. Shell Programming and Scripting

Get some data from a list, using grep or sed

Hi, my dear friends, I have to use very frequently the command of qsum to check the free node for job submitting. However, qsum always shows all the nodes regardless of their status. occupied p1 100000mb 48:00:00 1 p72 51200mb -- 1 p73 51200mb -- ... (4 Replies)
Discussion started by: liuzhencc
4 Replies

3. Shell Programming and Scripting

Help with Passing the Output of grep to sed command - to find and replace a string in a file.

I have a file example.txt as follows :SomeTextGoesHere $$TODAY_DT=20140818 $$TODAY_DT=20140818 $$TODAY_DT=20140818I need to automatically update the date (20140818) in the above file, by getting the new date as argument, using a shell script. (It would even be better if I could pass... (5 Replies)
Discussion started by: SriRamKrish
5 Replies

4. Shell Programming and Scripting

sed to delete selected line from file

Ultimate goal is to delete lines from before and after a block of lines in a given file. First attempt was something like this: sed -e '1,/STARTUP/ d' inputfile.txt > outputfile.txt but that deleted everything down to and including the line with STARTUP. I need to delete everything before... (6 Replies)
Discussion started by: edstevens
6 Replies

5. Shell Programming and Scripting

Remove data from grep command using the pattern in the file

Hi, I writing a shell program to remove the data from output of the find which matches a list in a file I am using the below find command to get the list of files x=`find . -name test*.dat` the output of the find command is as follows test1.dat test2.dat test3.dat test4.dat... (4 Replies)
Discussion started by: pals70423
4 Replies

6. Shell Programming and Scripting

Router ping log extract data from it Awk/Sed/grep

Hi, I am new to this world.. Using expect i loging to router and checking ping response to my links. I need to genarate report using this output and that report contains only three file link name, packet loss, latency. my output of script is like below: -bash-3.00$ monmw/mwbkp... (2 Replies)
Discussion started by: jkmistry
2 Replies

7. Shell Programming and Scripting

Need help please with Grep/Sed command to extract text and numbers from a file

Hello All, I need to extract lines from a file that contains ALPHANUMERIC and the length of Alphanumeric is set to 16. I have pasted the sample of the lines from the text file that I have created. My problem is that sometimes 16 appears in other part of the line. I'm only interested to... (14 Replies)
Discussion started by: mnassiri
14 Replies

8. Shell Programming and Scripting

Delete a line between selected lines using sed or any other command

I want to delete a line between selected lines using sed: e.g. : Between "bus" to "pins", delete lines conaining "signal" word. Input : bus direction signal new signal old pins signal ok end Desired Output: bus direction pins signal end (4 Replies)
Discussion started by: nehashine
4 Replies

9. UNIX for Dummies Questions & Answers

I want some selected data from first file and put into other file in specified format

I have a file with follwing content ---------------------------------- SCHEDULE XXXXXXXXX#JOBCOUNT ON EVERYDAY AT 0000 PRIORITY 50 SCHEDULE XXXXXXXXX#ABCDEFGH ON EVERYDAY AT 0001 PRIORITY 29 SCHEDULE... (5 Replies)
Discussion started by: shreyas
5 Replies

10. Shell Programming and Scripting

Big data file - sed/grep/awk?

Morning guys. Another day another question. :rolleyes: I am knocking up a script to pull some data from a file. The problem is the file is very big (up to 1 gig in size), so this solution: for results in `grep "^\ ... works, but takes ages (we're talking minutes) to run. The data is held... (8 Replies)
Discussion started by: dlam
8 Replies
Login or Register to Ask a Question