Nawk command to output in var


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Nawk command to output in var
# 1  
Old 08-05-2014
Nawk command to output in var

Hi

I have this command, which counts number of lines in a specific file and then prints it on screen.
Code:
nawk 'NF{c++}END{print "Number of GPS coordinates in file: "c}' $filename


I would like to have the output put into a variable, but can't seem to find the correct argument for it.

How do I manage that ?

Thanks in advance


Moderator's Comments:
Mod Comment Please use CODE tags. It makes code, input & output/errors so much easier to read and preserves critical spacing

Last edited by rbatte1; 08-05-2014 at 10:42 AM.. Reason: Spelling and CODE tags
# 2  
Old 08-05-2014
Code:
my_variable=$(nawk 'NF{c++}END{print "Number of GPS coordinates in file: "c}' $filename)
echo $my_variable

# 3  
Old 08-05-2014
Ahh great, thanks alot...

Had tried different approaches without luck, was pretty sure i could be done... thanks Smilie
# 4  
Old 08-05-2014
A variant: store only the number.
Code:
cnt=$(nawk 'NF{c++}END{print c}' $filename)
echo "Number of GPS coordinates in file: $cnt"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to print and append output of nawk script in commandline and as well into a file?

Hi All, I am working on nawk script, has the small function which prints the output on the screen.Am trying to print/append the same output in a file. Basically nawk script should print the output on the console/screen and as well it should write/append the same result to a file. script :... (3 Replies)
Discussion started by: Optimus81
3 Replies

2. UNIX for Advanced & Expert Users

Not logging ftp connections in /var/adm/wtmpx file (in last command output)

Hi all, I have F5 load balancer on my system and checking service status by opening an ftp session in every 30 seconds. These ftp sessions are being logged in /var/adm/wtmpx and filling up the file. when i run the last command most of the output is this ftp session. I was wondering if there is a... (1 Reply)
Discussion started by: cepxat
1 Replies

3. Shell Programming and Scripting

nawk is truncating output

Legends, I have 2 files f1 and f2. when i use nawk to compare the difference(subtraction) from 4th column of the file, it truncates the output. can you please help to resolve this. subtraction is (4th col of f1 - 4th col of f2). but it gives only below lines out of 116. I want to print all... (7 Replies)
Discussion started by: sdosanjh
7 Replies

4. Shell Programming and Scripting

Var in nawk script parse incorrectly

I'm writing a Texas Hold'em script in bash v3.00.16(1) to learn more about awk/nawk scripts and regex expressions by trying to randomize a list of names using awk's rand function. The problem is that the elements in the var convert to a single element in the nawk script. I've tried several things,... (4 Replies)
Discussion started by: HexKnot
4 Replies

5. Shell Programming and Scripting

Concatenate piped output and some var?

Hello, There is pipe chain and I want concacenate piped data with some variable: balh blah| ... $var1 What command I should use instead ... to concatenate piped output with $var1. I think I coud solve this using temp var - but could it be done in one line like sample above ? thanks... (4 Replies)
Discussion started by: vilius
4 Replies

6. Shell Programming and Scripting

help me how to use nawk for required output

Hi all i have 2 input files 1st file is N1 | N2|N3|N4|N5|N6|N7|N8|N9 4041491000|245160|1|0|0|1|0|0|0 4041401505|152178|1|1|1|1|0|0|0 4041450004|014052|1|1|1|1|0|0|0 4041450005|580916|1|1|1|1|0|0|0 4041491000|230990|1|0|1|1|0|0|0 4041460001|338317|1|1|1|1|0|0|0 2nd file N1 |... (8 Replies)
Discussion started by: dodasajan
8 Replies

7. Shell Programming and Scripting

Get coprocess output into var

This is probably a simple one for the wise. I have just started using a coprocess (first time) in order to facilitate telnet'ing from inside a shell script. It's working, but when I run a remote command I need to get the output into a local variable, but alas my kung-fu is weak. #!... (10 Replies)
Discussion started by: dan-e
10 Replies

8. UNIX for Dummies Questions & Answers

assigning (numeric) command output to var tcsh

Hello, I'm trying to assign a numeric value that is returned from one of my programs to a variable in tcsh. I want to do: @ r10 = './my_prog file 35' where ./my_prog file 35 returns a decimal value, but this doesn't work. How do I achieve the desired result? Janet (4 Replies)
Discussion started by: psran
4 Replies

9. Shell Programming and Scripting

assigning nawk output to shell variable

Hello friends, I doing the follwing script , but found problem to store it to a shell variable. #! /bin/sh for temp in `find ./dat/vector/ -name '*.file'` do echo $temp nawk -v temp=$temp 'BEGIN{ split(temp, a,"\/"); print a}' done output: ./dat/vector/drf_all_002.file... (6 Replies)
Discussion started by: user_prady
6 Replies

10. Shell Programming and Scripting

Assigning nawk output to variables

I do a lot of command line scripting to capture data from files or other command output. I've checked in a number of Unix and scripting books but for the life of me I can't find out how to asign field data from nawk output into variables that I can manipulate later. For example, reading a two... (6 Replies)
Discussion started by: steveje0711
6 Replies
Login or Register to Ask a Question