LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash script and sed (https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-and-sed-868327/)

Snoken99 03-13-2011 01:59 PM

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

druuna 03-13-2011 02:02 PM

Hi,

Use this as sed statement:
Code:

sed -rn '/Data|Start/p' $y > newfile
Hope this helps.

Nylex 03-13-2011 02:04 PM

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.

Snoken99 03-13-2011 03:00 PM

Thank you druuna. It works. So simple.

druuna 03-13-2011 03:21 PM

You're welcome :)

grail 03-13-2011 07:12 PM

Great you have a solution. Please mark as SOLVED :)

Nylex 03-14-2011 02:13 AM

My solution also works.

MTK358 03-14-2011 10:05 AM

Quote:

Originally Posted by Nylex (Post 4289392)
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.


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