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
# 1  
Old 10-19-2010
awk & grep - check for a value and write sub-word

Hi people,

I have a file status.txt:
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            
------------------------------------------------------------

I want to write ports whose STAT is different than 111111. the script should look for STAT1 STAT2 and STAT3 where the values are NOT 111111 and get related sub-portnames. according to example the script should write:
Code:
port1371
port2131
port2411
port2413
port3111
port3112
port3113
port3562
port4103

it seems complex but I have no idea to do it?? SmilieSmilieSmilieSmilie
# 2  
Old 10-19-2010
If you have a fixed length:

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

# 3  
Old 10-19-2010
Code:
 
awk '/^[0-9]/ {  gsub(/-|\//," ",$3);l=split($3,a," "); 
            for (i=1;i<l;i++) {if ($(i+3)!="111111") print a[1]a[i+1]}
          }' infile


Last edited by rdcwayx; 10-19-2010 at 08:17 PM..
# 4  
Old 10-19-2010
thanks but both solutions have errors:
Code:
awk: syntax error near line 1.
awk: illegal statement near line 1.

# 5  
Old 10-19-2010
Quote:
Originally Posted by gc_sw
thanks but both solutions have errors:
Code:
awk: syntax error near line 1.
awk: illegal statement near line 1.

You had similar problems in another thread, and nawk solved it.

If you are using Solaris don't use "awk", but nawk or /usr/xpg4/bin/awk.
# 6  
Old 10-19-2010
Quote:
Originally Posted by scottn
You had similar problems in another thread, and nawk solved it.

If you are using Solaris don't use "awk", but nawk or /usr/xpg4/bin/awk.
i have tried nawk also. but this time, it says:

Code:
nawk: can't open file
source line number 1

# 7  
Old 10-19-2010
Hi.

Sounds like you passed the wrong filename to nawk. The file is status.txt?

If that's not the problem, please copy and paste exactly the command you are running.
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