Print string after the word match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print string after the word match
# 1  
Old 11-11-2013
Print string after the word match

Hi,

I have the logs :
Code:
cat logsx.txt

744906,{"reportingGroups":[{"absoluteAccumulated":{"bidirVol":1442755910,"expiryDate":{"volume":"23-11-2013T02:00:22"},"previousExpiryDate":{"time":"02-10-2013T06:14:21","volume":"23-10-2013T02:00:10"},"resetPeriod":{"volume":"monthly day 23 02:00"}},"selected":"no","subsGName":"Matrix_1GB_16","subscriptionDate":"02-10-2013T06:14:21","validityTime":1178681},{"absoluteAccumulated":{"bidirVol":102098178,"previousExpiryDate":{"time":"09-11-2013T10:53:00","volume":"09-11-2013T10:53:00"}},"restartInfo":"09-11-2013T10:53","selected":"yes","subsGName":"1GB7D0800","subscriptionDate":"09-11-2013T10:53:00","validityTime":0}],"version":"2.0"}
678874,{"reportingGroups":[{"absoluteAccumulated":{"bidirVol":1318102561,"expiryDate":{"volume":"23-11-2013T02:00:20"},"previousExpiryDate":{"time":"02-10-2013T03:12:17","volume":"23-10-2013T02:00:02"},"resetPeriod":{"volume":"monthly day 23 02:00"}},"selected":"no","subsGName":"Matrix_5GB_16","subscriptionDate":"02-10-2013T03:12:17","validityTime":1051965},{"absoluteAccumulated":{"bidirVol":20004753,"previousExpiryDate":{"time":"10-11-2013T21:54:00","volume":"10-11-2013T21:54:00"}},"restartInfo":"10-11-2013T21:54","selected":"yes","subsGName":"1GB7D0800","subscriptionDate":"10-11-2013T21:54:00","validityTime":0}],"version":"2.0"}
193571,{"reportingGroups":[{"absoluteAccumulated":{"bidirVol":777438189,"expiryDate":{"volume":"08-12-2013T02:00:52"},"previousExpiryDate":{"time":"06-10-2013T18:23:34","volume":"08-11-2013T02:00:17"},"resetPeriod":{"volume":"monthly day 8 02:00"}},"selected":"yes","subsGName":"Go28_8","subscriptionDate":"06-10-2013T18:23:34","validityTime":2329299},{"absoluteAccumulated":{"bidirVol":1078862229,"previousExpiryDate":{"time":"01-11-2013T18:19:00","volume":"01-11-2013T18:19:00"}},"restartInfo":"01-11-2013T18:19","selected":"no","subsGName":"1GB7D0800","subscriptionDate":"01-11-2013T18:19:00","validityTime":0}],"version":"2.0"}
811537,{"reportingGroups":[{"absoluteAccumulated":{"bidirVol":204352282,"expiryDate":{"volume":"23-11-2013T02:00:39"},"previousExpiryDate":{"time":"02-10-2013T09:45:22","volume":"23-10-2013T02:00:06"},"resetPeriod":{"volume":"monthly day 23 02:00"}},"selected":"yes","subsGName":"Matrix_2GB_16","subscriptionDate":"02-10-2013T09:45:22","validityTime":995066}],"version":"2.0"}
772024,{"reportingGroups":[{"absoluteAccumulated":{"bidirVol":32687329,"expiryDate":{"volume":"23-11-2013T02:00:15"},"previousExpiryDate":{"time":"02-10-2013T16:42:02","volume":"23-10-2013T02:00:57"},"resetPeriod":{"volume":"monthly day 23 02:00"}},"selected":"yes","subsGName":"Matrix_5GB_16","subscriptionDate":"02-10-2013T16:42:02","validityTime":1241216}],"version":"2.0"}
732724,{"reportingGroups":[{"absoluteAccumulated":{"bidirVol":109926510,"expiryDate":{"volume":"23-11-2013T02:00:03"},"previousExpiryDate":{"time":"02-10-2013T03:05:19","volume":"23-10-2013T02:00:49"},"resetPeriod":{"volume":"monthly day 23 02:00"}},"selected":"no","subsGName":"Matrix_5GB_16","subscriptionDate":"02-10-2013T03:05:19","validityTime":994852}],"version":"2.0"}
983366,{"reportingGroups":[{"absoluteAccumulated":{"bidirVol":86051166,"expiryDate":{"volume":"23-10-2013T02:00:25"},"previousExpiryDate":{"time":"02-10-2013T03:07:52","volume":"02-10-2013T03:07:52"},"resetPeriod":{"volume":"monthly day 23 02:00"}},"selected":"yes","subsGName":"Matrix_5GB_16","subscriptionDate":"02-10-2013T03:07:52","validityTime":223910}],"version":"2.0"}

