UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 3rd, 2004, 05:42 PM
sauldls sauldls is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 2 sauldls User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
sed regexp question for deleting lines

I have a file that is being piped through sed and want to remove lines the contain the pattern ABC if the pattern falls within the 1st 20 characters of the line. If the line does not contain the pattern or if the pattern falls after the 20th character, the line goes to standardout.

The statement I've tried to use is:

cat file1 | sed '/ABC/d' | "another UNIX utility"

I'm open to using grep instead of sed, if anybody can help with the regexp to get it to just search the 1st 20 characters for the pattern.

grep -v ABC file1 | "another UNIX utility"

Thanks for any suggestions,
Don

Reply With Quote
  #2  
Old November 4th, 2004, 09:13 AM
andyb1ack andyb1ack is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 60 andyb1ack User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 m 14 sec
Reputation Power: 5
I don't know how to do it in sed. Until someone answers you here's an awk method:

My file:
Code:
                                 
line 01 andy
line 02 black
line 03 andy
line 04 black
line 05 andy
line 06 black
line 07 andy
line 08 black
line 09 andy 
line 10 black
line 11 andy
line 12 black
line 13 andy 
line 14 black
line 15 andy
line 16 black
line 17 andy
line 18 black
line 19 andy
line 20 black


Code:
nawk -v LINETO=5 -v STRING=andy '{if(NR<=LINETO && index($0,STRING) >0)
                                            {print $0" : this line matched"}
                                          else
                                            {print $0" : this line didnt match"}
                                         }' myfile
line 01 andy : this line matched
line 02 black : this line didnt match
line 03 andy : this line matched
line 04 black : this line didnt match
line 05 andy : this line matched
line 06 black : this line didnt match
line 07 andy : this line didnt match
line 08 black : this line didnt match
line 09 andy  : this line didnt match
line 10 black : this line didnt match
line 11 andy : this line didnt match
line 12 black : this line didnt match
line 13 andy  : this line didnt match
line 14 black : this line didnt match
line 15 andy : this line didnt match
line 16 black : this line didnt match
line 17 andy : this line didnt match
line 18 black : this line didnt match
line 19 andy : this line didnt match
line 20 black : this line didnt match

Reply With Quote
  #3  
Old November 4th, 2004, 12:46 PM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,098 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 5 Days 8 h 44 m 33 sec
Reputation Power: 9
sauldls please note: cat|sed is for dos-experts
sed ... filename does it.
sure sed in able to do it, my *nix is down at the moment,
spontaneous i would say something like:
sed -n '/.*\{20\}ABC/p' filename
i repeat NOT tested, man sed can help you

Reply With Quote
  #4  
Old November 4th, 2004, 01:53 PM
sauldls sauldls is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 2 sauldls User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks guggach! That gets me closer. In the regexp you suggested: '/.*\{20\}ABC/p', it works in cases where the string falls after the 20th character BUT I also need to print lines in which the string done not appear at all. It is only the lines that contain the string in the first 20 characters that I need to remove, all others need to be printed.

xxxxABCxxxxxxxxxxxxxxxxxxxxxxx (not printed)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (printed)
xxxxxxxxxxxxxxxxxxxxxxxxxABCxx (printed)
xABCxxxxxxxxxxxxxxxxxxxxxxxxxx (not printed)

Thanks,
Don

Reply With Quote
  #5  
Old November 5th, 2004, 06:21 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,098 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 5 Days 8 h 44 m 33 sec
Reputation Power: 9
sauldls
sure sed (and other tools) can easy do that
i am actually on a stupid dos sys and have no access to
my solaris sys (disk probls) so i cannot ...
maybe y Q is not clean, try (just for fun)
sed -n '/ABC/p' filename
or clearify the Q
if you only need the string 'ABC' try
sed -n 's/\(.*\)\(ABC\)\(.*\)/\2/'

Last edited by guggach : November 5th, 2004 at 06:24 AM. Reason: changes

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > sed regexp question for deleting lines


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT