The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 04:31 PM
Grep for Multiple patterns WillImm123 Shell Programming and Scripting 7 03-01-2006 04:23 PM
Grep multiple patterns malaymaru Shell Programming and Scripting 4 09-25-2005 01:20 AM
grep for multiple patterns tselvanin UNIX for Dummies Questions & Answers 1 11-12-2003 07:43 PM
How to parameterize multiple search patterns and generate a new file augustinep UNIX for Dummies Questions & Answers 6 07-30-2003 08:50 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-26-2007
Vijay06 Vijay06 is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 16
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?
  #2 (permalink)  
Old 11-26-2007
Cameron's Avatar
Cameron Cameron is offline Forum Advisor  
Registered User
  
 

Join Date: Nov 2001
Location: Brisbane, Australia
Posts: 500
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
  #3 (permalink)  
Old 11-26-2007
Vijay06 Vijay06 is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 16
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.
  #4 (permalink)  
Old 11-26-2007
Cameron's Avatar
Cameron Cameron is offline Forum Advisor  
Registered User
  
 

Join Date: Nov 2001
Location: Brisbane, Australia
Posts: 500
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

- - - - - - - - - - - - - - - - - - - - - - - - - -
  #5 (permalink)  
Old 11-26-2007
Vijay06 Vijay06 is offline
Registered User
  
 

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

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

Will post more details further!!!
  #6 (permalink)  
Old 11-26-2007
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,082
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:36 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0