How to cut multiple patterns from a file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to cut multiple patterns from a file?
# 1  
Old 11-26-2007
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  
Old 11-26-2007
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  
Old 11-26-2007
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  
Old 11-26-2007
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  
Old 11-26-2007
Thanks a lot!!!

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

Will post more details further!!!
# 6  
Old 11-26-2007
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep multiple patterns(file) and replace whole line

I am able to grep multiple patterns which stored in a files. However, how could we replace the whole line with either the pattern or new string? For example: pattern_file: *Info in the () is not part of the pattern file. They are the intended name to replace the whole line after the pattern... (5 Replies)
Discussion started by: wxboo
5 Replies

2. Shell Programming and Scripting

Grep from multiple patterns multiple file multiple output

Hi, I want to grep multiple patterns from multiple files and save to multiple outputs. As of now its outputting all to the same file when I use this command. Input : 108 files to check for 390 patterns to check for. output I need to 108 files with the searched patterns. Xargs -I {} grep... (3 Replies)
Discussion started by: Diya123
3 Replies

3. Shell Programming and Scripting

Find common patterns in multiple file

Hi, I need help to find patterns that are common or matched in a specified column in multiple files. File1.txt ID1 555 ID23 8857 ID4 4454 ID05 555 File2.txt ID74 4454 ID96 555 ID322 4454 (4 Replies)
Discussion started by: redse171
4 Replies

4. Shell Programming and Scripting

How to search multiple patterns and remove lines from a file?

Hi, I have a file content as below. Table : PAYR Displayed fields: 15 of 15 Fixed columns: 4 List width 0999... (4 Replies)
Discussion started by: shirdi
4 Replies

5. Shell Programming and Scripting

Awk to Count Multiple patterns in a huge file

Hi, I have a file that is 430K lines long. It has records like below |site1|MAP |site2|MAP |site1|MODAL |site2|MAP |site2|MODAL |site2|LINK |site1|LINK My task is to count the number of time MAP, MODAL, LINK occurs for a single site and write new records like below to a new file ... (5 Replies)
Discussion started by: reach.sree@gmai
5 Replies

6. Shell Programming and Scripting

Match multiple patterns in a file and then print their respective next line

Dear all, I need to search multiple patterns and then I need to print their respective next lines. For an example, in the below table, I will look for 3 different patterns : 1) # ATC_Codes: 2) # Generic_Name: 3) # Drug_Target_1_Gene_Name: #BEGIN_DRUGCARD DB00001 # AHFS_Codes:... (3 Replies)
Discussion started by: AshwaniSharma09
3 Replies

7. Shell Programming and Scripting

Searching for multiple patterns in a file

Hi All, I have a file in which i have to search for a pattern from the beginning of the file and if the pattern is found , then i have to perform a reverse search from that line to the beginning of the file to get the first occurrence of another pattern. sample input file hey what are you... (8 Replies)
Discussion started by: Kesavan
8 Replies

8. Shell Programming and Scripting

gzgrep for multiple patterns or with pattern file

Hi, I'm struggling to find an option for gzgrep to grep compressed files for multiple patterns preferanly using a pattern file (so pattern file can be altered seperately to rest of script). My regex patterns are say: 4\{15\} 3\{13\} /usr/xpg4/bin/grep will accept -f option but... (4 Replies)
Discussion started by: andyatit
4 Replies

9. Shell Programming and Scripting

Perl - How to search a text file with multiple patterns?

Good day, great gurus, I'm new to Perl, and programming in general. I'm trying to retrieve a column of data from my text file which spans a non-specific number of lines. So I did a regexp that will pick out the columns. However,my pattern would vary. I tried using a foreach loop unsuccessfully.... (2 Replies)
Discussion started by: Sp3ck
2 Replies

10. UNIX for Dummies Questions & Answers

How to parameterize multiple search patterns and generate a new file

I have one file: 123*100*abcd*10 123*101*abcd*-29*def 123*100*abcd*-10 123*102*abcd*-105*asd I would like to parameterize the search patterns in the following way so that the user could dynamically change the search pattern. *100* and *- (ie *minus) *102* and *- The output that is... (6 Replies)
Discussion started by: augustinep
6 Replies
Login or Register to Ask a Question