I want the results will be like this:

Code:
number,bidirVol1,subsGName1,bidirVol2,subsGName2
744906,Matrix_1GB_16,1442755910,102098178,1GB7D0800
678874,Matrix_5GB_16,1318102561,20004753,1GB7D0800
193571,Go28_8,777438189,1078862229,1GB7D0800
811537,Matrix_2GB_16,204352282
772024,Matrix_5GB_16,32687329
732724,Matrix_5GB_16,109926510
983366,Matrix_5GB_16,86051166

Anybody can help ?
Thanks

Last edited by justbow; 11-11-2013 at 04:02 AM.. Reason: word wrong
# 2  
Old 11-11-2013
The data you say you want in the results doesn't match the headings in the 2nd and 3rd fields of your output. If the output you really want is:
Code:
number,bidirVol1,subsGName1,bidirVol2,subsGName2
744906,1442755910,Matrix_1GB_16,102098178,1GB7D0800
678874,1318102561,Matrix_5GB_16,20004753,1GB7D0800
193571,777438189,Go28_8,1078862229,1GB7D0800
811537,204352282,Matrix_2GB_16
772024,32687329,Matrix_5GB_16
732724,109926510,Matrix_5GB_16
983366,86051166,Matrix_5GB_16

(which has bidirVol values from your sample data under the bidirVol1 header and subsGName values from your sample data under the subsGName1 header) the following seems to work:
Code:
awk -F '[^_[:alnum:]]*' '
BEGIN { printf("number,bidirVol1,subsGName1,bidirVol2,subsGName2\n") }
{       printf("%s", $1)
        for(i = 2; i < NF; i++)
                if($i == "bidirVol" || $i == "subsGName")
                        printf(",%s", $(i+1))
        printf("\n")
}' logsx.txt

If you want to run this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 11-11-2013
Code:
[root@client ~] sed -n ' s/[":[{,}]/ /pg'  logsx.txt > finallogsx.txt ; awk \
'BEGIN {OFS="," ;print "number,bidirVol1,subsGName1,bidirVol2,subsGName2"}{print $1, $30, \
$5,$39,$55} ' finallogsx.txt 

number,bidirVol1,subsGName1,bidirVol2,subsGName2
744906,Matrix_1GB_16,1442755910,102098178,1GB7D0800
678874,Matrix_5GB_16,1318102561,20004753,1GB7D0800
193571,Go28_8,777438189,1078862229,1GB7D0800
811537,Matrix_2GB_16,204352282,2.0,
772024,Matrix_5GB_16,32687329,2.0,
732724,Matrix_5GB_16,109926510,2.0,
983366,Matrix_5GB_16,86051166,2.0,


Last edited by Franklin52; 11-11-2013 at 05:55 AM.. Reason: Please use code tags
# 4  
Old 11-11-2013
Thank you all. But i have try the scripts but unexpected. Sorry for make confusing.

