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
1answer
18 views

How to print ${STR1} and $STR2 into a file in bash

I need to print expressions ${STR1} and $STR2 into a file (not the content of the variables).How can I accomplish this using echo or printf?
1
vote
0answers
36 views

Portable color codes with printf? [duplicate]

There is a color codes script on GitHub suitable for sourcing, which I am modifying for my own purposes. Background I have an odd mix of environments to work in, from a Mac OS terminal, to various ...
3
votes
2answers
72 views

posting data using cURL in a script

I'm trying to write a simple alternative script for uploading files to the transfer.sh service. One of the examples on the website mentions a way of uploading multiple files in a single "session": $ ...
1
vote
2answers
34 views

Get the output in a formatted way

In a for loop, I am using the command: echo " Job_time is $i : Stopping the job " Sample output: Job_time is 6 : Stopping the job Job_time is 6.50 : Stopping the job Desired output: Job_time is ...
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 ...
3
votes
2answers
61 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 ...
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
60 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"` ...
0
votes
2answers
78 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
393 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
63 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
44 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?
0
votes
0answers
32 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 ...
3
votes
2answers
51 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
40 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
525 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
316 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 ...
3
votes
3answers
527 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
340 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
56 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
54 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
55 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
151 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
185 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
477 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
165 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
199 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
297 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
199 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" ...
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
0answers
97 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
317 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
594 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 ...
1
vote
2answers
344 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
958 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
381 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
4k views

Add leading characters in front 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 in front of a string, if said string is shorter than 20 characters: printf '%020s\n' "$line" ...
4
votes
2answers
622 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
14k 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
747 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
412 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
330 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
904 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 ...