The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
AWK: Multiple patterns per line Plavixo UNIX for Dummies Questions & Answers 1 05-05-2008 01:31 PM
Grep for Multiple patterns WillImm123 Shell Programming and Scripting 7 03-01-2006 12:23 PM
Grep multiple patterns malaymaru Shell Programming and Scripting 4 09-24-2005 10:20 PM
grep for multiple patterns tselvanin UNIX for Dummies Questions & Answers 1 11-12-2003 03:43 PM
How to parameterize multiple search patterns and generate a new file augustinep UNIX for Dummies Questions & Answers 6 07-30-2003 05:50 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-26-2007
Registered User
 

Join Date: Nov 2006
Posts: 12
How to cut multiple patterns from a file?

Hi,

I need to cut values after searching for similar patterns in a file. For example, I have the following pattern in a file:

####<Nov12 2007> <user: Vijay> <user id:123456 college:anna univ> <error code: runtime exception>


I need the values for
date:
User:
User id:
College:
Error code:

Can somebody help me to obtain the same?
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 11-26-2007
Cameron's Avatar
Registered User
 

Join Date: Nov 2001
Location: Brisbane, Australia
Posts: 486
Vijay, show us what you have already in terms of your script.
This shows everyone that you've tried to apply some form of method.
Otherwise, you've already referred to one method.
Do a 'man cut' from the command line.

Cheers,
Cameron
Reply With Quote
  #3 (permalink)  
Old 11-26-2007
Registered User
 

Join Date: Nov 2006
Posts: 12
Those are Logs from an application

Thanks Cameron.

Actually, these are logs that are triggered from an application. I have given just a snippeto fo logs.
I have all the logs in the similar fashion.
Could you please help me to get the result?


Thanks,
Vijay.
Reply With Quote
  #4 (permalink)  
Old 11-26-2007
Cameron's Avatar
Registered User
 

Join Date: Nov 2001
Location: Brisbane, Australia
Posts: 486
Vijay,

A crude solution is outlined below - Note - I highly suspect that this is not the most efficient way to perform this task.

ALSO!! Please, in future provide the forum with detail of your query and a sample of your code to show how far you have troubled. You'll likely get more qualified responses in return.

Contents of 'thefile.txt':
####<Nov12 2007> <user: Vijay> <user id:123456 college:anna univ> <error code: runtime exception>
####<Nov13 2008> <user: Cameron> <user id:789012 college:bond univ> <error code: tux runtime exception>


Script:
Code:
#!/bin/ksh

while read inline
do
  echo ${inline}
  detail=`echo ${inline} | cut -f 1 -d ">" | cut -f 2 -d "<"`
  user=`echo ${inline} | cut -f 4 -d " " | cut -f 1 -d ">"`
  uid=`echo ${inline} | cut -f 3 -d ">" | cut -f 2 -d ":" | cut -f 1 -d " "`
  uni=`echo ${inline} | cut -f 3 -d ">" | cut -f 3 -d ":"`
  err=`echo ${inline} | cut -f 4 -d ">" | cut -f 2 -d ":" | cut -f 2- -d " "`

  echo '- - - - - - - - - - - - - - - - - - - - - - - - - -'
  echo ''
  echo ' Detail:      '${detail}
  echo ' User:        '${user}
  echo ' User ID:     '${uid}
  echo ' University:  '${uni}
  echo ' Error:       '${err}
  echo ''
  echo '- - - - - - - - - - - - - - - - - - - - - - - - - -'
  echo ''

done < /home/cameron/thefile.txt
Output:
Code:
####<Nov12 2007> <user: Vijay> <user id:123456 college:anna univ> <error code: runtime exception>
- - - - - - - - - - - - - - - - - - - - - - - - - -

 Detail:      Nov12 2007
 User:        Vijay
 User ID:     123456
 University:  anna univ
 Error:       runtime exception

- - - - - - - - - - - - - - - - - - - - - - - - - -

####<Nov13 2008> <user: Cameron> <user id:789012 college:bond univ> <error code: tux runtime exception>
- - - - - - - - - - - - - - - - - - - - - - - - - -

 Detail:      Nov13 2008
 User:        Cameron
 User ID:     789012
 University:  bond univ
 Error:       tux runtime exception

- - - - - - - - - - - - - - - - - - - - - - - - - -
Reply With Quote
  #5 (permalink)  
Old 11-26-2007
Registered User
 

Join Date: Nov 2006
Posts: 12
Thanks a lot!!!

That was an awesome reply Cameron!!
Thanks a lot for the help...

Will post more details further!!!
Reply With Quote
  #6 (permalink)  
Old 11-26-2007
Registered User
 

Join Date: Jun 2007
Posts: 355
awk

hi,

try this one.

in:
Code:
####<Nov11 2005> <user: leo> <user id:210375 college:traffic> <error code: compile exception>
####<Nov12 2006> <user: tony> <user id:210386 college:industry> <error code: runtime exception>
####<Nov13 2007> <user: jade> <user id:200124 college:oversea> <error code: testing exception>
out:
Code:
date:Nov11 2005
User:leo
User id:210375
College:traffic
Error code:compile exception
date:Nov12 2006
User:tony
User id:210386
College:industry
Error code:runtime exception
date:Nov13 2007
User:jade
User id:200124
College:oversea
Error code:testing exception
code:
Code:
nawk 'BEGIN{
FS="[<>: ]"
format="date:%s %s\nUser:%s\nUser id:%s\nCollege:%s\nError code:%s %s\n"
}
{
printf(format,$2,$3,$8,$13,$15,$21,$22)
}' filename
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 02:01 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0