The completed logs :
Code:
cat logsx.txt
744906,{"reportingGroups":[{"absoluteAccumulated":{"bidirVol":1442755910,"expiryDate":{"volume":"23-11-2013T02:00:22"},"previousExpiryDate":{"time":"02-10-2013T06:14:21","volume":"23-10-2013T02:00:10"},"resetPeriod":{"volume":"monthly day 23 02:00"}},"selected":"no","subsGName":"Matrix_1GB_16","subscriptionDate":"02-10-2013T06:14:21","validityTime":1178681},{"absoluteAccumulated":{"bidirVol":102098178,"previousExpiryDate":{"time":"09-11-2013T10:53:00","volume":"09-11-2013T10:53:00"}},"restartInfo":"09-11-2013T10:53","selected":"yes","subsGName":"1GB7D0800","subscriptionDate":"09-11-2013T10:53:00","validityTime":0}],"version":"2.0"}
678874,{"reportingGroups":[{"absoluteAccumulated":{"bidirVol":1318102561,"expiryDate":{"volume":"23-11-2013T02:00:20"},"previousExpiryDate":{"time":"02-10-2013T03:12:17","volume":"23-10-2013T02:00:02"},"resetPeriod":{"volume":"monthly day 23 02:00"}},"selected":"no","subsGName":"Matrix_5GB_16","subscriptionDate":"02-10-2013T03:12:17","validityTime":1051965},{"absoluteAccumulated":{"bidirVol":20004753,"previousExpiryDate":{"time":"10-11-2013T21:54:00","volume":"10-11-2013T21:54:00"}},"restartInfo":"10-11-2013T21:54","selected":"yes","subsGName":"1GB7D0800","subscriptionDate":"10-11-2013T21:54:00","validityTime":0}],"version":"2.0"}
193571,{"reportingGroups":[{"absoluteAccumulated":{"bidirVol":777438189,"expiryDate":{"volume":"08-12-2013T02:00:52"},"previousExpiryDate":{"time":"06-10-2013T18:23:34","volume":"08-11-2013T02:00:17"},"resetPeriod":{"volume":"monthly day 8 02:00"}},"selected":"yes","subsGName":"Go28_8","subscriptionDate":"06-10-2013T18:23:34","validityTime":2329299},{"absoluteAccumulated":{"bidirVol":1078862229,"previousExpiryDate":{"time":"01-11-2013T18:19:00","volume":"01-11-2013T18:19:00"}},"restartInfo":"01-11-2013T18:19","selected":"no","subsGName":"1GB7D0800","subscriptionDate":"01-11-2013T18:19:00","validityTime":0}],"version":"2.0"}
811537,{"reportingGroups":[{"absoluteAccumulated":{"bidirVol":204352282,"expiryDate":{"volume":"23-11-2013T02:00:39"},"previousExpiryDate":{"time":"02-10-2013T09:45:22","volume":"23-10-2013T02:00:06"},"resetPeriod":{"volume":"monthly day 23 02:00"}},"selected":"yes","subsGName":"Matrix_2GB_16","subscriptionDate":"02-10-2013T09:45:22","validityTime":995066}],"version":"2.0"}
772024,{"reportingGroups":[{"absoluteAccumulated":{"bidirVol":32687329,"expiryDate":{"volume":"23-11-2013T02:00:15"},"previousExpiryDate":{"time":"02-10-2013T16:42:02","volume":"23-10-2013T02:00:57"},"resetPeriod":{"volume":"monthly day 23 02:00"}},"selected":"yes","subsGName":"Matrix_5GB_16","subscriptionDate":"02-10-2013T16:42:02","validityTime":1241216}],"version":"2.0"}
732724,{"reportingGroups":[{"absoluteAccumulated":{"bidirVol":109926510,"expiryDate":{"volume":"23-11-2013T02:00:03"},"previousExpiryDate":{"time":"02-10-2013T03:05:19","volume":"23-10-2013T02:00:49"},"resetPeriod":{"volume":"monthly day 23 02:00"}},"selected":"no","subsGName":"Matrix_5GB_16","subscriptionDate":"02-10-2013T03:05:19","validityTime":994852}],"version":"2.0"}
983366,{"reportingGroups":[{"absoluteAccumulated":{"bidirVol":86051166,"expiryDate":{"volume":"23-10-2013T02:00:25"},"previousExpiryDate":{"time":"02-10-2013T03:07:52","volume":"02-10-2013T03:07:52"},"resetPeriod":{"volume":"monthly day 23 02:00"}},"selected":"yes","subsGName":"Matrix_5GB_16","subscriptionDate":"02-10-2013T03:07:52","validityTime":223910}],"version":"2.0"}
710053,{"reportingGroups":[{"absoluteAccumulated":{"bidirVol":0,"dlVolume":0,"expiryDate":{"volume":"08-12-2013T02:00:43"},"previousExpiryDate":{"time":"03-10-2013T14:52:13","volume":"08-11-2013T02:00:43"},"resetPeriod":{"volume":"monthly day 8 02:00"},"ulVolume":0},"selected":"yes","subsGName":"Matrix_5GB_8","subscriptionDate":"03-10-2013T14:52:13","validityTime":2290547}],"version":"2.0"}
789319,{"reportingGroups":[{"absoluteAccumulated":{"bidirVol":1073743863,"previousExpiryDate":{"time":"31-10-2013T19:17:00","volume":"31-10-2013T19:17:00"}},"restartInfo":"31-10-2013T19:17","selected":"no","subsGName":"1GB7DAY","subscriptionDate":"31-10-2013T19:17:00","validityTime":0}],"version":"2.0"}


