LinuxQuestions.org
Help answer threads with 0 replies.
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 02-22-2012, 10:17 AM   #1
niharikaananth
Member
 
Registered: Aug 2011
Posts: 58

Rep: Reputation: Disabled
sed/awk to print every before line that pattern match


Hi..All,
I want to print below colored words using awk or sed command.
Code:
#cat mdstat
RAID status
Personalities : [raid1] 
md0 : active raid1 sdb1[1] sda1[0]
      1020032 blocks [2/2] [__]
      
md2 : active raid1 sdb3[1] sda3[0]
      20482752 blocks [2/2] [UU]
      
md3 : active raid1 sdb5[1] sda5[0]
      537117056 blocks [2/2] [UF]
      
md4 : active raid1 sdb6[1] sda6[0]
      315741376 blocks [2/2] [UU]
      
md5 : active raid1 sdd1[1] sdc1[0]
      976759936 blocks [2/2] [_U]
      
md1 : active raid1 sdb2[1] sda2[0]
      102398208 blocks [2/2] [U_]
      
unused devices: <none>
I don't want to print those device for which the status as [UU]. So I wanted to print only failed raid arrays. For that I can search the status which are not as [UU] using below command.
Code:
#sed -n '/F\|_/p' mdstat
      1020032 blocks [2/2] [__]
      537117056 blocks [2/2] [UF]
      976759936 blocks [2/2] [_U]
      102398208 blocks [2/2] [U_]
But I am struggling in printing those lines before this pattern. Your kind help would be appreciated if you help me to print only those device(md*) which are in red color.

Last edited by niharikaananth; 02-22-2012 at 10:19 AM.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 02-22-2012, 10:23 AM   #2
uhelp
Member
 
Registered: Nov 2011
Location: Germany, Bavaria, Nueremberg area
Distribution: openSUSE, Debian, LFS
Posts: 205

Rep: Reputation: 43
no need for sed.

Code:
grep -E -e '\[UU\]' mdstat
-E makes sure that Extended regexes will be used
-e this is a regex following here. many can be given

' ensure that bash will definitely nothing do to our regex
\[ we are searching for [ but this is an meta character which begins a class of chars so we have to escape it
UU literally
\] same like above

Last edited by uhelp; 02-22-2012 at 10:27 AM. Reason: added explanation
 
Old 02-22-2012, 10:38 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

Not sure about the previous answer, but maybe I'm reading the question wrong.....

If all you want are the red words in your example:
Code:
grep -B1 "\[.*[_F].*\]" mdstat | awk '/^md[0-9][0-9]*/ { print $1 }'
The grep part looks for lines that contain 1 or more times _ or F between square brackets ("\[.*[_F].*\]"). When triggered it will print the current and previous line (-B1).

Awk then prints the md part from the previous line (and nothing else).

Hope this helps.
 
2 members found this post helpful.
Old 02-22-2012, 10:54 AM   #4
uhelp
Member
 
Registered: Nov 2011
Location: Germany, Bavaria, Nueremberg area
Distribution: openSUSE, Debian, LFS
Posts: 205

Rep: Reputation: 43
Quote:
Originally Posted by druuna View Post
Hi,

Not sure about the previous answer, but maybe I'm reading the question wrong.....
This is right.
I should read more carefuly.

Last edited by uhelp; 02-22-2012 at 10:56 AM.
 
1 members found this post helpful.
Old 02-22-2012, 10:59 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,577

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Probably over complicated, but its late:
Code:
awk '/^md/{md=$1;getline}md && !/\[UU\]$/{print md}{md=""}' mdstat
 
2 members found this post helpful.
Old 02-22-2012, 11:03 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Quote:
Originally Posted by grail View Post
Probably over complicated, but its late:
Code:
awk '/^md/{md=$1;getline}md && !/\[UU\]$/{print md}{md=""}' mdstat
Not pretty, but one command and no pipes. Probably faster then my solution!
 
Old 02-22-2012, 11:17 AM   #7
uhelp
Member
 
Registered: Nov 2011
Location: Germany, Bavaria, Nueremberg area
Distribution: openSUSE, Debian, LFS
Posts: 205

Rep: Reputation: 43
grep -B1 -E -e '\[[^U|[:digit:]]|[U][^U]\]' mdstat | grep -E '^md'

allbeit slower than grail, I think.

Last edited by uhelp; 02-22-2012 at 11:19 AM.
 
1 members found this post helpful.
Old 02-22-2012, 11:59 AM   #8
niharikaananth
Member
 
Registered: Aug 2011
Posts: 58

Original Poster
Rep: Reputation: Disabled
Hi..Thank you all,
Excellent, You saved me a lot of time.
Code:
$grep -B1 "\[.*[_F].*\]" mdstat | awk '/^md[0-9][0-9]*/ { print $1 }'
md0
md3
md5
md1
$awk '/^md/{md=$1;getline}md && !/\[UU\]$/{print md}{md=""}' mdstat
md0
md3
md5
md1
$
Once again thank you very much for your kind and quick response. And very very thanks to linuxquestions.org
 
Old 02-22-2012, 12:02 PM   #9
niharikaananth
Member
 
Registered: Aug 2011
Posts: 58

Original Poster
Rep: Reputation: Disabled
Thank you very much uhelp,
Code:
$grep -B1 -E -e '\[[^U|[:digit:]]|[U][^U]\]' mdstat | grep -E '^md' | cut -d: -f 1
md0 
md3 
md5 
md1
This is what I had expected, Thanks.
 
Old 02-22-2012, 12:09 PM   #10
uhelp
Member
 
Registered: Nov 2011
Location: Germany, Bavaria, Nueremberg area
Distribution: openSUSE, Debian, LFS
Posts: 205

Rep: Reputation: 43
no need for piping again to "cut":
Code:
grep -B1 -E -e '\[[^U|[:digit:]]|[U][^U]\]' mdstat | grep -E -o '^md.\ '
 
1 members found this post helpful.
Old 02-22-2012, 10:47 PM   #11
niharikaananth
Member
 
Registered: Aug 2011
Posts: 58

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by uhelp View Post
no need for piping again to "cut":
Code:
grep -B1 -E -e '\[[^U|[:digit:]]|[U][^U]\]' mdstat | grep -E -o '^md.\ '
Excellent uhelp, Thank you very much, This is what I had excpected from Linux Guru.
 
  


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
[SOLVED] Awk - How to print match instead of whole line protocol Linux - General 8 10-24-2011 01:30 PM
replace a pattern with a line using sed/awk lokeshn05 Linux - Newbie 3 05-06-2009 03:01 PM
simple pattern match with awk, sed alenD Linux - Newbie 10 03-10-2008 02:31 PM
grep/sed/awk - find match, then match on next line gctaylor1 Programming 3 07-11-2007 08:55 AM
sed display line after pattern match inonzi_prowler Linux - Software 3 02-19-2007 01:47 PM


All times are GMT -5. The time now is 01:21 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration