Awk, function of underscore char.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk, function of underscore char.
# 1  
Old 02-13-2014
Awk, function of underscore char.

Hello Friends,

I would appreciate so much if you could explain how the underscores works at the following code? Sorry if it sounds a bit novice question.

Code:
awk -F',' 'NR==FNR{_[$0]=1;next}!_[$4]{print}' exclude infile

KR,
Eagle
# 2  
Old 02-13-2014
It is like a letter. You can replace it with "A" for example. The array is called "_".
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 02-13-2014
Quote:
Originally Posted by Scrutinizer
It is like a letter. You can replace it with "A" for example. The array is called "_".
Hmm i could not guess it, well what about

Code:
_[$0]=1,

it takes all $0 as an array here and then checks if $4 in second file equals $0 in first file. but what _[$0]=1 means?
# 4  
Old 02-13-2014
It sets an array element with index $0 to the value 1. In the second part, if checks if for $4 in the second file there is an array element with a value other than 0 or "" and if so, it does not print the record.

Another (slightly better) way of doing it would be:
Code:
awk -F, 'NR==FNR{A[$0]; next}!($4 in A)' exclude infile

# 5  
Old 02-13-2014
As per manual: The name of a variable must be a sequence of letters, digits and underscores, but it may not begin with a digit. Case is significant in variable names.

So underscore is a valid variable name.
This User Gave Thanks to Yoda For This Post:
# 6  
Old 02-13-2014
It's just a somewhat oddly named variable.

Even more fun is when someone peppers an awk program with a variable Z or something, and never actually sets its value -- it's just a one-letter way to get a blank string, as opposed to "" !
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 02-13-2014
Quote:
Originally Posted by Scrutinizer
It sets an array element with index $0 to the value 1. In the second part, if checks if for $4 in the second file there is an array element with a value other than 0 or "" and if so, it does not print the record.

Another (slightly better) way of doing it would be:
Code:
awk -F, 'NR==FNR{A[$0]; next}!($4 in A)' exclude infile

this way is really more clear, understandable for me Scrutinizer, thanks!

Corona so its a lazy (or shorter) way of using variables Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Segmentation fault when I pass a char pointer to a function in C.

I am passing a char* to the function "reverse" and when I execute it with gdb I get: Program received signal SIGSEGV, Segmentation fault. 0x000000000040083b in reverse (s=0x400b2b "hello") at pointersExample.c:72 72 *q = *p; Attached is the source code. I do not understand why... (9 Replies)
Discussion started by: jose_spain
9 Replies

2. Shell Programming and Scripting

Need help on awk for printing the function name inside each function

Hi, I am having script which contains many functions. Need to print each function name at the starting of the function. Like below, functionname() { echo "functionname" commands.... } I've tried like below, func=`grep "()" scriptname | cut -d "(" -f1` for i in $func do nawk -v... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

3. Shell Programming and Scripting

awk split after second underscore in field

I am trying to split a tab-delimeted file using awk after the second _ in bold. The awk below is close but splits on the first _, and I am not sure how to use the second _. Thank you :). file chr1 92145889 92149424 NM_001195684_exon_0_10_chr1_92145900_r 0 - chr1 92161218 ... (4 Replies)
Discussion started by: cmccabe
4 Replies

4. Programming

Small query regarding function "char * strerror(int errnum)"

As this function returns the address of the string corressponding to the errno value provided to it. Can someone please let me know where, in the memory, it could be (on freeBSD). The MAN page tells under the BUG section that "For unknown error numbers, the strerror() function will return its... (5 Replies)
Discussion started by: Praveen_218
5 Replies

5. Programming

STL algorithm merge() function to concatenate char arrays

When the STL generic algorithm's merge() function is used to merge two char arrays, the output is not as expected. Below is the program I tried with. #include <iostream> #include <algorithm> #include <cstring> #include <deque> #include <iterator> using namespace std; int main() { ... (3 Replies)
Discussion started by: royalibrahim
3 Replies

6. Programming

unable to send a char parameter from main to a function

why does this not work? #include <stdio.h> #include <stdlib.h> char getFileMode(char charChanger) { char filetype; /*var to hold the value to be returned*/ filetype = charSetter; /*set filetype to "l" if it is a symlink*/ return filetype; } int main(void){ char... (8 Replies)
Discussion started by: bluetxxth
8 Replies

7. Shell Programming and Scripting

Using of gsub function in AWK to replace space by underscore

I must design a UNIX script to monitor files whose size is over a threshold of 5 MB in a specific UNIX directory I meet a problem during the for loop in my script. Some file names contain spaces. ls -lrt | awk '$5>=5000000 && length($8)==5 {gsub(/ /,"_",$9); print};' -rw-r--r-- 1 was61 ... (2 Replies)
Discussion started by: Scofield38
2 Replies

8. Shell Programming and Scripting

ksh function getopts get leading underscore unless preceded by hyphen

If I call my function with grouped options: "logm -TDIWEFO 'message' ", then only the "T" gets parsed correctly. The subsequent values returned have underscores prefixed to the value: "_D", "_I", etc. If I "logm -T -DIWEFO 'message' ", the "T" and the "D" are OK, but "I" through "O" get the... (2 Replies)
Discussion started by: kchriste
2 Replies

9. Shell Programming and Scripting

Regex/sed - matching any char,space,underscore between : and /

trying to remove the portion in red: Data: mds_ar/bin/uedw92wp.ksh: $AI_SQL/wkly.sql mds_ar/bin/uedw92wp.ksh: $EDW_TMP/wkly.sql output to be: mds_ar/bin/uedw92wp.ksh: wkly.sql mds_ar/bin/uedw92wp.ksh: wkly.sql SED i'm trying to use: sed 's/:+\//: /g' input_file.dat >... (11 Replies)
Discussion started by: danmauer
11 Replies

10. Shell Programming and Scripting

usage of underscore in awk

Hi what is the purpose of using underscore in awk. I suppose it is for defining macro's and reducing repeatation but can some one show me an example? (6 Replies)
Discussion started by: ahmedwaseem2000
6 Replies
Login or Register to Ask a Question