There are some line has 2 "bidirVol" and 2 "subsGName" and some line got 1 "bidirVol" and 1 "subsGName". Actually the idea just want to print after string "bidirVol" and "subsGName".

So the results will be like this :

Code:
number,bidirVol1,subsGName1,bidirVol2,subsGName2,
744906,1442755910,Matrix_1GB_16,102098178,1GB7D0800
678874,1318102561,Matrix_5GB_16,20004753,1GB7D0800
193571,777438189,Go28_8,1078862229,1GB7D0800
811537,204352282,Matrix_2GB_16
772024,32687329,Matrix_5GB_16
732724,109926510,Matrix_5GB_16
983366,86051166,Matrix_5GB_16
710053,0,Matrix_5GB_8
789319,1073743863,1GB7DAY

# 5  
Old 11-11-2013
Quote:
Originally Posted by justbow
Thank you all. But i have try the scripts but unexpected. Sorry for make confusing.

The completed logs :
Code:
     Deleted for brevity in this response

There are some line has 2 "bidirVol" and 2 "subsGName" and some line got 1 "bidirVol" and 1 "subsGName". Actually the idea just want to print after string "bidirVol" and "subsGName".

So the results will be like this :

Code:
number,bidirVol1,subsGName1,bidirVol2,subsGName2,
744906,1442755910,Matrix_1GB_16,102098178,1GB7D0800
678874,1318102561,Matrix_5GB_16,20004753,1GB7D0800
193571,777438189,Go28_8,1078862229,1GB7D0800
811537,204352282,Matrix_2GB_16
772024,32687329,Matrix_5GB_16
732724,109926510,Matrix_5GB_16
983366,86051166,Matrix_5GB_16
710053,0,Matrix_5GB_8
789319,1073743863,1GB7DAY

With your modified input, the difference between the output produced by the awk script I provided and the output you specified above is that my script omits the comma at the end of the 1st line of output (marked in red above). Your earlier requirements did not include that comma (and I don't' see a need for it now).

If that isn't the output you got from my script with this input:
  1. what output did you get. and
  2. what operating system are you using (output from uname -a)?
# 6  
Old 11-11-2013
Quote:
Originally Posted by Don Cragun
With your modified input, the difference between the output produced by the awk script I provided and the output you specified above is that my script omits the comma at the end of the 1st line of output (marked in red above). Your earlier requirements did not include that comma (and I don't' see a need for it now).

If that isn't the output you got from my script with this input:
  1. what output did you get. and
  2. what operating system are you using (output from uname -a)?
[*] It's work now. I think because of the results i write manually. I can manage to correct it.[*] I use Linux machine.

Thanks Don Smilie
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 print the next word from the same line based on grep string condtion match.

