Sponsored Content
Full Discussion: awk, What's NF and NR ?
Top Forums Shell Programming and Scripting awk, What's NF and NR ? Post 302350759 by malcomex999 on Saturday 5th of September 2009 05:10:24 AM
Old 09-05-2009
Yes, they are built in variables.
NF is for the number of fields in the current record.
NR is for the number of records in the input file.

so in the above example from you have posted...

Code:
 
awk '{print ; if (NF != 0) print ""}' infile > outfile

it prints the current record from infile and checks for the field if it is not equals to 0, then it prints empty line.

Code:
 
awk 'END {print NR}' infile

After it process all the records(END will show what has been there after the file is processed), it prints the number of records from the infile.

And read..
Code:
 
man awk

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk problem: How to express the single quote(') by using awk print function

Actually I got a list of file end with *.txt I want to use the same command apply to all the *.txt Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics. For example: I got the file below: file1.txt file2.txt file3.txt... (4 Replies)
Discussion started by: patrick87
4 Replies

2. Shell Programming and Scripting

scripting/awk help : awk sum output is not comming in regular format. Pls advise.

Hi Experts, I am adding a column of numbers with awk , however not getting correct output: # awk '{sum+=$1} END {print sum}' datafile 2.15291e+06 How can I getthe output like : 2152910 Thank you.. # awk '{sum+=$1} END {print sum}' datafile 2.15079e+06 (3 Replies)
Discussion started by: rveri
3 Replies

3. Shell Programming and Scripting

awk: assign variable with -v didn't work in awk filter

I want to filter 2nd column = 2 using awk $ cat t 1 2 2 4 $ VAR=2 #variable worked in print $ cat t | awk -v ID=$VAR ' { print ID}' 2 2 # but variable didn't work in awk filter $ cat t | awk -v ID=$VAR '$2~/ID/ { print $0}' (2 Replies)
Discussion started by: honglus
2 Replies

4. Shell Programming and Scripting

Problem with awk awk: program limit exceeded: sprintf buffer size=1020

Hi I have many problems with a script. I have a script that formats a text file but always prints the same error when i try to execute it The code is that: { if (NF==17){ print $0 }else{ fields=NF; all=$0; while... (2 Replies)
Discussion started by: fate
2 Replies

5. Shell Programming and Scripting

Comparison and editing of files using awk.(And also a possible bug in awk for loop?)

I have two files which I would like to compare and then manipulate in a way. File1: pictures.txt 1.1 1.3 dance.txt 1.2 1.4 treehouse.txt 1.3 1.5 File2: pictures.txt 1.5 ref2313 1.4 ref2345 1.3 ref5432 1.2 ref4244 dance.txt 1.6 ref2342 1.5 ref2352 1.4 ref0695 1.3 ref5738 1.2... (1 Reply)
Discussion started by: linuxkid
1 Replies

6. Shell Programming and Scripting

awk command to compare a file with set of files in a directory using 'awk'

Hi, I have a situation to compare one file, say file1.txt with a set of files in directory.The directory contains more than 100 files. To be more precise, the requirement is to compare the first field of file1.txt with the first field in all the files in the directory.The files in the... (10 Replies)
Discussion started by: anandek
10 Replies

7. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

8. Shell Programming and Scripting

Passing awk variable argument to a script which is being called inside awk

consider the script below sh /opt/hqe/hqapi1-client-5.0.0/bin/hqapi.sh alert list --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true >/tmp/alerts.xml awk -F'' '{for(i=1;i<=NF;i++){ if($i=="Alert id") { if(id!="") if(dt!=""){ cmd="sh someScript.sh... (2 Replies)
Discussion started by: vivek d r
2 Replies

9. Shell Programming and Scripting

Pass awk field to a command line executed within awk

Hi, I am trying to pass awk field to a command line executed within awk (need to convert a timestamp into formatted date). All my attempts failed this far. Here's an example. It works fine with timestamp hard-codded into the command echo "1381653229 something" |awk 'BEGIN{cmd="date -d... (4 Replies)
Discussion started by: tuxer
4 Replies

10. Shell Programming and Scripting

awk output yields error: awk:can't open job_name (Autosys)

Good evening, Im newbie at unix specially with awk From an scheduler program called Autosys i want to extract some data reading an inputfile that comprises jobs names, then formating the output to columns for example 1. This is the inputfile: $ more MapaRep.txt ds_extra_nikira_usuarios... (18 Replies)
Discussion started by: alexcol
18 Replies
makedbm(1M)															       makedbm(1M)

NAME
makedbm - make a Network Information System database SYNOPSIS
nis_input_file] nis_output_name] nis_domain_name] nis_master_name] infile outfile database_name Remarks The Network Information Service (NIS) was formerly known as Yellow Pages (yp). Although the name has changed, the functionality of the service remains the same. DESCRIPTION
generates databases (maps) for the Network Information System (NIS) from infile. A database created by consists of two files: and A data- base contains records called dbm records composed of key-value pairs. Each line of infile is converted to a single dbm record; all characters up to the first tab or space form the key, and the remainder of the line is the value. If a value read from infile ends with the value for that record is continued onto the next line. The NIS clients must interpret the character (which means that does not treat the as if it precedes a comment). If infile is a hyphen reads standard input. always generates a special dbm record with the key whose value is the time of last modification of infile (or the current time, if infile is This value is also known as the order number of a map, and prints it for a specified NIS map (see yppoll(1M)). Another special dbm record created by has the key Its value is usually the host name retrieved by however, the option can be used to spec- ify a different value (see gethostname(2)). If the option is used, another special dbm record with the key is created. When this key exists in the NIS host.by* maps or ipnodes.by* maps and the NIS host name resolution fails, the process will query the Internet domain name server, to provide the host name resolution. Before using the option, it is recommended that the name services switch, be set to allow NIS host name resolution first. (Note that, since the process only checks hosts.by* and ipnodes.by* for the existence of the key, using the option on any other NIS map will have no effect. Also, the option should be used on both the *.byname and *.byaddr maps, not one exclusively.) If the option is used, another special dbm record created is the key. If this key exists in an NIS map, will only allow privileged pro- cesses (applications that can create reserved ports) to access the data within the map. Options recognizes the following options and command-line arguments. Create a special dbm record with the key This key, which is in the hosts.byname, hosts.byaddr, ipnodes.byname, and ipnodes.byaddr maps, allows the process to query the Internet domain name server (see named(1M)). Convert the keys of the given map to lowercase. This command option allows host name matches to work independent of character-case distinctions. Accept connections from secure NIS networks only. Create a special dbm record with the key and the value If the option is used, another special dbm record created is the key. If this key exists in an NIS map, will only allow privileged processes to access the data within the map (that is, applications that can create reserved ports). Create a special dbm record with the key and the value nis_output_name. Create a special dbm record with the key and the value nis_domain_name. Replace the value of the special dbm record whose key is with nis_master_name. Undo the database_name (that is, write the contents of database_name to the standard output) one dbm record per line. A single space sepa- rates each key from its value. EXAMPLES
Shell scripts can be written to convert ASCII files such as to the key-value form used by For example, #!/usr/bin/sh /usr/bin/awk 'BEGIN { FS = ":" } { print $1, $0 }' /etc/netgroup | makedbm - netgroup converts the file to a form that is read by to make the NIS map The keys in the database are names, and the values are the remainders of the lines in the file. AUTHOR
was developed by Sun Microsystems, Inc. SEE ALSO
domainname(1), named(1M), ypinit(1M), ypmake(1M), yppoll(1M), gethostname(2), netgroup(4), ypfiles(4). makedbm(1M)
All times are GMT -4. The time now is 06:47 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy