How to extract exact strings in shell scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to extract exact strings in shell scripting
# 1  
Old 10-07-2010
How to extract strings from full path when full path is not fixed

Code:
/Path/snowbird9/nrfCompMgrRave1230100920.log.gz:09/20/2010 06:14:51 ERROR [UNKNOWN] Error Message.
/Path/snowbird6/nrfCompMgrRave1220100920.log.gz:09/20/2010 06:14:51 ERROR [UNKNOWN] Error Message.
/Path/snowbird14/nrfCompMgrRave920100920.log.gz:09/20/2010 06:14:51 ERROR [UNKNOWN] Error Message.
/Path/snowbird15/nrfraveComp10_SocketServerLog20100920.log.gz:09/20/2010 06:14:51 INFO [UNKNOWN] INFO Message.
/Path/thunderball6/nrfraveComp10_SocketServerLog20100920.log.gz:09/20/2010 06:14:51 INFO [UNKNOWN] INFO Message.
/Path/thunderball9/nrfraveComp13_SocketServerLog20100920.log.gz:09/20/2010 06:14:51 INFO [UNKNOWN] INFO Message.
/Path/thunderball15/nrfraveComp14_SocketServerLog20100920.log.gz:09/20/2010 06:14:51 INFO [UNKNOWN] INFO Message.
/Path/thunderball14/nrfraveComp22_SocketServerLog20100920.log.gz:09/20/2010 06:14:51 INFO [UNKNOWN] INFO Message.
/Path/thunderball10/nrfraveComp23_SocketServerLog20100920.log.gz:09/20/2010 06:14:51 INFO [UNKNOWN] INFO Message.
/Path/thunderball11/nrfraveComp09_SocketServerLog20100920.log.gz:09/20/2010 06:14:51 INFO [UNKNOWN] INFO Message.
/Path/thunderball13/nrfraveComp01_SocketServerLog20100920.log.gz:09/20/2010 06:14:51 INFO [UNKNOWN] INFO Message.
/Path/nrfraveComp01_SocketServerLog20100920.log.gz:09/20/2010 06:14:51 INFO [UNKNOWN] INFO Message.
/Path/nrfCompMgrRave920100920.log.gz:09/20/2010 06:14:51 ERROR [UNKNOWN] Error Message.

Note: Error Message contains Errors. INFO message contains information messages. so those are not fixed strings.


Here as $1 is having the full path...as my script takes parameter as input path. It will consists of three patterns
Code:
/Path/thunderball*/nrfraveComp10_SocketServerLog20100920.log.gz:09/20/2010
/Path/snowbird*/nrfCompMgrRave1230100920.log.gz:09/20/2010 06:14:51 ERROR [UNKNOWN] Error Message.
/Path/nrfCompMgrRave920100920.log.gz:09/20/2010 06:14:51 ERROR [UNKNOWN] Error Message.

Currently I have checked for the below command but problem here is in my input path i have /nrf/snowbird* or /nrf/thunderball* so i am making that as blank when i find /nrf in path. but it doesnot provide flexibility for the input path parameter that i am passing to script.
Code:
cat errors.txt | awk -F':|] ' '/ERROR|INFO/{sub(".*/nrf/","",$1);OFS=",";sub("/",",",$1);a[$1 OFS $NF]++}END{for(i in a)print i,a[i]}' | sort -k1,3 > report.csv

Above command displays required output for me like
Code:
ServerName,Filename,Error String,Count of String
snowbird9,nrfCompMgrRave1230100920.log.gz,Error Message.,1
thunderball6,nrfraveComp10_SocketServerLog20100920.log.gz,INFO Message.,1

But it is having limitation that it will always look for fixed pattern in $1(i.e.,/nrf). So I need to check in $1 if thunderball* or snowbird* is exists then print servername in report and if it is not exits then column for Servername should be blank.

does anybody have any idea how to implement it...

Thanks in advance !

Last edited by Shirisha; 10-07-2010 at 05:04 AM.. Reason: Please use code tags, thank you!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How can I extract digits at the end of a string in UNIX shell scripting?