I need to fetch particular string from log file based on grep condition match. Actual requirement is need to print the next word from the same line based on grep string condtion match. File :Java.lanag.xyz......File copied completed : abc.txt Ouput :abc.txt I have used below... (5 Replies)
Discussion started by: siva83
5 Replies

2. Shell Programming and Scripting

How to match the first word and print only that line in UNIX?

Below is the file DISK-A 109063.2 49 31 40.79 DISK-B 110058.5 49 44 57.07 DISK-c 4402.4 2 1 2.14 from the file, i want to search for 'DISK-A' and print only that line with the first word matching to DISK-A and the output should skip DISK-A. Desired Output: (If i'm... (2 Replies)
Discussion started by: web2moha
2 Replies

3. Shell Programming and Scripting

Print a word after a match

Hi all, I have the below line: 08 03 * * 1-5 XXXXXXXXXXXXX -ENVI LDNFOUAT10 -EXE xxxxxxxx -CONFIG \${xxxxx} -SUBCLASS RESET -START -EXTRAAPPARGS \" -env 38LDNFOUAT10 \" >> /tmp/SRRC_xxxxxxx_start.log.`/usr/bin/date +\%Y\%m\%d` 2>&1 I want to print just one word after the string "-env", in... (7 Replies)
Discussion started by: Cvg
7 Replies

4. Shell Programming and Scripting

How to print everything after a string match

Hi all, I'm trying to do some work on the authorized_keys file to do a check if there's any information after the hash key.. At the end of the hash key's in the file, there can be an = or == Is there a way to check if anything exists after these equals and if so print it out or else print... (2 Replies)
Discussion started by: Jazmania
2 Replies

5. Shell Programming and Scripting

find a word and print n lines before and after the match

how to find a word and print n lines before and after the match until a blank line is encounterd (14 Replies)
Discussion started by: chidori
14 Replies

6. Shell Programming and Scripting

print word after pattern match in two instances

i have a file like below. how can i printout the digits followed by the pattern -bwout and -bwin. say i run the script by entering line number 145 (the fourth line), then the o/p should be like 5000000 1024000 8 test1 -ipprot erp -ppsout 500 -ppsin 500 -bwout 300000 -bwin 300000 -statsdevice... (7 Replies)
Discussion started by: sb245
7 Replies

7. Shell Programming and Scripting

exact string match ; search and print match

I am trying to match a pattern exactly in a shell script. I have tried two methods awk '/\<mpath${CURR_MP}\>/{print $1 $2}' multipath perl -ne '/\bmpath${CURR_MP}\b/ and print' /var/tmp/multipath Both these methods require that I use the escape character. I am guessing that is why... (8 Replies)
Discussion started by: bash_in_my_head
8 Replies

8. Shell Programming and Scripting

Print all the words after a match word

Hi, I want to print all words till the last word after the match of "ERROR" word. For e.g. I'll get an sqlplus error with e.g. 1 $ ./calltest_fn.ksh var test_var:=test_fn1; calltest_fn.ksh file1 file2 file3 ERROR at line 4: ORA-06550: line 4, column 11: PLS-00201: identifier... (5 Replies)
Discussion started by: dips_ag
5 Replies

9. Shell Programming and Scripting

Search word in a line and print earlier pattern match

Hi All, I have almost 1000+ files and I want to search specific pattern. Looking forwarded your input. Search for: word1.word2 (Which procedure contain this word, I need procedure name in output. Expected output: procedure test1 procedure test2 procedure test3 procedure test4 ... (7 Replies)
Discussion started by: susau_79
7 Replies

10. UNIX for Dummies Questions & Answers

exact string match in a word

Hi all, Is anyone able to help with the following query? I have an input file with several lines of words, e.g. "hellolaylahello" "hellohellohellolayla" I want to search for the exact string "hello" in each line and display: 2 "hellolaylahello" 3 "hellohellohellolayla" I... (11 Replies)
Discussion started by: dr_sabz
11 Replies
Login or Register to Ask a Question