LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-16-2010, 03:38 PM   #1
bmorency
LQ Newbie
 
Registered: Oct 2005
Posts: 15

Rep: Reputation: 1
sed expression help


Hi,

I am new to using sed and I am trying to parse a file. I have a text file with customer information and I need to remove certain lines that do not have certain information. There are 23 lines for each customer record.

So I am trying to keep only the lines that has the customer account number and name (they are both on same line) and another line that has account type.

The lines are as follows:

1234 CustomerName
Account Type: Type1

The information starts at the beginning of each line with other lines between them.

When I try to parse each line one at a time my commands work but when I try to combine them I get a blank output file. I also have another issue where another line has a date (ie: 4/15/09) that will not be deleted because it starts with a number. I have tried to insert {3,4} after the square bracket to say that there should be 3-4 numbers but that did not help either. The file will be blank.

My commands look like this.
Code:
sed '/^[0-9]/!d' < inputfile.txt > outputfile.txt
sed '/^Account/!d' < inputfile.txt > outputfile.txt
I tried to combine them like this but get a blank file.
Code:
sed '/^[0-9]|Account/!d' < inputfile.txt > outputfile.txt

Does anyone know what I might be doing wrong?

Thanks for any help.
 
Old 03-16-2010, 03:44 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 741Reputation: 741Reputation: 741Reputation: 741Reputation: 741Reputation: 741Reputation: 741
The alternation operator (|) requires that you use extended regular expression. In SED, do this by adding the -r flag. (Or, escape the "|")
 
1 members found this post helpful.
Old 03-16-2010, 03:55 PM   #3
bmorency
LQ Newbie
 
Registered: Oct 2005
Posts: 15

Original Poster
Rep: Reputation: 1
Thank you very much. I put the escape character in there and it is working good.
 
Old 03-16-2010, 05:58 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 19,253

Rep: Reputation: 3395Reputation: 3395Reputation: 3395Reputation: 3395Reputation: 3395Reputation: 3395Reputation: 3395Reputation: 3395Reputation: 3395Reputation: 3395Reputation: 3395
As suggested, use "-r" - that will allow the {3,4} to work, and you won't need the escape.
Also put the carat (^) back in Account or you may get extraneous records.
 
1 members found this post helpful.
Old 03-17-2010, 08:24 AM   #5
bmorency
LQ Newbie
 
Registered: Oct 2005
Posts: 15

Original Poster
Rep: Reputation: 1
Thanks. That fixed the other issue. I will use "-r" instead of just the escape character.

Last edited by bmorency; 03-17-2010 at 08:27 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Embed command output in sed expression. AwesomeMachine Linux - Newbie 7 02-20-2010 03:48 PM
sed regular expression match everything up to a certain character bhepdogg Programming 3 05-28-2009 03:59 PM
sed - regular expression Vilmerok Programming 5 02-26-2009 09:44 AM
sed regular expression Ammad Linux - General 7 10-29-2008 06:52 PM
sed regular expression help needed Dew Linux - Newbie 1 03-30-2005 03:59 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration