using AWK printf with userdefine variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using AWK printf with userdefine variables
# 1  
Old 01-14-2009
using AWK printf with userdefine variables

seems simple but i've not been successfull in adding the value of a Variable to a every line of a file using AWK & printf
i've come up with the following, but it's not working.Smilie

--- mydatafile.dat

e.g. of data in file:
cau_actvty_fa_lrf.ksh
fan_soco_fa.ksh
ny_sum_lst.ksh
etc....

myvar_path=$PWD

awk -v PATH=$myvar_path '{printf("%s\nPATH",$0)}' mydatafile.dat


output is to be: value of $PWD before each file name
# 2  
Old 01-14-2009
Code:
awk -v PATH="$myvar_path"  '{printf( PATH "%s\n",$0)}' mydatafile.dat

# 3  
Old 01-15-2009
thanks...

but how would i then put a / after the value of PATH which will be between the filename and it's path Smilie it doesn't seem that PATH gets a formating expression since it's comming before the formatting expressions.... bear with me as i'm still learning the nuances of awk...Smilie

awk -v PATH="$myvar_path" '{printf( PATH "%s\n",$0)}' mydatafile.dat

i figured out another way too, but a bit more involved

awk '{printf("%s/%s\n","'"${myvar_path}"'",$1)}' mydatafile.dat

this way i was able to insert a / after the value of ${myvar_path}


Thanks again for your time.
# 4  
Old 01-15-2009
Quote:
Originally Posted by danmauer
thanks...

but how would i then put a / after the value of PATH which will be between the filename and it's path

Just put one there:

Code:
awk -v PATH="$myvar_path"  '{printf( PATH "/%s\n",$0)}' mydatafile.dat

Quote:
Smilie it doesn't seem that PATH gets a formating expression since it's comming before the formatting expressions....

PATH is part of the format string.
# 5  
Old 01-15-2009
Awesome.... i didn't realize you could put something before the %s

Thanks again, little by little , my little understanding is getting bigger.

thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Passing printf formatting parameters as variables

Hi, I haven't programed in C in a few years. I have been doing a lot of shell scripting, I.E. not really programming anything heavy. :o That said, I have a script that gives hourly usage statistics for our email server. It runs w-a-y to slow as a script for my impatience, and needs to... (7 Replies)
Discussion started by: mph
7 Replies

2. Shell Programming and Scripting

assinging the printf result to variables

Getting the below error while executing this. Able to run the below commands Individually. #!/bin/bash a=$(printf "%d\n" 0x01E); b=$(printf "%d\n" 0x01A); echo $a echo $b c=`expr $a - $b` echo $c syntax error at line 2: `a=$' unexpected (2 Replies)
Discussion started by: sai_1712
2 Replies

3. Shell Programming and Scripting

fieldwidths, printf and calculations with variables

Hello guys, I have a problem concerning the formatting when performing calculations with variables passed to awk and using fieldwidth definitions and printf. I have the following code: awk 'BEGIN{ FIELDWIDTHS = "5 3 7 5 8 8 8" }{if ($7 != "") printf"%s%-s%s%s%s%s%s\n",$1,$2,$3,$4,$5,$6,$7;... (2 Replies)
Discussion started by: origamisven
2 Replies

4. Shell Programming and Scripting

awk with printf

Hi, I am using the following code to assign a count value to a variable. But I get nothing. Do you see anything wrong here. I am new to all this. $CTR=`remsh $m -l $MACHINES{$m} -n cat $output | grep -v sent | grep \"$input\" | sort -u | awk '{print $5}'`; Upto sort - u it's... (2 Replies)
Discussion started by: nurani
2 Replies

5. Shell Programming and Scripting

AWK printf help

Target file contains short text (never more than 1 line) and filenames. The format is, e.g.,: TEXT1 filename1 TEXT2 TEXT3 filename3dddd filename3dddd TEXT4 filename4 TEXT5 filename5dddd filename5dddd filename5 where dddd is a random 4-digit whole number. Desired output: (4 Replies)
Discussion started by: uiop44
4 Replies

6. Shell Programming and Scripting

IF and awk/printf

Hi Friends, Scripting newb here. So I'm trying to create a geektool script that uses awk and printf to output certain fields from top (namely command, cpu%, rsize, pid and time, in that order). After much trial and error, I've pretty much succeeded, with one exception. Any process whose name... (3 Replies)
Discussion started by: thom.mattson
3 Replies

7. Shell Programming and Scripting

awk and printf

echo $bbsize 1.5 echo $fillpercent .95 echo $bbsize | awk '{printf "%.2f\n",$0*$fillpercent}' 2.25 echo $bbsize | awk '{printf "%.2f\n",$0*.95}' 1.42 1.42 is what I'm expecting... echo $blocksize 4096 echo $bbsize | awk '{printf "%.2f\n",$0*$blocksize}' 2.25 echo $bbsize |... (3 Replies)
Discussion started by: xgringo
3 Replies

8. Shell Programming and Scripting

printf in awk

Hi friends.. I am confused about awk printf option.. I have a comma separated file 88562848,21-JAN-08,2741079, -1188,-7433,TESTING 88558314,21-JAN-08,2741189, -1273,-7976,TESTING and there is a line in my script ( written by someone else) What is the use of command? I guess... (10 Replies)
Discussion started by: clx
10 Replies

9. Shell Programming and Scripting

How printf handles empty variables

See attached file that includes code, input and output. I am processing a colon delimited input file and building a ":" (colon) delimited output file. My printf statement prints contents of each of 20 variables and puts them into a record file. I am pushing out approximately 120 records. The... (2 Replies)
Discussion started by: Skyybugg
2 Replies

10. 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
Login or Register to Ask a Question