awk & grep - check for a value and write sub-word


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk & grep - check for a value and write sub-word
# 8  
Old 10-19-2010
I am pasting code:
Code:
 nawk '/port/ {port=substr($0,11,7);p[port"1"]=substr($0,26,6);p[port"2"]=substr($0,34,6);p[port"3"]=substr($0,43,6)} END{for (i in p) {if (p[i] != "      " && p[i] != "111111"){print i}}}' status.txt

the result is:
Code:
ort ava1
ort ava2
ort ava3
3 por1
3 por2
3 por3
por1
por2
por3
ability1
ability2
ability3
5 por2
8 por1
8 por2
8 por3

my status.txt is:
Code:
Following 6 ports are totally or partially unavailable:
------------------------------------------------------------
MOD LINK  PORTNAMES      STAT1   STAT2    STAT3   SYN   TYPE
------------------------------------------------------------
8   Pr37  port137-1/2/3  LLLLL   111111   111111  1111   A
13  Pr38  port213-1      L00000                   1110   A
42  Pr39  port241-1/2/3  L00000  111111   L00000  1110   A
43  Pr43  port311-1/2/3  100011  100011   100011  1110   A
43  Pr49  port356-1/2    111111  L00000           1110   A
45  Pr57  port410-1/2/3  111111  111111   L00000  1110   A            
------------------------------------------------------------
Port availability: 460 of 468 ports are up (98.3 %)
Site availability: 164 of 168 sites are fully operational (97.6 %)
Unlocked Port availability: 460 of 460 unlocked ports are up (100.0 %)

it is getting wrong lines. but the it seems right approximation.
# 9  
Old 10-19-2010
A perl version:
Code:
perl -ne 'if (/^\d+\s+\S+\s+port(\d+)-(\S+)\s+(.*)\d\d\d\d\s+A.*/ ) {$port="port".$1;@rang=split /\//,$2;@val=split /\s+/,$3; 
          foreach $i ( @rang ) {$i--;if ( $val[$i] ne "111111") { $i++;print $port.$i."\n";}}}' file



---------- Post updated at 02:05 PM ---------- Previous update was at 02:01 PM ----------

Working fine for me:
Code:
awk '/port...-/ {port=substr($0,11,7);p[port"1"]=substr($0,26,6);p[port"2"]=substr($0,34,6);p[port"3"]=substr($0,43,6)}
     END{for (i in p) {if (p[i] != "      " && p[i] != "111111"){print i}}}' status.txt|sort
port1371
port2131
port2411
port2413
port3111
port3112
port3113
port3562
port4103

# 10  
Old 10-19-2010
thanks Klashxx, your result is the BEST Smilie
# 11  
Old 10-19-2010
shell code:
  1. cut -c11-50 infile |
  2. { read; read; read; read          # skip the first 4 lines
  3.   while read portnames stat1 stat2 stat3
  4.   do
  5.     for i in 1 2 3; do
  6.       eval s=\$stat$i
  7.       if [ "$s" != "111111" ] && [ -n "${s## *}" ]; then
  8.         echo "${portnames%-*}$i"
  9.       fi
  10.     done
  11.   done
  12. }
# 12  
Old 10-20-2010
Quote:
Originally Posted by Scrutinizer
shell code:
  1. cut -c11-50 infile |
  2. { read; read; read; read # skip the first 4 lines
  3. while read portnames stat1 stat2 stat3
  4. do
  5. for i in 1 2 3; do
  6. eval s=\$stat$i
  7. if [ "$s" != "111111" ] && [ -n "${s## *}" ]; then
  8. echo "${portnames%-*}$i"
  9. fi
  10. done
  11. done
  12. }
thanks Scrutinizer;
i want to modify my script according to this new status.txt:
Code:
Following 4 sites are totally or partially unavailable:
---------------------------------------------------------------------------------------------------------------------
MOD  IUBLINK  CELLNAMES            CFRPHEM1 CFRPHEM2 CFRPHEM3 CFRPHEM4 CFRPHEM5 CFRPHEM6 ICDS   TN ATMPORTS
---------------------------------------------------------------------------------------------------------------------
  8  Iub37    cell137-1/2/3        LLLLL    111111   111111                              1111   A                
 13  Iub135   cell337-1            L00000                                                1110   A                
 42  Iub153   cell355-1/2/3        L00000   L00000   L00000                              1110   A                
 45  Iub158   cell360-1/2/3        L00000   L00000   L00000                              1110   A                
---------------------------------------------------------------------------------------------------------------------
Cell availability: 460 of 468 cells are up (98.3 %)
Site availability: 164 of 168 sites are fully operational (97.6 %)
Unlocked Cell availability: 460 of 460 unlocked cells are up (100.0 %)

i have changed "portnames" with "cell" but there is no result? could you please help me to modify new script according to new status.txt??

