LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-23-2016, 11:47 AM   #1
oliveoyl
Member
 
Registered: Sep 2016
Posts: 42

Rep: Reputation: Disabled
egrep move search results all to one line


Output of command I'm trying to egrep on:

NodeName=node101 Arch=x86_64 CoresPerSocket=4
CPUAlloc=8 CPUErr=0 CPUTot=8 CPULoad=2.27 Features=core8,mem16gb,gig,harpertown
Gres=(null)
NodeAddr=10.0.1.10 NodeHostName=c0101 Version=15.08
OS=Linux RealMemory=16000 AllocMem=0 FreeMem=2792 Sockets=2 Boards=1
State=ALLOCATED ThreadsPerCore=1 TmpDisk=16000 Weight=1 Owner=N/A
BootTime=2016-05-12T13:52:35 SlurmdStartTime=2016-05-12T13:53:18
CapWatts=n/a
CurrentWatts=0 LowestJoules=0 ConsumedJoules=0
ExtSensorsJoules=n/s ExtSensorsWatts=0 ExtSensorsTemp=n/s


NodeName=node102 Arch=x86_64 CoresPerSocket=4
CPUAlloc=8 CPUErr=0 CPUTot=8 CPULoad=2.14 Features=core8,mem16gb,gig,harpertown
Gres=(null)
NodeAddr=10.0.1.11 NodeHostName=c0102 Version=15.08
OS=Linux RealMemory=16000 AllocMem=0 FreeMem=4779 Sockets=2 Boards=1
State=ALLOCATED ThreadsPerCore=1 TmpDisk=16000 Weight=1 Owner=N/A
BootTime=2016-05-12T13:53:00 SlurmdStartTime=2016-05-12T13:53:42
CapWatts=n/a
CurrentWatts=0 LowestJoules=0 ConsumedJoules=0
ExtSensorsJoules=n/s ExtSensorsWatts=0 ExtSensorsTemp=n/s
...

I found a nifty awk/sed command to get this:
# # mycommand | egrep 'NodeName|State' | sed 'N;s/\n/ /'
OR
# mycommand | egrep 'NodeName|State' | awk 'NR%2{printf "%s ",$0;next;}1'
NodeName=node101 Arch=x86_64 CoresPerSocket=4 State=ALLOCATED ThreadsPerCore=1 TmpDisk=16000 Weight=1 Owner=N/A
NodeName=node102 Arch=x86_64 CoresPerSocket=4 State=ALLOCATED ThreadsPerCore=1 TmpDisk=16000 Weight=1 Owner=N/A
...


What I want to get is just the NodeName and State. What is the proper way to go about getting this result?

NodeName=node101 State=ALLOCATED
NodeName=node102 State=ALLOCATED
 
Old 09-23-2016, 11:54 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,792

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
actually I used to suggest: do not use both grep and awk, because one of them should be sufficient to solve (such kind of issues).
In your case I would prefer awk, and I would try something like this:
Code:
awk -F'[ =]+' ' /NodeName/ { var=$1 }
                /State/ { print "var=" var "State=" $1 }'
It is not tested and obviously not a complete solution.
 
1 members found this post helpful.
Old 09-23-2016, 12:25 PM   #3
oliveoyl
Member
 
Registered: Sep 2016
Posts: 42

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
actually I used to suggest: do not use both grep and awk, because one of them should be sufficient to solve (such kind of issues).
In your case I would prefer awk, and I would try something like this:
Code:
awk -F'[ =]+' ' /NodeName/ { var=$1 }
                /State/ { print "var=" var "State=" $1 }'
It is not tested and obviously not a complete solution.
Can you please explain that field separtor value?
 
Old 09-23-2016, 12:30 PM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,792

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
that is a regexp, it can be any number of = and/or space.
That will make something like this:
Code:
CPUAlloc=8 CPUErr=0 CPUTot=8 CPULoad=2.14 Features=core8,mem16gb,gig,harpertown
   $1    $2  $3   $4  $5   $6  $7    $8     $9       $10
(just an example)
 
1 members found this post helpful.
Old 09-23-2016, 12:43 PM   #5
oliveoyl
Member
 
Registered: Sep 2016
Posts: 42

Original Poster
Rep: Reputation: Disabled
This worked:

awk -F'[ =]+' '/NodeName/ { var=$2 } /State/ {print "NodeName=" var " State=" $3}

Trying to understand it. The var is assigning the values from /NodeName/ $2 to it?

Somewhat related but what is the equivalent of egrep 'foo|bar' in awk?
 
Old 09-23-2016, 12:50 PM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,792

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
var is a variable, named var. var=$2 is an assignment, this will save the content of the second field in the variable named var (just remember the field separator).
/pattern/ will look for the line containing that pattern.

Code:
egrep 'foo|bar'
awk '/foo|bar/'
or did I misunderstand something?
 
1 members found this post helpful.
Old 09-23-2016, 01:55 PM   #7
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,294
Blog Entries: 3

Rep: Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719
Quote:
Originally Posted by oliveoyl View Post
Somewhat related but what is the equivalent of egrep 'foo|bar' in awk?
It would be:

Code:
awk '/foo|bar/'
which is short for

Code:
awk '/foo|bar/ { print }'
which is, in turn, short for

Code:
awk '/foo|bar/ { print $0 }'
"awk" is mostly just a bunch of if-then statements, with the ifs and thens left out. So in pseudo-code the above might be expressed as

Code:
if ( $0 =~ /foo|bar/ ) {
     print $0;
}
 
1 members found this post helpful.
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] egrep search patterns with whitespace in a list tech-support Linux - General 4 08-20-2016 10:00 PM
using egrep to search a file michaelgg13 Linux - Newbie 7 04-16-2014 11:12 AM
Search for two or more 'patterns' and move files according to results lithos Linux - Newbie 3 10-07-2011 11:04 AM
nice egrep 300 search words in one go? shalomajay LinuxQuestions.org Member Intro 2 03-01-2006 05:31 PM
nice egrep 300 search words in one go? shalomajay Programming 1 03-01-2006 03:21 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:19 PM.

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