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 08-14-2014, 09:06 AM   #1
tulsi
Member
 
Registered: Aug 2009
Location: Chennai
Posts: 54

Rep: Reputation: 15
get word from file


I have file with 1000 lines & its just output of multipath from different boxes. some of the multipath has faulty & above the multipath line has hostname there.

I want to get "faulty line & mpath with hostname from file" how do i get from file.

==> hostname <==
mpath0...
roud robin...
ready or faulty
 
Old 08-14-2014, 09:18 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,026

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
sorry, not clear on what you are trying to acheive. can you please respond with a small example of the input and the expected result you are after ?
 
Old 08-14-2014, 09:19 AM   #3
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,387

Rep: Reputation: 397Reputation: 397Reputation: 397Reputation: 397
hostname is 3 lines before faulty ?
if yes,
Code:
grep -B 3 faulty file.txt
 
1 members found this post helpful.
Old 08-15-2014, 03:43 AM   #4
tulsi
Member
 
Registered: Aug 2009
Location: Chennai
Posts: 54

Original Poster
Rep: Reputation: 15
File look like below


==> linuxbox1 <==
mpath0 (3600d0230003228bc000339414edb8101) [size=10 GB][features="0"][hwhandler="0"]
\_ round-robin 0 [prio=1][active]
\_ 2:0:0:6 sdb 8:16 [active][ready]
\_ round-robin 0 [prio=1][enabled]
\_ 3:0:0:6 sdc 8:64 [active][faulty]


==> linuxbox2 <==
mapth0 (1494554000000000000000000030000000300940111008000)
[size=197 MB][features="0"][hwhandler="0"]
\\_ round-robin 0 [active]
\\_ 5:0:0:1 sdk 8:160 [active][ready]
\\_ round-robin 0 [enabled]
\\_ 5:0:0:0 sdh 8:112 [active][ready]

mpath1 (3600d0230003228bc000339414edb8101) [size=10 GB][features="0"][hwhandler="0"]
\_ round-robin 0 [prio=1][active]
\_ 2:0:0:6 sdb 8:16 [active][ready]
\_ round-robin 0 [prio=1][enabled]
\_ 3:0:0:6 sdc 8:64 [active][ready]

==> linuxbox3 <==
mapth0 (1494554000000000000000000030000000300940111008000)
[size=197 MB][features="0"][hwhandler="0"]
\\_ round-robin 0 [active]
\\_ 5:0:0:1 sdk 8:160 [active][ready]
\\_ round-robin 0 [enabled]
\\_ 5:0:0:0 sdh 8:112 [active][faulty]

mpath1 (3600d0230003228bc000339414edb8101) [size=10 GB][features="0"][hwhandler="0"]
\_ round-robin 0 [prio=1][active]
\_ 2:0:0:6 sdb 8:16 [active][ready]
\_ round-robin 0 [prio=1][enabled]
\_ 3:0:0:6 sdc 8:64 [active][ready]

mpath2 (3600d0230003228bc000339414edb8101) [size=10 GB][features="0"][hwhandler="0"]
\_ round-robin 0 [prio=1][active]
\_ 2:0:0:6 sdb 8:16 [active][ready]
\_ round-robin 0 [prio=1][enabled]
\_ 3:0:0:6 sdc 8:64 [active][ready]


in that file i want hostname(linuxbox1),mpathname(mpath0) and faulty line.

Thanks for replies.
 
Old 08-15-2014, 05:36 AM   #5
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 804

Rep: Reputation: 186Reputation: 186
I'll tell you the logic and you can write the code.
0. Define two variables H and M
1. If a line starts with the phrase "==> linuxbox" it's the host name. Store the line into variable H. Clear variable M
2. If a line starts with the phrase "mpath" it's the mpathname. Store the line in variable M.
3. If the line ends with the phrase "[faulty]", write out variable H, M and the current line.

I'd use awk but that's just 1 option.

What did you try so far? Or do you seriously expect us to do your office work for you?

Also maintain a count of the lines outputted (may be zero) and on end, write it also to the output.

OK

OK

Last edited by AnanthaP; 08-15-2014 at 05:38 AM.
 
1 members found this post helpful.
Old 08-15-2014, 06:19 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
And what about the 'faulty' for linuxbox3?
 
Old 08-15-2014, 06:39 AM   #7
tulsi
Member
 
Registered: Aug 2009
Location: Chennai
Posts: 54

Original Poster
Rep: Reputation: 15
@grail, its failed multipath, we need to find a faulty path on the file.

@anathap thanks for the your input & once finished i'll post it.
 
Old 08-15-2014, 09:01 AM   #8
tulsi
Member
 
Registered: Aug 2009
Location: Chennai
Posts: 54

Original Poster
Rep: Reputation: 15
Hi,

This is my initial attempt to get line..


files=/home/tulsis/multipath.txt
hostname="==>*"
mvalue="mpath*"

if grep faulty $files ;
then
echo " $hostname , $temp ,$mvalue "
else
echo " no faulty in multipath"
fi

Above one is not working & I am working on correct one ..
give me suggestion to create correct one.
 
Old 08-15-2014, 09:35 AM   #9
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,026

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
Quote:
Originally Posted by tulsi View Post
File look like below
...
==> linuxbox3 <==
mapth0 (1494554000000000000000000030000000300940111008000)
[size=197 MB][features="0"][hwhandler="0"]
\\_ round-robin 0 [active]
\\_ 5:0:0:1 sdk 8:160 [active][ready]
\\_ round-robin 0 [enabled]
\\_ 5:0:0:0 sdh 8:112 [active][faulty]
...
fyi, this seems like a copy-pasta typo.
 
Old 08-15-2014, 09:53 AM   #10
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,387

Rep: Reputation: 397Reputation: 397Reputation: 397Reputation: 397
Quote:
Originally Posted by tulsi View Post
Hi,

This is my initial attempt to get line..


files=/home/tulsis/multipath.txt
hostname="==>*"
mvalue="mpath*"

if grep faulty $files ;
then
echo " $hostname , $temp ,$mvalue "
else
echo " no faulty in multipath"
fi

Above one is not working & I am working on correct one ..
give me suggestion to create correct one.
- "files" variable should be named "file" as it seems there is only one multipath file
- you echo $temp while temp variable is not defined

Your script should work at least for detecting if "faulty" word is in multipath file

That could be a first step, eg detect if there is faulty in multipath, if not the script could quit without processing further, maybe with a message like "there is no faulty device"
 
Old 08-15-2014, 10:00 AM   #11
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,026

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
heres my attempt:
Code:
[schneidz@hyper ~]$ cat ./tulsi.ksh 
#!/bin/bash

files=tulsi.txt

cat $files | while read line
do
 if [ "`echo $line | grep ==`" ]
 then
  hostname="$line"
 fi
 if [ "`echo $line | grep mpath`" ]
 then
  mvalue="$line"
 fi
 if [ "`echo $line | grep faulty`" ]
 then
  echo $hostname
  echo $mvalue
  echo $line
 fi
# if grep faulty $files ;
# then
#  echo " $hostname , $temp ,$mvalue "
# else
#  echo " no faulty in multipath"
# fi
done
[schneidz@hyper ~]$ ./tulsi.ksh 
==> linuxbox1 <==
mpath0 (3600d0230003228bc000339414edb8101) [size=10 GB][features="0"][hwhandler="0"]
_ 3:0:0:6 sdc 8:64 [active][faulty]
==> linuxbox3 <==
mpath0 (1494554000000000000000000030000000300940111008000)
\_ 5:0:0:0 sdh 8:112 [active][faulty]
i hope none of your linuxbox's have the word faulty in their name.
 
Old 08-16-2014, 05:15 AM   #12
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 804

Rep: Reputation: 186Reputation: 186
Using awk we get
Code:
padmanaban@padmanaban-laptop:~/awk_scripts/fawlty$ awk -f fawlty.awk fawlty.data > fawlty.output
padmanaban@padmanaban-laptop:~/awk_scripts/fawlty$ cat fawlty.output
==> linuxbox1 <==
mpath0 (3600d0230003228bc000339414edb8101) [size=10 GB][features="0"][hwhandler="0"]
\_ 3:0:0:6 sdc 8:64 [active][faulty]
==> linuxbox3 <==

\\_ 5:0:0:0 sdh 8:112 [active][faulty]
padmanaban@padmanaban-laptop:~/awk_scripts/fawlty$ cat fawlty.awk
BEGIN {
	H="" ;
	M="" ;
}
{
	if(substr($0,1,12)=="==> linuxbox") { H=$0; 
		M="";
	}
	if(substr($0,1,5)=="mpath") M=$0 ;
	if(substr($0,length($0)-7,8)=="[faulty]") {
		print H;
		print M;
		print $0;
	}
}
Changing the code for occasionally mapth for mpath is trivial by changing
Quote:
if(substr($0,1,5)=="mpath") M=$0 ;
Quote:
if(substr($0,1,5)=="mpath") M=$0 ;
if(substr($0,1,5)=="mapth") M=$0 ;
In the case of mapth, the seeming wrap around into the next line seems due to the text editor. Even otherwise it's easily handled.

OK

Last edited by AnanthaP; 08-16-2014 at 05:32 AM.
 
Old 08-16-2014, 06:00 AM   #13
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,387

Rep: Reputation: 397Reputation: 397Reputation: 397Reputation: 397
Dirty onliner:
Code:
perl -ne '$h=$_ if /^=/;$m=$_ if /^m/;print "$h$m$_" if /faulty/' /home/tulsis/multipath.txt
 
Old 08-16-2014, 06:59 AM   #14
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Awk alternative:
Code:
awk '/faulty/{print $1 FS $2 FS $NF}' RS="" FS="\n" file
Although it assumes 'faulty' in last record, but gives you something to play with.
 
1 members found this post helpful.
Old 08-16-2014, 07:10 AM   #15
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,832

Rep: Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820
Big assumption.
What about if you get "faulty" in more than one mpath[[:digit:]] for a single hostname ?. I can see lots of potential corner cases.

The OP needs to analyse this in more depth methinks.
 
  


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] Attach word from file to the end of other file recursively bash vnistelroot Programming 3 01-12-2012 10:51 AM
bash shell script read file word by word part 2 justina Programming 7 01-25-2011 02:19 PM
[SOLVED] bash shell script read file word by word. justina Programming 15 01-22-2011 11:12 AM
how to delete last number/word of a file and incude file count at the end of the chennaiguy Linux - Newbie 2 02-18-2008 10:08 PM
[SOLVED] find a word in a file, and change a word beneath it ?? vikas027 Programming 10 02-14-2008 10:46 PM


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