![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Rules & FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
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? |
| Forum Sponsor | ||
|
|
|
|||
|
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. |
|
||||
|
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
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 - - - - - - - - - - - - - - - - - - - - - - - - - - |
|
|||
|
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> 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:
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
|
|||
| Google UNIX.COM |