Sponsored Content
Top Forums Shell Programming and Scripting String formatting using awk printf Post 302411621 by sugan on Friday 9th of April 2010 03:37:49 AM
Old 04-09-2010
String formatting using awk printf

Hi Friends,
I am trying to insert lines of the below format in a file:
Code:
# 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:
Code:
awk 'printf "# %s %9s %18s\n", $2,$3,$4}'

The above code does not work when the length of feild 1 varies.
Example, I get results as shown below:
Code:
# x3a4914       Joe       2010/04/07
# seh      Lane       2010/04/07
# IN01379     Larry       2010/04/07

Please let me know how to modify the prinf in awk to achieve this.

Thanks,
Sugan
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk printf formatting using string format specifier.

Hi all, My simple AWK code does C = A - B If C can be a negative number, how awk printf formating handles it using string format specifier. Thanks in advance Kanu :confused: (9 Replies)
Discussion started by: kanu_pathak
9 Replies

2. Shell Programming and Scripting

printf with Character String

I am trying to use printf with a character string that is used within a do loop. The problem is that while in the loop, the printf prints the variable name instead of the value. The do loop calls the variable name from a text file (called device.txt): while read device do cat $device.clean... (2 Replies)
Discussion started by: dleblanc67
2 Replies

3. 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

4. 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

5. UNIX for Dummies Questions & Answers

printf formatting

Is there a way to make these 2 numbers - $482477.37 and $1875000.00 look like $482,477.37 and $1,875,000.00 with printf? (4 Replies)
Discussion started by: nickg
4 Replies

6. 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

7. Shell Programming and Scripting

Text Formatting using printf[ksh]

Hi All, I am attempting to create a fixed length tilde delimited file using printf. The variables are initialized to fixed length blank spaces a=' ' b=' ' c=' ' d=' ' Sometimes the variable might contain values and sometimes they are... (5 Replies)
Discussion started by: angie1234
5 Replies

8. 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

9. 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

10. Shell Programming and Scripting

Printf padded string

Is possible to print padded string in printf? Example echo 1 | awk '{printf("%03d\n", $1)}' 001I want S1 S11 S2 S21to be padded as: S01 S11 S02 S21Thanks! (26 Replies)
Discussion started by: yifangt
26 Replies
inttypes(5)							File Formats Manual						       inttypes(5)

NAME
inttypes - fixed size integer data types SYNOPSIS
DESCRIPTION
This header file defines integer data types of various sizes. By using the data types defined in this header file, developers can be assured that the data types will have the same properties and behavior on different systems. Since not all implementations are required to support all of the integer sizes defined in this manual page, the proper way to see if a par- ticular size of an integer is supported on the current implementation is to test the symbol that defines its maximum value. For example, if tests false, then that implementation does not support 64-bit unsigned signed integers. The header includes header. This header file defines the following data type. structure type that is the type of the value returned by the function The following macros can be used as formatting options with the family of functions (see printf(3S)). These macros are used to select the correct formatting option for the integer data types defined earlier in this manual page. The following macros can be used as formatting options with the family of functions (see scanf(3S)). These macros are used to select the correct formatting option for the integer data types defined earlier in this manual page. Notes The formatting options for the family of functions all begin with whereas the formatting options for the family of functions all begin with These formatting strings may not be interchangeable. EXAMPLES
The following example shows how to use one of the print formatting options with the function. AUTHOR
was developed by HP. FILES
SEE ALSO
imaxdiv(3), printf(3S), scanf(3S), standards(5), stdint(5). inttypes(5)
All times are GMT -4. The time now is 03:06 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy