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 |
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 ?
|
hostname is 3 lines before faulty ?
if yes, Code:
grep -B 3 faulty file.txt |
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. |
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 |
And what about the 'faulty' for linuxbox3?
|
@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. |
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. |
Quote:
|
Quote:
- 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" |
heres my attempt:
Code:
[schneidz@hyper ~]$ cat ./tulsi.ksh |
Using awk we get
Code:
padmanaban@padmanaban-laptop:~/awk_scripts/fawlty$ awk -f fawlty.awk fawlty.data > fawlty.output Quote:
Quote:
OK |
Dirty onliner:
Code:
perl -ne '$h=$_ if /^=/;$m=$_ if /^m/;print "$h$m$_" if /faulty/' /home/tulsis/multipath.txt |
Awk alternative:
Code:
awk '/faulty/{print $1 FS $2 FS $NF}' RS="" FS="\n" file |
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. |
All times are GMT -5. The time now is 07:53 PM. |