LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 01-26-2013, 07:10 AM   #1
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Rep: Reputation: 16
awk or perl print feilds after regex.


Is there a way in awk or perl to just print everything including and after the 'sh'
This text file had the ip and host names edited out. The filed are not all uniform - I can't use the stardard awk print out $1 $4 and $5 fields, because the information in each field is not consistent.


Code:
Jan 26 03:00:03  corectld: client casperhost:36121: exec: sh -c "ping -c 3 172.18.47.1"
Jan 26 03:00:20  corectld: client casperhost:45957: exec: sh -c ifconfig
Jan 26 03:00:20  corectld: client casperhost:46804: exec: sh -c "ethtool eth1"
Jan 26 03:00:33  ntpd[541]: kernel time sync status change 6001
Jan 26 03:17:36  ntpd[541]: kernel time sync status change 2001
Jan 26 03:51:45  ntpd[541]: kernel time sync status change 6001
Jan 26 04:00:05  corectld: client casperhost:53123: exec: sh -c "cat /proc/cpuinfo"
Jan 26 04:00:05  corectld: client casperhost:53176: exec: sh -c "dmidecode -t system"
Jan 26 04:00:07  corectld: client casperhost:53321: exec: sh -c "dmidecode -t bios"
Jan 26 04:00:18  corectld: client casperhost:58729: exec: sh -c uptime
Jan 26 04:00:19  corectld: client casperhost:58748: exec: sh -c "cat /proc/meminfo"
roc/sys/vm/min_free_kbytes" ectld: client casperhost:33485: exec: sh -c "cat /p--More-- (24% of 5110 bytes)
Jan 26 04:00:24  corectld: client casperhost:34644: exec: sh -c "uname -a"
Jan 26 04:00:26  corectld: client casperhost:35100: exec: sh -c "cat /proc/cmdline"
Jan 26 04:00:27  corectld: client casperhost:35154: exec: sh -c "ls /usr/java/"

Last edited by casperdaghost; 01-26-2013 at 07:11 AM.
 
Old 01-26-2013, 07:29 AM   #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
Are you looking for something like this:
Code:
awk -F" sh" '/sh/{ print $2 }' infile
 -c "ping -c 3 172.18.47.1"
 -c ifconfig
 -c "ethtool eth1"
 -c "cat /proc/cpuinfo"
 -c "dmidecode -t system"
 -c "dmidecode -t bios"
 -c uptime
 -c "cat /proc/meminfo"
 -c "cat /p--More-- (24% of 5110 bytes)
 -c "uname -a"
 -c "cat /proc/cmdline"
 -c "ls /usr/java/"
And although not perl/awk, here's also a sed solution:
Code:
sed -n 's%.*exec: sh%%p' infile 
 -c "ping -c 3 172.18.47.1"
 -c ifconfig
 -c "ethtool eth1"
 -c "cat /proc/cpuinfo"
 -c "dmidecode -t system"
 -c "dmidecode -t bios"
 -c uptime
 -c "cat /proc/meminfo"
 -c "cat /p--More-- (24% of 5110 bytes)
 -c "uname -a"
 -c "cat /proc/cmdline"
 -c "ls /usr/java/"

Last edited by druuna; 01-26-2013 at 07:38 AM. Reason: fixed minor mistake
 
Old 01-26-2013, 07:45 AM   #3
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
Quote:
Originally Posted by casperdaghost View Post
Is there a way in awk or perl to just print everything including and after the 'sh'
I might have misinterpreted that in my previous reply.

Easily fixed:
Code:
awk -F" sh" '/sh/{ print "sh"$2 }' infile 
sh -c "ping -c 3 172.18.47.1"
sh -c ifconfig
sh -c "ethtool eth1"
sh -c "cat /proc/cpuinfo"
sh -c "dmidecode -t system"
sh -c "dmidecode -t bios"
sh -c uptime
sh -c "cat /proc/meminfo"
sh -c "cat /p--More-- (24% of 5110 bytes)
sh -c "uname -a"
sh -c "cat /proc/cmdline"
sh -c "ls /usr/java/"

sed -n 's%.*exec: sh%sh%p' infile 
sh -c "ping -c 3 172.18.47.1"
sh -c ifconfig
sh -c "ethtool eth1"
sh -c "cat /proc/cpuinfo"
sh -c "dmidecode -t system"
sh -c "dmidecode -t bios"
sh -c uptime
sh -c "cat /proc/meminfo"
sh -c "cat /p--More-- (24% of 5110 bytes)
sh -c "uname -a"
sh -c "cat /proc/cmdline"
sh -c "ls /usr/java/"
Have your pick
 
Old 01-26-2013, 01:02 PM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
In perl (untested)
Code:
#! /usr/bin/perl -w
use strict;
    while(<>){
        if( $_ =~ m/sh\s+/ ){
            print $&.$';
        }
    }
---- rod.

Last edited by theNbomr; 01-26-2013 at 01:08 PM.
 
Old 01-27-2013, 02:59 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Code:
grep -o 'sh -c.*$' infile
Assuming that the start of the pattern is always "sh -c", of course.
 
  


Reply



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
perl print regex string casperdaghost Programming 6 06-17-2012 11:40 PM
[SOLVED] differences between shell regex and php regex and perl regex and javascript and mysql golden_boy615 Linux - General 2 04-19-2011 01:10 AM
Perl to find regex and print following 5 lines after regex casperdaghost Linux - Newbie 3 08-29-2010 08:08 PM
awk regex with variable bertl1982 Linux - General 2 03-17-2010 08:38 AM
awk regex question Guest1234 Programming 6 12-25-2007 01:31 PM

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

All times are GMT -5. The time now is 01:22 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