How can I extract digits at the end of a string in UNIX shell scripting or perl? cat file.txt abc_d123_4567.txt A246_B789.txt B123cc099.txt a123_B234-012.txt a13.txt What can I do here? Many thanks. cat file.txt | sed "s/.txt$//" | ........ 4567 789 099 012 13 (11 Replies)
Discussion started by: mingch
11 Replies

2. Shell Programming and Scripting

Extract between two Exact matched strings.

more data.txt i need this exacted from data.txt This is the command i tried sed -n "/Start_of_DISK_info:\/u/,/End_of_DISK_info:\/u/p" data.txtBut, unfortunately it does not do an exact match. Instead, it prints text between both these strings /u & /u/tmp like below. i need this... (6 Replies)
Discussion started by: mohtashims
6 Replies

3. UNIX for Dummies Questions & Answers

Issue when using egrep to extract strings (too many strings)

Dear all, I have a data like below (n of rows=400,000) and I want to extract the rows with certain strings. I use code below. It works if there is not too many strings for example n of strings <5000. while I have 90,000 strings to extract. If I use the egrep code below, I will get error: ... (3 Replies)
Discussion started by: forevertl
3 Replies

4. UNIX for Dummies Questions & Answers

Extract directory name from the full directory path in UNIX using shell scripting

My input is as below : /splunk/scrubbed/rebate/IFIND.REBTE.WROC.txt /splunk/scrubbed/rebate/IFIND.REBTE.WROC.txt /splunk/scrubbed/loyal/IFIND.HELLO.WROC.txt /splunk/scrubbed/triumph/ifind.triumph.txt From the above input I want to extract the file names only . Basically I want to... (5 Replies)
Discussion started by: IshuGupta
5 Replies

5. Shell Programming and Scripting

How to extract data from XML file using shell scripting?

Hi , I have input file as XML. following are input data #complex.xml Code: <?xml version="1.0" encoding="UTF-8"?><TEST_doc xmlns="http://www.w3.org/2001/XMLSchema-instance"> <ENTRY uid="123456"> <protein> <name>PROT001</name> <organism>Human</organism> ... (1 Reply)
Discussion started by: arun_kohan
1 Replies

6. Shell Programming and Scripting

How to extract data from xml file using shell scripting?

Hi evry1, This is my 1st post in this forum.Pls help me I want to extract some data froma xml file which has 2000 lines using shell scripting. Actually my xml file has some "audio and video codes" which i need to arrange in a column wise format after extracting it using shell scripting.I... (4 Replies)
Discussion started by: arun_kohan
4 Replies

7. Shell Programming and Scripting

Shell scripting to extract data from file

Hi, i want to fetch the data from the alert log file, for a particular time interval. Example : Alert log content : Thu Mar 18 08:47:36 2010 Completed: alter database open Thu Mar 18 19:13:38 2010 MMNL absent for 6390 secs; Foregrounds taking over Fri Mar 19 08:30:52 2010... (1 Reply)
Discussion started by: Pinki018
1 Replies

8. Shell Programming and Scripting

Comparison of two files which contains strings using Shell scripting or PERL

Hi, I need sample code to compare the two files line by line which contains text strings and to print the difference in the third file. Thanks in advance (1 Reply)
Discussion started by: sudhakaryadav
1 Replies

9. Shell Programming and Scripting

Grepping for Exact Strings

ok, apparently this is a very difficult question to answer based on my searches on google that came up fruitless. what i want to do is grep through a file for words that match a specified string. but the thing is, i keep getting all words in the file that have the string in them. say for... (27 Replies)
Discussion started by: SkySmart
27 Replies

10. Shell Programming and Scripting

comparison of strings in unix shell scripting

Hi STORAGE_TYPE=$1 echo "########### SQL SESSION STARTED ###########" VALUE=`sqlplus -S /nolog << THEEND connect tcupro/tcupro_dev@bdd1optn SET SERVEROUTPUT ON DECLARE V_STORAGE_TYPE varchar2(3); V_ERR_MSG varchar2(255) ; V_LOG_LEVEL varchar2(200); BEGIN... (1 Reply)
Discussion started by: piscean_n
1 Replies
Login or Register to Ask a Question