LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 05-09-2008, 01:55 PM   #1
Echo Kilo
Member
 
Registered: Jul 2004
Distribution: Ubuntu - Debian Based
Posts: 242

Rep: Reputation: 30
Another simple scripting question


If I have a file with the output below, how do I strip it to another file that has only the parts I want?


The original file (with some lines manually deleted b/c it was longer):


Loading "priorities" plugin
Loading "installonlyn" plugin
Setting up Update Process
Setting up repositories
Reading repository metadata in from local files
Excluding Packages in global exclude list
Finished
331 packages excluded due to repository priority protections
Resolving Dependencies
--> Populating transaction set with selected packages. Please wait.
---> Package kpartx.x86_64 0:0.4.7-12.el5_1.3 set to be updated
---> Package gdb.x86_64 0:6.5-25.el5_1.1 set to be updated
---> Package net-snmp-libs.x86_64 1:5.3.1-19.el5_1.4 set to be updated
---> Package squid.x86_64 7:2.6.STABLE6-5.el5_1.3 set to be updated
---> Package sos.noarch 0:1.7-9.2.el5 set to be updated
--> Populating transaction set with selected packages. Please wait.
---> Package perl-TermReadKey.x86_64 0:2.30-3.el5.rf set to be updated
--> Running transaction check

Dependencies Resolved

=============================================================================
Package Arch Version Repository Size
=============================================================================
Installing:
kernel x86_64 2.6.18-53.1.19.el5 updates 15 M
kernel-devel x86_64 2.6.18-53.1.19.el5 updates 4.9 M
Updating:
autofs x86_64 1:5.0.1-0.rc2.55.el5.3 updates 835 k
cups x86_64 1:1.2.4-11.14.el5_1.6 updates 2.7 M
cups-libs x86_64 1:1.2.4-11.14.el5_1.6 updates 176 k
dbus x86_64 1.0.0-6.3.el5_1 updates 466 k
dbus-devel x86_64 1.0.0-6.3.el5_1 updates 1.3 M
Installing for dependencies:
perl-TermReadKey x86_64 2.30-3.el5.rf rpmforge 57 k

Transaction Summary
=============================================================================
Install 3 Package(s)
Update 23 Package(s)
Remove 0 Package(s)

Total download size: 50 M



However, here is what I want it to say (note that I'm taking the information after the word "Dependencies Resolved"):

File I want:

Host1 has the following updates available:


=============================================================================
Package Arch Version Repository Size
=============================================================================
Installing:
kernel x86_64 2.6.18-53.1.19.el5 updates 15 M
kernel-devel x86_64 2.6.18-53.1.19.el5 updates 4.9 M
Updating:
autofs x86_64 1:5.0.1-0.rc2.55.el5.3 updates 835 k
cups x86_64 1:1.2.4-11.14.el5_1.6 updates 2.7 M
cups-libs x86_64 1:1.2.4-11.14.el5_1.6 updates 176 k
dbus x86_64 1.0.0-6.3.el5_1 updates 466 k
dbus-devel x86_64 1.0.0-6.3.el5_1 updates 1.3 M
Installing for dependencies:
perl-TermReadKey x86_64 2.30-3.el5.rf rpmforge 57 k

Transaction Summary
=============================================================================
Install 3 Package(s)
Update 23 Package(s)
Remove 0 Package(s)

Total download size: 50 M
 
Old 05-09-2008, 03:29 PM   #2
rtspitz
Member
 
Registered: Jan 2005
Location: germany
Distribution: suse, opensuse, debian, others for testing
Posts: 307

Rep: Reputation: 32
- man tail
- man head
- man cut
- man sed
 
Old 05-09-2008, 03:46 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
sed is quite enough for that job.

Code:
sed '1 i\
host1 has following updates available
1,/Dependencies Resolved/d' echokilo
where echokilo was the snippet above.


Cheers,
Tink
 
Old 05-09-2008, 03:54 PM   #4
rtspitz
Member
 
Registered: Jan 2005
Location: germany
Distribution: suse, opensuse, debian, others for testing
Posts: 307

Rep: Reputation: 32
I was just giving an answer to his next question(s) :-)
 
Old 05-10-2008, 11:07 PM   #5
Echo Kilo
Member
 
Registered: Jul 2004
Distribution: Ubuntu - Debian Based
Posts: 242

Original Poster
Rep: Reputation: 30
Would I use that exact code in 3 lines?

Where would that go in this code:

#!/bin/bash
##
CNT=1
until [ $CNT -eq 10 ]
do IP=192.168.1.$CNT
REMHOST=`ssh root@$IP -C hostname`
echo "Host $REMHOST at IP $IP returned the following result:"
ssh root@$IP -C yum update | tee list_of_updates
CNT=`expr $CNT + 1`
done
 
Old 05-11-2008, 03:01 AM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by Echo Kilo View Post
Would I use that exact code in 3 lines?
No, you wouldn't. I used a file, echokilo; you'll be wanting to
pipe values through the sed.

Quote:
Originally Posted by Echo Kilo View Post
Where would that go in this code:

#!/bin/bash
##
CNT=1
until [ $CNT -eq 10 ]
do IP=192.168.1.$CNT
REMHOST=`ssh root@$IP -C hostname`
echo "Host $REMHOST at IP $IP returned the following result:"
ssh root@$IP -C yum update | tee list_of_updates
CNT=`expr $CNT + 1`
done
After the pipe ...



Cheers,
Tink
 
Old 05-11-2008, 09:17 PM   #7
Echo Kilo
Member
 
Registered: Jul 2004
Distribution: Ubuntu - Debian Based
Posts: 242

Original Poster
Rep: Reputation: 30
I tried this to no avail. Says broken pipe

#!/bin/bash
##
CNT=1
until [ $CNT -eq 10 ]
do IP=192.168.1.$CNT
REMHOST=`ssh root@$IP -C hostname`
echo "Host $REMHOST at IP $IP returned the following result:"
ssh root@$IP -C yum update | sed '1 i\
host1 has following updates available
1,/Dependencies Resolved/d' updatefile
CNT=`expr $CNT + 1`
done
 
Old 05-11-2008, 09:49 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by Echo Kilo View Post
I tried this to no avail. Says broken pipe

#!/bin/bash
##
CNT=1
until [ $CNT -eq 10 ]
do IP=192.168.1.$CNT
REMHOST=`ssh root@$IP -C hostname`
echo "Host $REMHOST at IP $IP returned the following result:"
ssh root@$IP -C yum update | sed '1 i\
host1 has following updates available
1,/Dependencies Resolved/d' updatefile
CNT=`expr $CNT + 1`
done
Wellll ... you don't have updatefile. Just get rid of
that. And you probably want to replace the ' with "
around your sed command, and the host1 with $REMHOST


Cheers,
Tink
 
Old 05-12-2008, 04:44 PM   #9
Echo Kilo
Member
 
Registered: Jul 2004
Distribution: Ubuntu - Debian Based
Posts: 242

Original Poster
Rep: Reputation: 30
I'm not sure what you mean about not having updatefile? I must be confused because it's in the script.

Here's where I am now (simplified the script a little)

#!/bin/bash
for i in $(seq 110 111)
do
ip=192.168.1.$i
host=$(ssh $ip -C hostname)
echo "Host $host at IP $ip returned the follow result"
ssh $ip "yum list updates" | sed '1 i\
1,/Dependencies Resolved/d' updatefile
done

however, updatefile is empty after running it?
 
Old 05-12-2008, 06:27 PM   #10
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Yes, and you SHOULDN'T :}

sed will try to READ that file, not create it. I'm surprised
that it actually exists in your environment.

Can you please clarify what you're trying to achieve?


Cheers,
Tink
 
Old 05-14-2008, 10:52 AM   #11
Echo Kilo
Member
 
Registered: Jul 2004
Distribution: Ubuntu - Debian Based
Posts: 242

Original Poster
Rep: Reputation: 30
I'm writing a script that will check for available updates on all the systems.

It turns out that "yum list updates" gives me what I want after I grep it. I was using "sudo yum update" which gave me all this information I didn't need.

Here's the final script:

#!/bin/bash

# loop that checks each system in iprange specified
for i in $(seq 100 250)
do
ip=192.168.1.$i
host=$(ssh $ip -C hostname 2>&1)
echo " "
echo "========================================================================="
echo "Host $host at IP $ip has the following updates available"
echo "========================================================================="
ssh $ip "yum list updates"
done > updates 2>&1

# manipulate file 'updates' here...
# removing unneeded lines with grep -v

grep -v Loading updates > 1
grep -v Setting 1 > 2
grep -v Reading 2 > 3
grep -v Excluding 3 > 4
grep -v Finished 4 > available_updates
 
  


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
YASQ Yet Another Simple Question regarding scripting christianunix Linux - Newbie 2 10-31-2007 11:29 AM
simple scripting question jerf Linux - Newbie 4 09-19-2007 04:12 AM
Simple Shell Scripting Question hellomynameisphil Programming 2 08-27-2005 04:41 AM
Bash Scripting--Simple Question mooreted Linux - General 4 05-10-2004 02:44 PM
simple scripting question wedgeworth Programming 6 02-04-2004 11:30 AM


All times are GMT -5. The time now is 08:00 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration