String formatting using awk printf


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting String formatting using awk printf
# 1  
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
# 2  
Old 04-09-2010
set the width of first field to some max.
eg.
Code:
awk '{printf "# %10-s %9s %18s\n", $2,$3,$4}'

Please try.
# 3  
Old 04-09-2010
Ya, its working fine. Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

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