Doubts About awk, and Gawk


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Doubts About awk, and Gawk
# 1  
Old 02-27-2013
Doubts About awk, and Gawk

well i have some doubts about the use of this commands:


my first doubt is to know if there is a way to execute a awk program from a file? (now i do copy paste, i copy the script of a notepad on the terminal and then i press enter, but i want to put this scripts in some folder and execute them)

another doubt i have is how can i say to awk for retrieving all the information beside certaing pattern for example i want to get all the SBL codes except SBL151

Code:
awk ' {
        for(i=1;i<=NF;i++) {
                if($i ~ /SBL/ && $i <> /SBL151/ )
                        a[$i]++;
        }
} END {
        for(i in a)
                print i, a[i];
}' file

is this code ok?

other doubt is if is possible to use a switch in my code i have something like

Code:
awk ' {
        for(i=1;i<=NF;i++) {
                if($i ~ /SBL/)
                        a[$i]++;
                 if($i ~ /DBL/)
                        c[$i]++;
                 if($i ~ /XSL/)
                        d[$i]++;
                 if($i ~ /NBL/)
                        e[$i]++;

        }
} END {
        for(i in a)
                print i, a[i];
}' file

i want to make something like a switch based of i value instead of making 4 ifs.



and my last doubt is recently download gawk 3.1.2(awk for windows) but i cant make it work, i go with the cmd to the bin folder and then i try to copy paste the script of awk in the cmd and press enter but a lot of error codes are displayed. i dont know how to use this program, any of you have used it?, whats the process to run a script?


thanks for all!!
# 2  
Old 03-01-2013
Well, you can put awk in a shell script first line "#!/bin/bash" or the like, or make a pure awk script "#!/bin/awk -f".
# 3  
Old 03-02-2013
Syntax error:
Quote:
if($i ~ /SBL/ && $i <> /SBL151/ )
Should be either another REGEXP
Code:
if ( $i ~ /SBL/ && $i !~ /SBL151/ )

or a string comparison
Code:
if ( $i ~ /SBL/ && $i != "SBL151" )

# 4  
Old 03-05-2013
Maybe '/SBL/' should be '/^SBL[0-9]\{3\}$/' ?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

This function (decode64) runs on gawk but not on busybox awk

Hello, I'm trying to figure out a way to use a decode64 function in an embedded system who has few utilities, including busybox. Right now have something like this (taken from "google base64-and-base85-encoding-awk-scripts" sorry, I'm not able to post urls yet) _decode64() { &&... (4 Replies)
Discussion started by: chilicuil
4 Replies

2. Shell Programming and Scripting

Awk miscellaneous doubts

Hi, I am having the following doubts on awk. Please clarify for me. a) What does it mean? awk '$1=$1' and how does it change if I append FS="" to the above code? b) What if I use awk -vFS="\n" (i.e) setting (input) field separator to newline char, then what will be the value of $0,... (6 Replies)
Discussion started by: royalibrahim
6 Replies

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

4. Shell Programming and Scripting

Gawk / Awk Merge Lines based on Key

Hi Guys, After windows died on my netbook I installed Lubuntu and discovered Gawk about a month ago. After using Excel for 10+ years I'm amazed how quick and easily Gawk can process data but I'm stuck with a little problem merging data from multiple lines. I'm an SEO Consultant and provide... (9 Replies)
Discussion started by: Jamesfirst
9 Replies

5. Shell Programming and Scripting

Removing \n within a record (awk/gawk)

I am using a solution that was provided by a member: awk '{s=$0;if(length(s) < 700){getline; s=s " " $0}printf("%s\n",s)}' This scans through a file and removes '\n' within a record but not the record delimiter. However, there are instances where there are MULTIPLE instances of '\n'... (10 Replies)
Discussion started by: CKT_newbie88
10 Replies

6. Shell Programming and Scripting

Substitution using awk/gawk

Hello, I have a file containing lines such as: (1 104 (16) (17) (18) (102))$ (1 105 (16) (17) (19:21) (102))$ I would like to extract the numbers, only by using awk (or gawk). I do not want to use "sed" as it is very slow. For now my solution consists in... (2 Replies)
Discussion started by: jolecanard
2 Replies

7. Shell Programming and Scripting

awk miscellaneous doubts

In this following command: awk 'BEGIN{ORS=""}1' what does '1' signifies that comes after closing curly brace '}' of awk? I guess, it does not mean 'first occurrence' because I verified that. And, pls tell me how to override or suppress awk's field variables like $1, $2.. by positional... (7 Replies)
Discussion started by: royalibrahim
7 Replies

8. Shell Programming and Scripting

awk, sed and a shell doubts

Hi, I have 3 doubts which I posted it here. Doubt 1: I have a file containing data: 22 -73 89 10 99 21 15 -77 23 63 -80 91 -22 65 28 97 I am trying to print the fields in the reverse order and replace every field by its absolute (positive) value (ie.) I am looking for output: 10... (8 Replies)
Discussion started by: royalibrahim
8 Replies

9. Shell Programming and Scripting

awk,gawk in bat file

Hi. I'm trying to convert bat file into shell script. Bat file invokes awk file in one section: c:\upg\exe\gawk -f c:\upg\awk\gen_sae.awk -v OP=C:\\upg\\lod\\... ...c:\upg\ref\saaxi.ref c:\upg\log\SAAEPWO.log c:\upg\ref\saaepref.log First of all I issued unix2dos command on that awk file.... (0 Replies)
Discussion started by: andrej
0 Replies

10. UNIX for Dummies Questions & Answers

Calculating field using AWK, or GAWK script

Hello all, I'm totally new to UNIX/Linux but I'm taking a course in it at my local JC. My question: I have been tasked with writing a gawk script that will create a nicely formatted report. That part I've done ok on...however, the very last thing that must be done is a calculation of a... (4 Replies)
Discussion started by: Trellot
4 Replies
Login or Register to Ask a Question