Question about awk - create a user-defined variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Question about awk - create a user-defined variable
# 1  
Old 06-17-2013
Question about awk - create a user-defined variable

Hi, guys,
The content of file is below (from <UNIX® Shells by Example Fourth Edition>):
Quote:
[root@centostest2 test]# cat lab3.data
Mike Harrington:(510) 548-1278:250:100:175
Christian Dobbins:(408) 538-2358:155:90:201
Susan Dalsass:(206) 654-6279:250:60:50
Archie McNichol:(206) 548-1348:250:100:175
Jody Savage:(206) 548-1278:15:188:150
Guy Quigley:(916) 343-6410:250:100:175A
Dan Savage:(406) 298-7744:450:300:275
Nancy McNeil:(206) 548-1278:250:80:75
John Goldenrod:(916) 348-4278:250:100:175
Chet Main:(510) 548-5258:50:95:135
Tom Savage:(408) 926-3456:250:168:200
Elizabeth Stachelin:(916) 440-1763:175:75:300
My code is below:
Code:
gawk -F'[ :]' ' { OFS = "****"; $3 = "(904)"; $8 = $5 + $6 + $7; print } ' lab3.data

The result is below:
Quote:
****525*Harrington****(904)****548-1278****250****100****175
****446an****Dobbins****(904)****538-2358****155****90****201
****360**Dalsass****(904)****654-6279****250****60****50
****525***McNichol****(904)****548-1348****250****100****175
****353*Savage****(904)****548-1278****15****188****150
****525Quigley****(904)****343-6410****250****100****175A
****1025avage****(904)****298-7744****450****300****275
****405**McNeil****(904)****548-1278****250****80****75
****525*Goldenrod****(904)****348-4278****250****100****175
****280*Main****(904)****548-5258****50****95****135
****618Savage****(904)****926-3456****250****168****200
****550th****Stachelin****(904)****440-1763****175****75****300
So, where is the $1? Why is the variable $8 located at the wired position?

Last edited by franksunnn; 06-18-2013 at 12:38 AM..
# 2  
Old 06-18-2013
When I tried this in AWK, it is all fine.
# 3  
Old 06-18-2013
It works fine with awk and gawk. Could be a terminal issue.
# 4  
Old 06-18-2013
I'd bet there's a (windows originated?) <CR> (<carriage return>, ^M, 0x0D) char at the end of each line...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Call user defined function from awk

My requirement is to call function ("fun1") from awk, and print its returned value along with $0. fun1() { t=$1 printf "%02d\n", $t % 60; } echo "Hi There 23" | awk '{print $0; system(fun1 $3)}' Any suggestions what to be modified in above code to achieve requirement.. (5 Replies)
Discussion started by: JSKOBS
5 Replies

2. UNIX for Beginners Questions & Answers

Awk: use variable defined in begin

Hi there, I'm working with file more than 400K lines, 60 columns. Column count is going to be multiple of 12: 60, 12, 72 or so. NF/12 gives me on how many iterations I've to do to check certain value. For example: 7, 14th if only 24 columns in file. 7th, 14th and 21st if 36 columns in... (6 Replies)
Discussion started by: genome
6 Replies

3. Shell Programming and Scripting

[solved] awk: placement of user-defined functions

Hi folks, is there any recommendation, especially from a point of performance, about where to place a user-defined function in awk, like in BEGIN{} or if it is only need once at the end in END{}? Or doesn't it matter at all since, awk is so clever and only interprets it once, wherever it is... (3 Replies)
Discussion started by: zaxxon
3 Replies

4. AIX

AIX : User Defined Authorizations Question

Hello everyone, We have got a Server say Testserver with AIX 6.1 running on it. We want to create user defined authorization for syslogd, ntpd and named . We don't want to use pre-defined authorization (aix.network.daemon). Is it possible to create an independent authorization for commands?... (1 Reply)
Discussion started by: coolr
1 Replies

5. Shell Programming and Scripting

User defined functions in awk

Hi; Is der ne to to use user defined functions for the values in awk find $1 -type f -ls | nawk '{{print "|"$3"|"$5"|"$6"|"$8"|"$9"|"$10"|"} for(i=11;i<=NF;i++){printf("%s",$i)}}' In above command i want to append some values returned by user functions on line. thnks; ajay (1 Reply)
Discussion started by: ajaypadvi
1 Replies

6. Shell Programming and Scripting

Unable to add user defined variable

Hi, I have a user defined variable _TIME1=xxx I am using awk command for pattern matching. cat $_LOCATION/catalina.txt | awk '/^`$_TIME1`:??:??/' It not taking the value of $_TIME! eg:I am using the command to get all the patter from 12:00:00 to 12:59:59 The user defined variable... (2 Replies)
Discussion started by: ahamed
2 Replies

7. Shell Programming and Scripting

Return an array of strings from user defined function in awk

Hello Friends, Is it possible to return an array from a user defined function in awk ? example: gawk ' BEGIN{} { catch_line = my_function(i) print catch_line print catch_line print catch_line } function my_function(i) { print "echo" line= "awk" line= "gawk"... (2 Replies)
Discussion started by: user_prady
2 Replies

8. Shell Programming and Scripting

awk printf for user defined variables

I am working on a SunFire 480 - uname -a gives: SunOS bsmdb02 5.9 Generic_112233-08 sun4u sparc SUNW,Sun-Fire-480R I am tyring to sum up the total size of all the directories for each running database using awk: #!/usr/bin/ksh for Database in `ps -efl | grep "ora_pmon" | grep -v grep |... (1 Reply)
Discussion started by: jabberwocky
1 Replies

9. Shell Programming and Scripting

Force Input in User Defined Variable

In a line such as: echo -n "How many days back would you like to check? "; read days How can I ensure that the user has a.) entered a number between 1-30 (not 0 or 31+) and b.) has not just hit enter (ie set it to "") and if it's entered wrong, how do I start the if statement over? I... (10 Replies)
Discussion started by: earnstaf
10 Replies

10. Shell Programming and Scripting

Replace variable with a user defined variable

I have a file that has a list of entries in a column x z z z x y z The column can have any length and any number of any strings. I need to replace each unique string with a user defined number. I can filter the unique entries out using awk '{if (NF==5) print $2}' file | uniq | nl >... (1 Reply)
Discussion started by: ce124
1 Replies
Login or Register to Ask a Question