awk equivalent code in C for printing NF


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk equivalent code in C for printing NF
# 1  
Old 05-11-2013
awk equivalent code in C for printing NF

Hi all !

whether anyone in forum knows what awk will use while printing number of fields in file(NF) ?

for example

Code:
 awk END'{print NF}' file

prints number of columns in file

if anyone knows equivalent code in C kindly share or explain logic behind it
# 2  
Old 05-11-2013
Quote:
Originally Posted by Akshay Hegde
whether anyone in forum knows what awk will use while printing number of fields in file(NF) ?
The source code for the latest release of GNU awk is available from the GNU project's ftp server and its many mirrors.

The current development sources are available through the gawk project on savannah.
# 3  
Old 05-11-2013
Thank you so much Yoda
# 4  
Old 05-12-2013
This prints number of fields for every line and the total last.
Code:
awk '{print NF;t+=NF} END {print "total="t}'

# 5  
Old 05-12-2013
Dear Jotne my intention is to know what awk will use internally when you type
Code:
awk 'END{print NF}' file

on terminal, in fact I am searching for c code to do the same..

for instance in C we use following to find line count thats NR in awk

Code:
// Line Count
        while(1)
            {
            ch=fgetc(fp);
            if(ch==EOF)
                break;
            if(ch=='\n')
                NR++;
            }

# 6  
Old 05-12-2013
Quote:
Originally Posted by Akshay Hegde
Dear Jotne my intention is to know what awk will use internally when you type
Code:
awk 'END{print NF}' file

on terminal, in fact I am searching for c code to do the same..
This is an occasional misconception in programming languages... awk does not have a "print number of fields in end block" piece of code, that'd be an oddly specific thing to have. You'd have to find two separate, independent things in awk's source code:

1) Whatever sets the NF special variable.
2) Whatever causes the END code block to be run.

There are many ways to do it in C, but you almost certainly wouldn't be doing it the way awk does it, which can be quite complex -- some versions of awk let you use a regex for the field separator...

One way:

Code:
char fs=' ';
const char *p="a b c d e";
int nf=0;

// Keep looping until you can't find fs
while(p=strchr(p, fs)) nf++;

printf("nf=%d\n", nf);

# 7  
Old 05-12-2013
thank you..so much dear...I added end block so that it will print only one value rather than printing NF of each line..
once again thank you..for explanation..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk script Equivalent .

Hello. I wrote some code for an awk command but I want to learn to turn it into an awk script but am stuck. I have a file (data.csv) that has the following data: ADD,1,3,5,8,10,11,54 SUB,1,2,3,4 ADD,15,18,21,42,37 ADD,1,1,1,0,0,3,16 ADD,4,1,8,0,4,6,13,16,17,20,8,6,4 SUB,13,8If the line... (5 Replies)
Discussion started by: Eric7giants
5 Replies

2. Shell Programming and Scripting

awk code to inspect variable before printing

i have a unique scenario id like help with. im currently running this command and it does what i want: printf '%s\n' "${RawContent}" | awk '/## Beginning Stages ##/,/## Ending Stages ##/' | awk '!/^#.*\!|^#\!|DefaultError/' Can this be shortened? I'm looking for something portable as... (8 Replies)
Discussion started by: SkySmart
8 Replies

3. Shell Programming and Scripting

sed Equivalent for awk/grep

Any equivalent command using awk or grep? sed -n "/^$(date --date='10 minutes ago' '+%b %_d %H:%M')/,\$p" /abc.log (7 Replies)
Discussion started by: timmywong
7 Replies

4. Shell Programming and Scripting

Java - Arrays.binarySearch function equivalent in awk

Hi all Does anyone know Java-Arrays.binarySearch function equivalent in awk I tried like this but it's not correct one,it just returns array index if and only when searched value available in array, for some reason if searched value not found then I want to return upper nearest neighbour index.... (1 Reply)
Discussion started by: Akshay Hegde
1 Replies

5. Shell Programming and Scripting

What is the equivalent of NR (awk) in perl?

Hello, I searched online; it seems that perl use $NR as NR in awk; however it does not work for me. For example, how to re-write the following awk using perl: awk '{ print NR}' inputfile---------- Post updated at 01:55 PM ---------- Previous update was at 12:49 PM ---------- I found... (2 Replies)
Discussion started by: littlewenwen
2 Replies

6. Shell Programming and Scripting

Printing using awk

Hi I am relatively new to awk so i am getting confused a lot I am in need of help ... I am trying to append coloumns to the end of line using AWK I tried using this command awk -F "," '{for(s=7;s<=217;s++);$s="0";}1' OFS=, sam_sri_out It is giving me an output like this...... (1 Reply)
Discussion started by: Sri3001
1 Replies

7. Shell Programming and Scripting

Create a .sh file for an equivalent Excel VBA code

Hi guys, I am new to Unix, Need your help here. I have installed cygwin software (Unix) in my computer (Windows vista). Now i want to create a shell script (.sh file)/other script which is equivalent of VBA code (at the bottom) and then put this .sh file into bin directory of c:/cygwin. so... (7 Replies)
Discussion started by: bansalpankaj88
7 Replies

8. Shell Programming and Scripting

awk equivalent of regex

Hi all, Can someone tell me what's the (g)awk equal of this simple regex to find ip addresses in urls: egrep "^http://{1,3}\.{1,3}\.{1,3}\.{1,3}(:{1,5})?/"Input: http://10.0.0.1/query.exe http://11y10x09w:80/howaboutme http://192.168.100.190:1234/takeme.gpg Output:... (8 Replies)
Discussion started by: r4v3n
8 Replies

9. Windows & DOS: Issues & Discussions

awk to findstr equivalent

Hi, I 'd like to translate this command from awk to findstr on Windows DOS FILE: str1 server1 a str1 server2 a str2 server1 b str2 server2 b Awk command: awk ' $1 ~/str1/ { print $2, $3 } ' file.txt OUTPUT: server1 a server2 a Thanks, (1 Reply)
Discussion started by: phamp008
1 Replies

10. Shell Programming and Scripting

awk equivalent script

Hi All, I need a script that prints a blank line after finding that the next lines first field doesn't match the current lines first field. I really want to do this in awk to improve my own knowledge but the best I can come up with is a bash script. I'd finally like to understand how awk... (10 Replies)
Discussion started by: pondlife
10 Replies
Login or Register to Ask a Question