RE: OT: Linux Shell Script

From: Tyson Scott <tscott_at_ipexpert.com>
Date: Wed, 28 Apr 2010 08:55:55 -0400

Dan,

 

Thanks for sharing. With this large of a group there always has to be
someone that knows the bash shell better than myself. That is exactly what
I was looking for. cat isn't necessary in this example. The whole task can
be accomplished with sed.

 

sed -e '/3/{

N

/3\n4/d

}

' -i file.txt

 

This way I can edit the file inline. Thanks again.

 

Regards,

 

Tyson Scott - CCIE #13513 R&S, Security, and SP

Technical Instructor - IPexpert, Inc.

Mailto: <mailto:tscott_at_ipexpert.com> tscott_at_ipexpert.com

Telephone: +1.810.326.1444, ext. 208

Live Assistance, Please visit: <http://www.ipexpert.com/chat>
www.ipexpert.com/chat

eFax: +1.810.454.0130

 

IPexpert is a premier provider of Self-Study Workbooks, Video on Demand,
Audio Tools, Online Hardware Rental and Classroom Training for the Cisco
CCIE (R&S, Voice, Security & Service Provider) certification(s) with
training locations throughout the United States, Europe, South Asia and
Australia. Be sure to visit our online communities at
<http://www.ipexpert.com/communities> www.ipexpert.com/communities and our
public website at <http://www.ipexpert.com/> www.ipexpert.com

 

From: Dan Shechter [mailto:danshtr_at_gmail.com]
Sent: Wednesday, April 28, 2010 1:20 AM
To: Tyson Scott
Cc: ccielab_at_groupstudy.com
Subject: Re: OT: Linux Shell Script

 

Hi Tyson,

Try this:

dans_at_dans-desktop:~/tmp/tyson$ cat a.txt | sed -e '/3/{
N
/3\n4/d
}
'

HTH,

Dan #13685 (RS/Sec/SP)

Troubleshooting blog: <http://dans-net.com/> http://dans-net.com

 

On Wed, Apr 28, 2010 at 7:19 AM, Tyson Scott <tscott_at_ipexpert.com> wrote:

Hey guys,

Ignore this if you are not familiar with Linux. I am throwing this out
because I expected it to work and can't get it working.

I am trying to create a bash script to delete two lines in a file only if
they are directly connected to one another.

For example if I had a file "file.txt" with the following lines

1

2

3

4

5

And I wanted to delete the lines with 3 & 4 but only if they were together.
i.e. I want to make sure I am not deleting if they are:

1

3

2

4

5

I know I could do

sed '/3/d' -i file.txt;sed '/4/d' -i file.txt

but that doesn't make sure they are directly together

I tried

sed '/3$\r\n4/d' -i file.txt

But it seems either it doesn't match or I am using the incorrect regular
expression

I know I could also do

cat file.txt | grep -v 3 | grep -v 4 > file.txt

but still not making sure it is one line. Any thoughts would be
appreciated. I believe there should be a way to do this with sed but I
wouldn't call myself a guru on the great intricacies of sed. If not then I
will just use expect, but I wanted to keep it simplistic.

I know with expect I can do

expect -re "\n3\r\n4" { exec sed '/3/d' -i file.txt;sed '/4/d' -i file.txt }

And that verifies the two lines are together and then I can delete the files
based off of them being together but I was hoping to stick with a simple
bash shell script for this task.

Regards,

Tyson Scott - CCIE #13513 R&S, Security, and SP

Technical Instructor - IPexpert, Inc.

Mailto: tscott_at_ipexpert.com

Telephone: +1.810.326.1444, ext. 208

Live Assistance, Please visit: www.ipexpert.com/chat

eFax: +1.810.454.0130

IPexpert is a premier provider of Self-Study Workbooks, Video on Demand,
Audio Tools, Online Hardware Rental and Classroom Training for the Cisco
CCIE (R&S, Voice, Security & Service Provider) certification(s) with
training locations throughout the United States, Europe, South Asia and
Australia. Be sure to visit our online communities at
www.ipexpert.com/communities and our public website at www.ipexpert.com
<http://www.ipexpert.com/>

Blogs and organic groups at http://www.ccie.net
Received on Wed Apr 28 2010 - 08:55:55 ART

This archive was generated by hypermail 2.2.0 : Sat May 01 2010 - 09:49:57 ART