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

0
votes
0answers
10 views

running command in Mobax Terminal are there plugins that are needed

while read CTFlist; do #next we need to create ctfs padded with zeros as a variable ctfPadded=$(printf ${ctflist}00000000) #then call rc2 key as variable rc2Key="TemporaryRC2Key1" #next create a ...
5
votes
2answers
892 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 ...
0
votes
2answers
59 views

format command output with variable and fixed space

How can I achieve this best maintaining the hierarchy on the left and aligning the size value on the right? Also, at the same time I'd like to round the size to one or two digits. I tried with printf ...
3
votes
2answers
59 views

use printf to format output of an array

I have an array that contains details about each NIC. Each array index consists of three space-separated values. I would like to have a nice table as output. Is there a way to assign formatting ...
6
votes
3answers
26k views

print output to 3 separate columns

MYPATH=/var/www/html/error_logs/ TOTALFILE=$(ls $MYPATH* | wc -l) FILETIME=$(stat --format=%y $MYPATH* | head -5 | cut -d'.' -f1) FILE=$(ls -1tcr $MYPATH* | head -5 | rev | cut -d/ -f1 | rev) ...
0
votes
1answer
30 views

Printing Space Between Strings

#!/bin/bash ARRAY="185.18.207.66 31.18.212.113" result="" for i in $ARRAY do result=$(printf '%s %s' "$result" "$i" "checked") done paste <(printf "%s\n" $result) I am trying to print ...
-1
votes
3answers
40 views

Why am I getting an error “print_unicode: line 9: printf: missing unicode digit for \u Unicode 0187” with this script

I have written them following shell script to view what the Unicode characters look like on my terminal. #!/bin/bash X=0 while [ $X -lt 65536 ]; do HEX=`bc <<< "obase=16; $X"` ...
3
votes
2answers
386 views

Printf printing only a single line

I'm trying to get the output of env in a shell variable and print it. #!/bin/sh ENV_BEFORE=$(env) printf $ENV_BEFORE As a result a single variable from the env output gets printed. When using echo ...
1
vote
1answer
56 views

Bash script how to accept single or double digit number and make it 2 digit

I have a following simple extract from my script which is strangely stuck up for a particular inputs. echo -e "Enter the Day of the Month : \c " read input_day printf -v input_day '%02d' ...
3
votes
1answer
41 views

How can I print a variable with padded center alignment?

How can I print $myvar padded so that it is in the center of the terminal, and to either side are = to the edge of the screen?
289
votes
5answers
109k 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 ...
0
votes
0answers
31 views

Include text from other file in screen printf

The original code of main.sh: screen -S myscreenname -p 0 -X stuff "`printf "myabcdefg"`" I want to put only abcdefg (without my) in other file. Below codes works just fine but I need the text.txt ...
9
votes
3answers
2k 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
2answers
49 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), ...
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 ...
3
votes
3answers
428 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 ...
4
votes
2answers
607 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 ...
0
votes
1answer
37 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 ...
6
votes
3answers
487 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
283 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 ...
6
votes
1answer
1k 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 ...
2
votes
4answers
288 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
51 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
52 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 ...
4
votes
3answers
454 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 ...
0
votes
1answer
51 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
1k 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
148 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
166 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 ...
1
vote
2answers
128 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
177 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
51 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
71 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
2k 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
281 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' ...
7
votes
3answers
13k 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 ...
1
vote
2answers
193 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 ...
3
votes
1answer
2k 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" ...
3
votes
0answers
93 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
314 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
559 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
2k 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 ...
4
votes
3answers
2k views

Prevent watch breaking colors

I'm altering scripts at work that monitor log files to single out certain items and colorize them. The final output is a list of 6-digit numbers in several columns. I've been able to add a ~ to the ...
0
votes
2answers
333 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
894 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
369 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
1answer
398 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 ...
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" ...
1
vote
1answer
1k views

Where to find printf formatting reference?

Where is the offline reference of the format specifiers (e.g., %i, %s) which are supported by the Bash/system printf command? I've looked in help printf, man 1 printf and man 3 printf without luck. ...
2
votes
2answers
730 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 " ...