LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   get word from file (https://www.linuxquestions.org/questions/linux-newbie-8/get-word-from-file-4175514805/)

tulsi 08-14-2014 08:06 AM

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

schneidz 08-14-2014 08:18 AM

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 ?

keefaz 08-14-2014 08:19 AM

hostname is 3 lines before faulty ?
if yes,
Code:

grep -B 3 faulty file.txt

tulsi 08-15-2014 02:43 AM

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.

AnanthaP 08-15-2014 04:36 AM

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

grail 08-15-2014 05:19 AM

And what about the 'faulty' for linuxbox3?

tulsi 08-15-2014 05:39 AM

@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.

tulsi 08-15-2014 08:01 AM

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.

schneidz 08-15-2014 08:35 AM

Quote:

Originally Posted by tulsi (Post 5221435)
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.

keefaz 08-15-2014 08:53 AM

Quote:

Originally Posted by tulsi (Post 5221546)
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"

schneidz 08-15-2014 09:00 AM

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.

AnanthaP 08-16-2014 04:15 AM

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

keefaz 08-16-2014 05:00 AM

Dirty onliner:
Code:

perl -ne '$h=$_ if /^=/;$m=$_ if /^m/;print "$h$m$_" if /faulty/' /home/tulsis/multipath.txt

grail 08-16-2014 05:59 AM

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.

syg00 08-16-2014 06:10 AM

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.