awk printf formatting using string format specifier.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk printf formatting using string format specifier.
# 1  
Old 08-20-2008
awk printf formatting using string format specifier.

Hi all,

My simple AWK code does
Code:
C = A - B

If C can be a negative number, how awk printf formating handles it using string format specifier.

Thanks in advance
Kanu
Smilie
# 2  
Old 08-20-2008
%d would be the normal one to use for whole integers.

Code:
awk 'BEGIN { printf("%d\n",C) }'

# 3  
Old 08-21-2008
%d option is not working

My code:
Code:
#!/bin/awk -f
BEGIN {

# Enter the numbers
        a = 2;
        b = 3;
        c = a - b;

                printf ("The first number is%d\n",a);
                printf ("The second number is%d\n",b);
                printf ("The result is%d\n",c);


exit;
}


result:
Code:
The first number is2
The second number is3
The result is-1

i want there the whole number using format specifier...Smilie
# 4  
Old 08-21-2008
Quote:
i want there the whole number using format specifier
means value of C should come around to be 1 (absolute value)
# 5  
Old 08-21-2008
why not use absolute value function?

If you want the absolute value of the number you want to print,
why don't you store it as an absolute value in the first place,
i.e., c = abs (a - b)
note that you should include the external library stdlib.h for using this function with integer values.
when you printf c you'll get a nonsigned number
else you can incorporate abs in the printf command

printf ("The result is %d\n", abs(c))

something like that
# 6  
Old 08-21-2008
OK! thanks budd! how to do it with a format specifier??
# 7  
Old 08-21-2008
Try %u it is the unsigned integer format specifier used in C programming just like printf command.
I hope it will work with awk.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Format specifier for sscanf() in C

Hello, I have formatted lines delimited by colon ":", and I need to parse the line into two parts with sscanf() with format specifiers. infile.txt: Sample Name: sample1 SNPs : 91 MNPs : 1 Insertions : 5 Deletions ... (13 Replies)
Discussion started by: yifangt
13 Replies

2. Shell Programming and Scripting

Unable to match string within awk printf

Hi All I am working to process txt file into csv commo separated. Input.txt 1,2,asdf,34sdsd,120,haahha2 2,2,wewedf,45sdsd,130,haahha ..... .... Errorcode.txt 120 130 140 myawk.awk code: { BEGIN{ HEADER="f1,f2,f3,f4,f5,f6" (4 Replies)
Discussion started by: krsnadasa
4 Replies

3. Shell Programming and Scripting

printf format with awk

Hello Here is an easy one Data file 12345 (tab) Some text (tab) 53.432 23456 (tab) Some longer text (tab) 933.422 34567 (tab) Some different text (tab) 29.309 I need to awk these three tab-delimited columns so that the first two are unchanged (unformatted) and the third shows two decimal... (1 Reply)
Discussion started by: palex
1 Replies

4. Shell Programming and Scripting

awk printf format in columns

Hi all, i have written this script: awk -F';' ' BEGIN { printf "\n" printf "\n" printf "\n" printf "----------------------------------------------\n" print " For test " printf "----------------------------------------------\n" test_200 = 0 test_300 = 0 test_500 = 0 test_1000 = 0... (11 Replies)
Discussion started by: arrals_vl
11 Replies

5. UNIX for Dummies Questions & Answers

awk printf in required format

Hi my awk variable $0 contains the below data Input file 000001 The Data 000002* The line 2 000003* The line3 output file Req 000001* The Data 000002** The line 2 000003** The line3 one * at column seven needs to be appended to the input lines, (5 Replies)
Discussion started by: rakeshkumar
5 Replies

6. Shell Programming and Scripting

String formatting using awk printf

Hi Friends, I am trying to insert lines of the below format in a file: # x3a4914 Joe 2010/04/07 # seh Lane 2010/04/07 # IN01379 Larry 2010/04/07 I am formatting the strings as follows using awk printf: awk 'printf "# %s %9s %18s\n", $2,$3,$4}' ... (2 Replies)
Discussion started by: sugan
2 Replies

7. Shell Programming and Scripting

String formatting using AWK

Hi, I need to insert a line at a particular line number. I am using the below code: sed $REV_LINO_NO" i\\ # $CURRENT_DATE $NAME Changed pwd for cindy\'s id" file > file1 This code works, but the formatting is not as I expected. For example, I get lines as shown below... (2 Replies)
Discussion started by: sugan
2 Replies

8. Shell Programming and Scripting

Explanation for printf string in awk

hi all can any one help me to understand this bdf -t vfxs | awk '/\//{printf("%-30s%-10s%-10s%-10s%-5s%-10s\n",$1,$2,$3,$4,$5,$6)}' i want to understand the numbers %-30S% (4 Replies)
Discussion started by: maxim42
4 Replies

9. Shell Programming and Scripting

Help formatting a string. Something like printf?

Hi I'm having a problem with converting a file: ID X 1 7 1 8 1 3 2 5 2 7 2 2 To something like this: ID X1 X2 X3 1 7 8 3 2 5 7 2 I've tried the following loop: for i in `cat tst.csv| awk -F "," '{print $1}'| uniq`;do grep -h $i... (4 Replies)
Discussion started by: flotsam
4 Replies

10. Shell Programming and Scripting

How to format the output using float in awk{printf}

Hi I'm using awk to manipulate the data in the 6th field of the file xxx_yyy.hrv. The sample data that is available in this field is given below 220731.7100000000000000 When i tried using this command cat xxx_yyy.hrv | awk '{printf("%23.16f\n",$6*-1)}' I get the output as... (4 Replies)
Discussion started by: angelarosh
4 Replies
Login or Register to Ask a Question