The shell builtin command that formats and prints data. Questions that deal with printf command or broadly using printf syntax by programming language (like awk, PHP, perl, C++...)

learn more… | top users | synonyms

-5
votes
0answers
21 views

How can I avoid this line from getting printed? [on hold]

I have this piece of code that I developed just to address a problem that I have in another large program that I am developing. My goal is simple, When the user enter quit. I will just display ...
3
votes
2answers
43 views

using printf function for repeated patterns

I have codes which append the patterns into text file w.r.t. the user's input as follows; echo -n "WHICH STATIONS?" read station awk -v input="$station" ' BEGIN { n = split(tolower(input), ...
0
votes
1answer
31 views

Display non-printable parameters in running processes

ps aux displays commands running on the server, but it does not display non-printable parameters in any sort of way that is useful. mycmd.sh: #!/bin/bash while true do sleep 5 done ...
5
votes
3answers
411 views

Weird float rounding behavior with printf

I've read some answers on this site and found the printf rounding desirable. However when I used it in practice, a subtle bug led me to the following behavior: $ echo 197.5 | xargs printf '%.0f' 198 ...
2
votes
4answers
223 views

How do I append multiple lines involving variables to the end of a bash script?

I would like to add: function ps_mem { python /home/vagrant/ps_mem/ps_mem.py -p $@ } To the end of ~/.bashrc from the command-line. I have tried using: printf "function ps_mem {\n python ...
5
votes
1answer
648 views

Where is `scanf` implementation source code?

I want to find C source code for scanf implementation on Linux machine. Googling to find scanf implementation does not tell me the way to find it. I tried to find that source code from gcc source tree ...
3
votes
3answers
224 views

How do I use a multi-character delimiter for array expansion in bash?

I'm reducing the question to (I believe) the simplest case. Let's say I have a script myscript.sh with the following contents: #!/bin/bash IFS='%20' echo "$*" If I run the command as follows, the ...
2
votes
4answers
170 views

Human-readable filesize with find's printf

I am writing a simple helper function to show a specific folder in the format I want: find . -maxdepth 1 -not -name "." -printf '[%TY-%Tm-%Td]\t%s\t%f\n' | sort -n However I want to show the ...
0
votes
0answers
42 views

Logging to a file in tabular format

I am trying to print something into a file through the script which will be sent as an attachment to the mail. Now what i am willing is to preserve a proper formatting within the file. Can i print ...
2
votes
3answers
48 views

Print columns of data that wrap internally

I am printing some monitoring data from a log as such: printf " %10s %5s %25s %15s %15s %s${txtrst}\n" $date $time $metric $status $current_criticality "$failure" I would like the last ...
0
votes
1answer
43 views

Need help understanding this cpu utilization script

Need help in understanding the below Script : HEADER='CPU pctUser pctNice pctSystem pctIowait pctIdle' HEADERIZE="BEGIN {print \"$HEADER\"}" PRINTF='{printf "%-3s %9s %9s %9s %9s ...
3
votes
1answer
625 views

convert timestamp to epoch with bash builtin printf

While researching a different approach to convert a timestamp like 2015-09-08 11:07:35 to 1441703255 I found a lot of pretty commonly found solutions use e.g. date +%s --date ...
4
votes
1answer
137 views

bash prevent printf from interrupting another printf

I have the following test script. #!/bin/bash function foo { printf "Test line break: $1\nafter line break\n\n" } for VARIABLE in {1..30} do foo $VARIABLE & done wait Now I executed the ...
-1
votes
1answer
107 views

How to rename or delete an alias permanently from system?

I have created one alias in my system, printf "%s\n" "alias shh='sqlplus hfdora/hfdora@hfd2" >> ~/.kshrc Now, I want to change the alias name from shh2 to shh Also, how to delete other ...
4
votes
3answers
397 views

How to print control characters with escape sequences?

I have defined the color red using tput red=$(tput setaf 1) to colorize warnings in my program. This works fine: printf '%sfail\n' "$red" # prints 'fail' in red But on one occasion I would like ...
1
vote
2answers
105 views

Unable to store ascii value in a variable

I am trying to convert hex to decimal to ascii and store it in a variable. I am using the following code. HEX=30 DEC=`printf "%d\n" 0x${HEX}` echo "$DEC" ASC=`printf \\$(printf '%03o' $DEC)` echo ...
0
votes
1answer
117 views

print characters repetitively using astreisk symbol in bash [duplicate]

print '*' * 80 This python snippet prints 80 astreisks, which i use often as delimiters in log file. How can i do it in bash script using operator overloading?
3
votes
3answers
48 views

printing a series of characters

My ultimate goal here is to generate a block of text that can be used to test out various fonts at a terminal. I want to generate the file as basically an ascii chart. A series of characters from 1 ...
1
vote
2answers
69 views

Alignment when printing a series of records

How can I resolve the problem of the tables going to the right. I just want it to be shown under 1. Here's my script with START added. The alignment has gone wonky now: while IFS="," read f1 f2 f3 ...
4
votes
3answers
1k views

How to use printf and %s when there are color codes?

I had these lines: if [[ $# -eq 0 ]]; then printf "$fail_color Error - Function: $function, Line: $line_number \n" printf "do_test: Third parameter missing - expected result\n" exit 1 fi This ...
1
vote
2answers
223 views

bash - Why does type -a execute “$(printf 'a')” and is it possible to get back the original alias? [duplicate]

After i ask this question 2 days ago, i decided to make this alias in my ~/.bashrc: alias catvu="LC_ALL=C sed \"$(printf 's/[^\t -\176\200-\377]/^&/g')\"|LC_ALL=C tr '\0-\10\13-\37\177' ...
1
vote
2answers
150 views

Format Column Width with Printf

I have searched online for 10 hours now and tried various codes, but still need help. I am attempting to merge three files using 'paste' and 'awk'. However, the columns are not adjusting to the ...
2
votes
1answer
1k views

Printf formatting with variable format - what does this var reference?

I am experimenting with printf formatting in bash and I ran across this piece of code: #/bin/bash divider=============================== divider=$divider$divider header="\n %-15s %8s %10s %11s\n" ...
9
votes
3answers
1k views

Writing a character N times using the printf command

I found the following command to repeat a character in Linux: printf 'H%.0s' {1..5000} > H.txt I want, for example, H to repeat 5000 times. What does %.0s mean here?
3
votes
0answers
78 views

find printf switch POSIX alternative

I'd like to rewrite these 2 command so they will use only POSIX-complaint switches: find "$TARGET_DIR" -maxdepth 1 -type d -printf '(DIR) %f\n' find "$TARGET_DIR" -maxdepth 1 -type f -printf '%s %f ...
2
votes
1answer
300 views

How do I print hyphens if a variable is empty?

I am collecting windows system information using wmic on Linux system. For that purpose, I make various wmic calls and their output is stored in some variables. After required data is collected, I ...
0
votes
1answer
427 views

How do I escape quotes for json conversion in Bash

I have a file which I need to convert to a json string and it needs to be done via bash. My test file looks like 1 2 ' 3 4 Here is what's happening $ LOG=$(cat file) $ printf '{"log": "%q"}\n' ...
3
votes
1answer
1k views

How to use printf to show the results of a calculation?

My program is #!/bin/sh printf 'Please enter salary amount: ' read salary TAX="echo $salary * 0.15 | bc" NET="echo $salary + $TAX | bc" printf "Net Salary= %.2f" $NET printf "\n" But whatever I ...
0
votes
2answers
295 views

How to printf an exclamation point in tcsh?

What's the best way to printf an exclamation point in tcsh? It seems like using single or double quotes has no effect on escaping the exclamation point; and using a backspace does work, but then it ...
5
votes
3answers
680 views

How do I capture stdin to a variable without stripping any trailing newlines?

In a shell script... How do I capture stdin to a variable without stripping any trailing newlines? Right now I have tried: var=`cat` var=`tee` var=$(tee) In all cases $var will not have the ...
1
vote
3answers
327 views

Using a command's output in printf in ksh

I am trying to perform the following : printf "Are you sure you want to copy %s (y/n) ? (file bigger than 10 MB) " "$0" It works fine, nevertheless, I would like to display the actual size of my ...
4
votes
5answers
3k views

Add leading characters infront of string using printf or echo

How can I add leading characters to fill a string to a certain length? Assume I want to add zeroes infront of a string, if said string is shorter than 20 characters: printf '%020s\n' "$line" ...
4
votes
2answers
556 views

Thousands separator in printf in zsh

I have been trying to define a thousands separator in printf for a while now and I have discovered that zsh has some problems with it. In bash I can use something like: $ printf "%'d\n" 1234567890 ...
7
votes
3answers
11k views

Awk printf number in width and round it up

I need to printf a number out, but with given width and rounded (with awk!) %10s I have this and somehow I need to connect the %d but everything I do, ends up with too much parametres for awk ...
2
votes
2answers
647 views

printf output running over into next line

I have created Bash SS in vi. My output is running over into the line below it. I am new to UNIX so I'm trying to learn. My output is displayed using printf. printf "%-15s %15s %15s %2d\n %2s " ...
4
votes
1answer
361 views

One liner for padding columns (similar to printf) of output from the previous pipe command?

I'd normally use printf to pad each cell with enough whitespace to maintain header position over its respective column. Ultimately I can just iterate over each line of the output and do enough cut ...
3
votes
0answers
308 views

How to create a bash command which creates output that exactly mimics a source text file that can be pasted into bash shell where histexpand is on?

The goal is to create a bash one line command that can be copied and pasted - for instance published on a web forum - and then patches files. See it as a way to distribute a patch file by copy and ...
5
votes
2answers
831 views

Why does Bash history not record this command?

The command printf "#!/bin/bash\n" > t generates an error(bash: !/bin/bash\n": event not found), but hitting the up key does not recall the command so that I can modify it. Why is that? (What I ...
2
votes
1answer
186 views

How to compute a thumbnail filename from the shell?

I started noticing some time ago in Xfce4 that when I sent some file to the Trash, tumbler (the Xfce4 thumbnailer) would cause very high I/O load for quite some time. Upon investigating the issue, I ...
1
vote
3answers
194 views

How to print to the left and right of already printed output

I have a command say printf "%5s1" and its output is: 1 How can I print digit 2 to left and digit 3 to right of already printed 1. So, the output looks like below: 213 So, there is one ...
4
votes
2answers
2k views

Overwriting line in unix shell after printing

How can I modify a line after printing it in unix shell.We can delete the line and write again by using \r as shown below,but how can I overwrite on existing line and not delete it completely and get ...
3
votes
4answers
4k views

Pad a number with a zero

I'm trying to pad zero to a number a=010 printf "%04d" $a 0008 I need the "output" as "0010" it's converting the value..can any one help on this ?? and even tried with typeset a=010 typeset -RZ2 ...
2
votes
1answer
5k views

i/o format with printf in bash-shell

I am trying to output the results of my shell script using printf. To keep the results aligned in column, I am using tab as: printf "%s\t %s\t %s\t %s\t %s\t %s\t %s\n" $i $est $var_m $jk $mst $var_M ...
4
votes
2answers
1k views

Where can I find some good documentation for bash printf?

man printf, info printf and printf --help are all pretty useless to me; I want an explanation of the FORMAT options. For example, I could use: printf '%s\n' foo bar and get output: foo bar From ...
3
votes
1answer
1k views

Setting the column position in printf

How to fix the positions of output string in shell using printf? For example ,if I want to print text TEXT-A at position starting from third position from left and one more text TEXT-B at eleventh ...
3
votes
1answer
130 views

Print arguments to printf in no particular order

How can I refer back to arguments to coreutil's printf in no particular order? This is what I want to avoid: $ printf '%s %s %s %s %s %s %s' a c a c b c a a c a c b c a This is what I would like ...
2
votes
2answers
2k views

bash script , echo output in box

I have created this Function , which generate below output on terminal , but this function seems complex, So I posted here for any improvement or for same alternate solution. #!/bin/bash function ...
276
votes
5answers
101k views

Why is printf better than echo?

I have heard that printf is better than echo. I can recall only one instance from my experience where I had to use printf because echo didn't work for feeding some text into some program on RHEL 5.8 ...
1
vote
1answer
457 views

Why does printf ignore the IFS when printing out the result of my script?

This is a follow up to a question posted on SO. I've written a script called except which should print all filenames / directories except the ones given like so. $ ls a b c d $ rm $(except b d) $ ls ...
1
vote
1answer
520 views

Pretty print in VIM?

I have few lines that each of them are colon separated values, abc:def:3 Now I could do the reformat with printf plus IFS variable, while read line; do IFS=":" printf "%-20s %-16s %5d\n" ...