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 and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
grep regular expression Calypso Shell Programming and Scripting 7 05-18-2009 08:08 AM
grep with regular expression daikeyang Shell Programming and Scripting 6 03-22-2009 08:46 PM
How can we grep only those content according to regular expression Johnivy UNIX for Dummies Questions & Answers 3 12-16-2008 08:43 AM
regarding grep regular expression ukatru UNIX for Advanced & Expert Users 2 10-23-2008 12:47 AM
grep : regular expression RishiPahuja Shell Programming and Scripting 10 09-13-2005 01:53 AM

Reply
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 10-01-2009
terry2009 terry2009 is offline
Registered User
  
 

Join Date: Sep 2009
Posts: 11
Regular expression in grep -E | awk print

Hi All,

I have file.txt with contents like this:

random text
To: recipient@email.co.uk
<HTML>S7randomtext more random text
random text
To: recip@smtpemail.com
<HTML>E5randomtext more random text
random text
I need the output to look like this:

1,,,1,S7
1,,,1,E5

My code so far is:

Code:
 
grep -E "[A-Z][0-9]" file.txt | awk '{print ",1,,,1,"[A-Z][0-9]}' > out.txt

but I get this error: awk: line 1: syntax error at or near [

How can I get the Regular Expression [A-Z][0-9] into the print of awk??? Please help asap

Last edited by terry2009; 10-02-2009 at 05:41 AM..
  #2 (permalink)  
Old 10-01-2009
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131

Code:
echo '<HTML>S7randomtext' | sed 's#.*>\([A-Z][0-9]\).*#1,,,1,\1#'

  #3 (permalink)  
Old 10-01-2009
terry2009 terry2009 is offline
Registered User
  
 

Join Date: Sep 2009
Posts: 11
Thanks, but would this work (this is a snippet of a much much larger script):


Code:
grep -E "[A-Z][0-9]" file.txt | sed 's#.*>\([A-Z][0-9]\).*#1,,,1,\1#' > out.txt

  #4 (permalink)  
Old 10-01-2009
EAGL€ EAGL€ is offline
Registered User
  
 

Join Date: Aug 2009
Location: istanbul
Posts: 46
Quote:
Originally Posted by terry2009 View Post
Thanks, but would this work (this is a snippet of a much much larger script):


Code:
grep -E "[A-Z][0-9]" file.txt | sed 's#.*>\([A-Z][0-9]\).*#1,,,1,\1#' > out.txt
Hello friends, could you please tell me what does " > " stands for in SED part of the script? I understand all the regex except it,

regards
  #5 (permalink)  
Old 10-01-2009
scottn scottn is offline Forum Advisor  
VIP Member
  
 

Join Date: Jun 2009
Location: Zürich, CH
Posts: 1,133
Quote:
Originally Posted by EAGL€ View Post
Hello friends, could you please tell me what does " > " stands for in SED part of the script? I understand all the regex except it,

regards
It's a literal > (meaning match a greater-than sign, or in this context an angled bracket)
  #6 (permalink)  
Old 10-01-2009
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
Quote:
Originally Posted by EAGL€ View Post
Hello friends, could you please tell me what does " > " stands for in SED part of the script? I understand all the regex except it,

regards
It's not part of an RE. Look at the original pattern:

Code:
echo '<HTML>S7randomtext' | sed 's#.*>\([A-Z][0-9]\).*#1,,,1,\1#'

  #7 (permalink)  
Old 10-02-2009
terry2009 terry2009 is offline
Registered User
  
 

Join Date: Sep 2009
Posts: 11
Hi Again!

Here is an extended part of the script... I'd like to be sure that sed will work instead of grep....

Code:
grep -l "search string in files I want" smtp*.tmp > email_filenames_list.txt ;
cat email_filenames_list.txt | xargs cat >> smtp_all_emails_logs.txt ;
sed '/[A-Z][0-9]/ s#.*>\([A-Z][0-9]\).*#,1,,,1,\1#' smtp_all_emails_logs.log > out.txt ;
grep "^To" smtp_all_emails_logs.txt | awk '{print ","$2}' > recipients.txt ;
paste out.txt recipients.txt

the grep line definately works, but would sed run through a massive list of emails that have all been put in one file (smtp_all_email_logs.txt) and print:
S7,1,,,1,recipient@email.co.uk
R2,1,,,1,recip@smtpemail.com
etc in AIX environment

Another quick question: how can I stop a carrage return/line feed being included after the recipients email address? Cause it's screwing up my spreadsheet.
Reply

Bookmarks

Tags
awk, expression, grep, print, regular

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 04:20 PM.


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