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
extract the lines prvnrk Shell Programming and Scripting 2 05-25-2008 03:17 AM
How to convert C source from 8bit chars to 16bit chars? siegfried Shell Programming and Scripting 0 09-26-2007 11:26 AM
How to extract a sequence of n lines from a file 0ktalmagik Shell Programming and Scripting 4 06-29-2006 08:24 PM
Extract known lines nhatch UNIX for Dummies Questions & Answers 2 09-16-2004 08:59 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-09-2008
Registered User
 

Join Date: Jun 2008
Posts: 3
sed to extract first two uppercase chars in targeted lines

Hello,

I have a file temp.txt:
-------------------------
HELLO WORLD
This is a temp file.
TENCHARSHEre
no beginning UPPERCHARS
HI There
-------------------------

What is a sed egrep command that will target lines that begin with 3-10 uppercase chars, and output the first 2 chars?
desired result:
---
HE
TE
---

So far I have been able to extract the targeted lines in egrep using the expression: egrep ^[A-Z]\{3,10\} < temp.txt . . but no luck in sed.

Thank you
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 06-09-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milan, Italy/Varna, Bulgaria
Posts: 1,432
Code:
sed -n 's/^\([A-Z][A-Z]\)[A-Z].*/\1/p' temp.txt
Reply With Quote
  #3 (permalink)  
Old 06-09-2008
Registered User
 

Join Date: Jun 2008
Posts: 3
Thank you for the reply..
However I have to do the following using egrep.

I tried translating what you gave using -r:
sed -r 's/^([A-Z][A-Z])[A-Z].*/\1/g' temp.txt

Updated temp.txt:
-------------------------
HELLO WORLD
This is a temp file.
TENCHARSHEre
no beginning UPPERCHARS
HI There
ELEVENCHARActers
-------------------------
desired output:
HE
TE

actual output:
HE
This is a temp file.
TE
no beginning UPPERCHARS
HI There
EL

1) How do I ignore irrelevant targets? (i.e. "This is a temp file.", etc..)
2) It also includes 11 upperchars as the target. How do I restrict it to 10 uppercase chars only?

Sed and regex are very difficult concepts for me to grasp, so I appreciate the patience and time. Thank you
Reply With Quote
  #4 (permalink)  
Old 06-09-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milan, Italy/Varna, Bulgaria
Posts: 1,432
Code:
sed -nr '/^[A-Z]{11}/!{s/^([A-Z]{2})[A-Z].*/\1/p;}' temp.txt
Or:

Code:
sed -nr 's/^([A-Z]{2})[A-Z]{1,8}[^A-Z].*/\1/p' temp.txt

Last edited by radoulov; 06-09-2008 at 12:30 PM.
Reply With Quote
  #5 (permalink)  
Old 06-09-2008
Registered User
 

Join Date: Jun 2008
Posts: 3
Thank you radoulov

The second method is a little more intutitive . . . Is there a way to execute the expression without the -n option?

Thanks again
Reply With Quote
  #6 (permalink)  
Old 06-10-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milan, Italy/Varna, Bulgaria
Posts: 1,432
Quote:
Originally Posted by str8danked View Post
Thank you radoulov

The second method is a little more intutitive . . . Is there a way to execute the expression without the -n option?
Why you need to remove the -n option?
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 10:32 PM.


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