printf with Character String


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting printf with Character String
# 1  
Old 04-24-2009
Error 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):

Code:
while read device

do

cat $device.clean |awk -F '\n' '{ printf ("%s\t%s\t%s\t%s\t%s\n", $device, substr($1,1,6), substr($1,43,5), substr($1,11,18), substr($1,30,10)) }'

done < device.txt

The issue is the first %s in the printf. I want the output to contain the variable name (from device.txt). However, it writes $device.

Thanks, in advance!
# 2  
Old 04-24-2009
Code:
while read device
do
    awk -F '\n' '{ printf ("%s\t%s\t%s\t%s\t%s\n", myDevice, substr($1,1,6), substr($1,43,5), substr($1,11,18), substr($1,30,10)) }' myDevice="${device}" $device.clean
done < device.txt

# 3  
Old 04-24-2009
Thank you! Worked like a champ!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed searches a character string for a specified delimiter character, and returns a leading or traili

Hi, Anyone can help using SED searches a character string for a specified delimiter character, and returns a leading or trailing space/blank. Text file : "1"|"ExternalClassDEA519CF5"|"Art1" "2"|"ExternalClass563EA516C"|"Art3" "3"|"ExternalClass305ED16B8"|"Art9" ... ... ... (2 Replies)
Discussion started by: fspalero
2 Replies

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

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

How to print a string using printf?

I want to print a string say "str1 str2 str3 str4" using printf. If I try printing it using printf it is printing as follows. output ------- str1 str2 str3 str4 btw I'm working in AIX. This is my first post in this forum :) regards, rakesh (4 Replies)
Discussion started by: enigmatrix
4 Replies

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

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

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

8. Shell Programming and Scripting

repeat character with printf

It's all in the subject. I try to figure out how to repeat a character a number of time with printf. For example to draw a line in a script output. Thks (13 Replies)
Discussion started by: ripat
13 Replies

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

10. Shell Programming and Scripting

find: problems escaping printf-command string

Hi Folks! Can you help me with this find -printf command. I seem to be unable to execute the printf-command from my shell script. I'm confused: :confused: My shell script snippet looks like this: #!/bin/sh .. COMMAND="find ./* -printf '%p %m %s %u %g \n'" echo "Command: ${COMMAND}"... (1 Reply)
Discussion started by: grahamb
1 Replies
Login or Register to Ask a Question