thanks
# 13  
Old 10-20-2010
You need to cut different columns, also you need to cut off the lines that do not matter. Lastly you need 6 stat variables. For example
shell code:
  1. grep "^ *[0-9]" infile435 | cut -c15-88 |
  2. while read portnames stat1 stat2 stat3 stat4 stat5 stat6
  3. do
  4.   i=0
  5.   while [ $((i+=1)) -le 6 ]
  6.   do
  7.     eval s=\$stat$i
  8.     if [ "$s" != "111111" ] && [ -n "${s## *}" ]; then
  9.       echo "${portnames%-*}$i"
  10.     fi
  11.   done
  12. done

You can change the variable names to match the new situation:
shell code:
  1. grep "^ *[0-9]" infile435 | cut -c15-88 |
  2. while read cellnames cfrphem1 cfrphem2 cfrphem3 cfrphem4 cfrphem5 cfrphem6
  3. do
  4.   i=0
  5.   while [ $((i+=1)) -le 6 ]
  6.   do
  7.     eval s=\$cfrphem$i
  8.     if [ "$s" != "111111" ] && [ -n "${s## *}" ]; then
  9.       echo "${cellnames%-*}$i"
  10.     fi
  11.   done
  12. done

Last edited by Scrutinizer; 10-20-2010 at 06:32 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep or awk a unique and specific word across many fields

Hi there, I have data with similar structure as this: CHR START-SNP END-SNP REF ALT PATIENT1 PATIENT2 PATIENT3 PATIENT4 chr1 69511 69511 A G homo hetero homo hetero chr2 69513 69513 T C . hetero homo hetero chr3 69814 69814 G C . . homo homo chr4 69815 69815 C A hetero . . hetero is... (10 Replies)
Discussion started by: daashti
10 Replies

2. Shell Programming and Scripting

Write a script for getout afew line and grep a define word

dear all Hi i want write a script can count number of my log file and every 5min run script for cgk log file for find a specific word such as Error for example in first 5 min we have 500 line in my log file and in second 5min we have 2500 line in my log file how can chk error word in my log... (4 Replies)
Discussion started by: Baber
4 Replies

3. Shell Programming and Scripting

How to write this script:- check output word and send a mail?

Hi Guys, I am not Good at scripting. I need to write a script such that if output of command shows the particular word in output then send mail to abc@compay.com -bash-3.2$ ps -ef | grep bpbkar root 6040 1 0 13:05:19 ? 0:00 bpbkar -r 2678400 -ru root -dt 47395 -to 0... (20 Replies)
Discussion started by: manalisharmabe
20 Replies

4. Shell Programming and Scripting

Piping through grep/awk prevents file write

So, this is weird... I'm running this command: iotop -o -P -k -bt -d 5 I'd like to save the output relelvant to rsyslogd to a file, so I do this: iotop -o -P -k -bt -d 5 | grep rsyslogd >> /var/log/rsyslogd Nothing is written to the file! I can write the full output to the file: ... (2 Replies)
Discussion started by: treesloth
2 Replies

5. Shell Programming and Scripting

awk - grep particular word from output

Hi Experts, - Getting error while using it through a variable to get the PID, PID=42 # UNIX95=1 ps -e -o pcpu,pid,ppid,stime,etime,args | awk '{if ($2~"^42$") print $0}' 0.00 42 0 Feb 10 600-17:21:29 nfs_async_io - But when using with a variable it is not working . #... (6 Replies)
Discussion started by: rveri
6 Replies

6. Shell Programming and Scripting

Grep to isolate a text file line and Awk to select a word?

I am looking at using grep to locate the line in the text file and them use awk to select a word or words out of it. I know awk needs -v to allow a variable to be used, but also needs -F to allow the break up of the sentence and allow the location of separate variables. $line = grep "1:" File |... (8 Replies)
Discussion started by: Ironguru
8 Replies

7. Shell Programming and Scripting

awk (gawk) grep & columns

Hi, I'm working with gawk (on DOS) today. A goal is: find a string for-instance '123', cut a line in two columns and write second one. The problem is: command line works OK, awk file doesn't. But I would like to work with file because there are many strings to find. input: line command: awk... (4 Replies)
Discussion started by: frajer
4 Replies

8. Shell Programming and Scripting

PHP read large string & split in multidimensional arrays & assign fieldnames & write into MYSQL

Hi, I hope the title does not scare people to look into this thread but it describes roughly what I'm trying to do. I need a solution in PHP. I'm a programming beginner, so it might be that the approach to solve this, might be easier to solve with an other approach of someone else, so if you... (0 Replies)
Discussion started by: lowmaster
0 Replies

9. Shell Programming and Scripting

grep & awk

Hi all, I'm figuring on how to grep only specific data I want below: Bin Total % ----- ------- ----- 1 15 42.9 Bin Total % ----- ------- ----- 2 15 ... (3 Replies)
Discussion started by: *Jess*
3 Replies

10. Shell Programming and Scripting

how to grep word from .gz files & .z files

Hi I have find some account numbers from .gz files. There thousands of .gz files, also i am unable to grep .gz / .z files. I want to find file names & and those lines from list. Could you pls tell me / give me any syntax for finding word from ,gz files using grep? Thanks in advance (8 Replies)
Discussion started by: udaya_subbu
8 Replies
Login or Register to Ask a Question