LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-13-2011, 01:59 PM   #1
Snoken99
LQ Newbie
 
Registered: Mar 2011
Posts: 2

Rep: Reputation: 0
Thumbs up bash script and sed


Hi,

I'm trying to write a bash script to find all lines containing two different strings in many files. I don't have access to egrep so I want to use sed for this purpose.
The files will look like this:
FileX
------
Info:18
Data:76
Contact:me@home.com
Start:1500

I want to generate a new file from these files with only the rows containing Data and Start. Something like this:

for y in `ls /file*.db`;
do sed '/Data|Start/p' $y > newfile
done

I'v tried to find a solution to this but not found it. Anyone have a suggestion for this?

Thanks,
Goran
 
Old 03-13-2011, 02:02 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Use this as sed statement:
Code:
sed -rn '/Data|Start/p' $y > newfile
Hope this helps.
 
1 members found this post helpful.
Old 03-13-2011, 02:04 PM   #3
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Are you using the GNU version of sed? If so, you want "\|", rather than "|" as per the manual. Also, you probably want to use the -n option, to stop it printing out the entire pattern space.
 
Old 03-13-2011, 03:00 PM   #4
Snoken99
LQ Newbie
 
Registered: Mar 2011
Posts: 2

Original Poster
Rep: Reputation: 0
Thank you druuna. It works. So simple.
 
Old 03-13-2011, 03:21 PM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
You're welcome
 
Old 03-13-2011, 07:12 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Great you have a solution. Please mark as SOLVED
 
Old 03-14-2011, 02:13 AM   #7
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
My solution also works.
 
1 members found this post helpful.
Old 03-14-2011, 10:05 AM   #8
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by Nylex View Post
Are you using the GNU version of sed? If so, you want "\|", rather than "|" as per the manual. Also, you probably want to use the -n option, to stop it printing out the entire pattern space.
The -r option tells sed to use extended regex, which supports operators such as "|", "+", and "()" without having to use backslashes.
 
  


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
[SOLVED] Help with sed script in bash corp769 Programming 5 02-26-2011 03:27 PM
error when using sed in a bash script raprat Programming 3 11-09-2009 11:21 PM
Problem with SED in BASH Script stlouis Programming 2 01-22-2008 02:21 PM
problem with sed in a bash script nexus55 Linux - Software 6 05-03-2004 09:40 PM
sed in small BASH script OhLordy Linux - General 1 08-29-2003 11:32 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:11 AM.

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