Questions tagged [printf]

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

Filter by
Sorted by
Tagged with
0 votes
0 answers
59 views

Rounding 2.555500000000000104805053 with printf -> 2.555 or 2.556? [duplicate]

I tried printf "%.3f\n" 2.555500000000000104805053 printf "%.3f\n" 2.555500000000000104805054 the result was 2.555 2.556 same with printf "%.3f\n" 3....
  • 157
2 votes
1 answer
103 views

printf "%.3f" ${variable with newlines} - error with \n

Like HERE I have a file.csv with numbers in quotes: "0.2" "0.3339" "0.111111" To round the number (3 decimals) this solutions works great: printf "%.03f\n" $(...
  • 157
5 votes
3 answers
488 views

Rounding many values in a csv to 3 decimals (printf ?)

I have a paste command like this paste -d , file1.csv file2.csv file3.csv And file2.csv contains numbers like this 0.2 0.3339 0.111111 I want the values in file2.csv having 3 decimals like this: 0....
  • 157
0 votes
1 answer
42 views

What format to pass to printf to convert seconds to hh:mm:ss?

I'm looking at an instruction of the form: $ printf [format] $elapsed with $elapsed expressed in seconds. I want it to output 'hh:mm:ss'. I couldn't figure out format from the man. Would someone ...
  • 561
0 votes
1 answer
38 views

first character goes to last when formatting XYZ columns with printf awk

I have a file with 7 columns and I want to get the first 4 and format them with awk. When I use awk without formatting it works however when I put it with printf it happens that the first character of ...
1 vote
4 answers
155 views

How to find out if pipe is broken?

I have a POSIX shell script which has its standard output 1 redirected to a pipe. At some point of the script execution, the pipe will break and I'd like to find out (in my shell script) when that ...
  • 2,260
0 votes
1 answer
30 views

Loop to Monitor Line Count Growth Using wc -l

I've written a short bash script to monitor the line count growth (wc -l) in a file which is receiving results from a loop. Thus: printf "Name of file to monitor\n" read file printf "...
0 votes
1 answer
43 views

Why I can pipe echo into bc, but I can't do the same with printf?

I can pipe echo into bc. But I cannot do the same with "printf": it gives syntax error. ❯ echo "100-5" | bc 95 ❯ printf "%s" "100-5" | bc (standard_in) 1: ...
0 votes
3 answers
181 views

How to convert seconds to hh:mm:ss.ms when getting difference between 2 times

I performed a calculation that converts current timestamp giving milliseconds as well using perl command such as below: perl -MTime::HiRes=time -MPOSIX=strftime -e ' $now = int(time() * 1000); printf &...
0 votes
2 answers
44 views

Shell function to center its first argument

In the nano text editor, I can pipe the selection into a command, and I quite often need to center text, so I came up with the following code center() { str=$1 # Strip leading and trailing ...
user avatar
3 votes
2 answers
818 views

How can I write in multiple files at once using 'cat'?

This question is kind of related to my previous question. This is the output of tree: [xyz@localhost Semester1]$ tree . ├── Eng │   ├── credits │   ├── links │   └── notes ├── IT │   ├── credits │   ├─...
2 votes
2 answers
116 views

Split string into lines, then frame output

I've been working on a way to take a string, set a max character width then split the lines near or at the limit by words and then frame the whole thing. I managed to do it with printf an array and a ...
  • 131
1 vote
1 answer
332 views

Linux C | How can we redirect printf to syslog?

We are using printf till now and the output was coming to stdio. Now we want to remove it from stdio and put in syslog. I have a big code base and so replacing all the printf will not be a good ...
  • 11
1 vote
2 answers
381 views

How to count the number of lines per file in a directory, then create summary of number of files with n lines

I'm trying to create a summary of how many files in a directory have n number of lines in them. I'm using wc -l * | sort to print out the number of lines against the name of each file. What I'm trying ...
1 vote
1 answer
263 views

Is it safe to snprintf with output to NULL and size 0?

Is it safe to call snprintf(NULL, 0, "…", …)? I can also ask in other way: Does snprintf write the NUL char if size is 0? The example in Linux man-pages manpage printf(3) provides an example ...
  • 922
0 votes
0 answers
332 views

Writing a File with CSV Header Column and Unique Column Values in Bash

I have a bunch of csvfiles that I'm importing into a database. I'd like to get a preview of the unique values in each column to help me create the tables. I've written a script that takes a input csv ...
0 votes
1 answer
26 views

Calling printf without a connected monitor

I would like to know if when we call printf without connecting a monitor (and uninstalling any driver related to the screen), does a driver get called anyway?
0 votes
0 answers
140 views

write terminal output to file while keeping new lines [duplicate]

when I call env to see the environment variables it shows nicely with new lines: $ env SHELL=/bin/sh EDITOR=vi PWD=/home/user ... but when I want to store this to a file the new lines get ...
  • 764
15 votes
2 answers
3k views

Are there security consequences from not giving printf a format to use?

A well-formed printf usually has a format to use: $ var="Hello" $ printf '%s\n' "$var" Hello However, what could be the security implications of not providing a format? $ printf &...
user avatar
2 votes
2 answers
447 views

How to format timestamp field with AWK/GAWK

I am processing a CSV file with GAWK that has a field with a timestamp formatted like this "18-APR-22 11:00:00". I would like to format the date like this in the output of awk "2022-04-...
2 votes
1 answer
198 views

How to avoid Bash printf error when printing certain hexa values: Input/output error

I'm using the command printf of bash on Ubuntu under WSL. I'm trying to use this documented syntax \xHH byte with hexadecimal value HH (1 to 2 digits) This works for printf "\x0A" (...
  • 355
4 votes
2 answers
989 views

Printf formatting a ZSH variable without forking?

For my powerlevel10k custom prompt, I currently have this function to display the seconds since the epoch, comma separated. I display it under the current time so I always have a cue to remember ...
  • 308
-1 votes
1 answer
503 views

AWK command ignores adding newline ending "\n" for blank line and print in one single line

When I use the below command, it adds \n to all the lines but skips for a blank line. awk 'NF {sub(/\r/, ""); printf "%s\\n",$0;}' Example: Input: George Washington, 1789-1797 ...
-1 votes
4 answers
509 views

Easy way to output a file and change the implicit newlines ($) in the file into literal (\n)

I have a file with a number of invisible Newlines. In Vim it's shown as $ (can be seen with :set list). I had a hard time to convert them to an output which is represented as a one line string with ...
0 votes
3 answers
204 views

Set printf dynamic format for trailing whitespaces

Any standard way to make the '%-6s' format for printf dynamic? function justify { echo "$(printf '%-6s' $1)" } I tried inserting variable into the format but I'm getting Error processing ...
  • 3
0 votes
1 answer
122 views

Should echo be aliased to printf?

I'm not quite versed on the terminal, but the few things I've known and read lead me to believe that printf is much more powerful and confortable than echo but work for similar (if not same) purposes. ...
0 votes
2 answers
209 views

Average of lines with different conditions at header and tail of files

Edit: edited it for clarity and made the example files minimal and more reproducible for easier help. Thanks! I have files that gave 1000+lines. Each file is formatted with the same amount of lines. ...
0 votes
2 answers
109 views

generate a report of sales with awk

I trying to create a report of sales for each store using an awk script. The dataset is in csv format and there are 45 stores. Example of the data is something like this: Store,Store_name,Date,Year,...
  • 25
0 votes
4 answers
376 views

How can I output a txt file, that has part of filename followed by path of file for all files in a folder, one line per file?

I have the following files /folder/abc1.txt.gz /folder/abc2.txt.gz /folder/abc3.txt.gz I would like to make a txt file with the following abc1 /folder/abc1.txt.gz abc2 /folder/abc2.txt.gz abc3 /...
0 votes
1 answer
71 views

When storing text on a USB drive, how do I make cat not hang?

Warning: I used these commands on a drive that had nothing on it (/dev/sdb). Do not attempt this on a drive with anything important on it. I was experimenting some, and I discovered that the ...
  • 341
1 vote
2 answers
311 views

shortcut command for printf with a specific format string

I want to use a shortcut command for printf with a specific format string, and came up with the following. local PF="printf %s\n" $PF "Some Text" It does the job, but wonder if ...
  • 101
0 votes
5 answers
148 views

How can I separate numbers formatted as string (without space) into numbers with spaces based on the number of digits after decimal

I have a text file from a simulation where the numbers are without any spaces between them. An example is given below. I want to separate the numbers based on the decimal point number (13). 5....
1 vote
3 answers
716 views

Center text with printf

printf "%*s\n" $(((${#fname}+$COLUMNS)/2)) "$fname" I get this error: line 9: (7+)/2: syntax error: operand expected (error token is ")/2") This works in the terminal ...
0 votes
1 answer
253 views

Using printf in bash

When using printf in bash, does one always have to supply a format string as here. printf "" "V01 Oct 2021" Or would printf "V01 Oct 2021" still be ok?
  • 189
1 vote
2 answers
190 views

color codes messing up table formatting of printf to xterm

My script-in-progress displays a multi-column colorized table, but the color-codes are interfering with the formatting. The color codes cannot be moved to the format string because the coloring of ...
  • 11
1 vote
3 answers
371 views

Why isn't zsh printf respecting octal notation?

sh -c 'printf "%d " 024' bash -c 'printf "%d " 024' zsh -c 'printf "%d" 024' The above outputs 20 20 24. Why is zsh not respecting octal notation? Is there a way to ...
  • 543
1 vote
1 answer
92 views

TOP command combined with printf

I have a simple while true statement: #!/bin/bash while true; do printf -- '-%.0s' {1..100}; echo "" | top -l 1 >> file.txt sleep 10 done The issue I am facing is that the ...
4 votes
2 answers
570 views

print awk array and replace special characters with comma

I hope the title explains this correctly. I am currently trying to print out an array after counting unique values from a spreadsheet. My awk command works correctly: awk -F"," 'NR>1{col[$...
4 votes
3 answers
828 views

How to align the output generated in a shell "for" loop by columns

I'm trying to align output from a bash for loop. Currently, I'm getting output from my loop that looks like so: Directory: /some/long/directory/path Remote: some-remote Directory: /some/dir/path ...
  • 117
-1 votes
1 answer
973 views

printf invalid option -- 'd'

I am doing printf '%s\n' "$@" and calling the function using pfm "-d DIR" "--directory=DIR" I get errors, namely: bash: invalid option -- 'd' bash: invalid option -- ' ' ...
  • 341
1 vote
2 answers
352 views

Best way to pad stdin to desired length with NULL

I have the following requirement. I have an input stream that I need to truncate to a certain fixed length of bytes. I do not know the length of the input stream beforehand. If the stream is less in ...
10 votes
3 answers
25k views

How to do formatted printing with jq?

jq has built-in ability to convert numbers to string or concatenate strings. How can I format strings inside jq similar to printf like padding (%4s). For example, how can I force number to occupy 10 ...
-1 votes
1 answer
2k views

how to use echo or printf with xargs to iterate over a list from a file?

In poking about with xargs, as here: nicholas@gondor:~/x$ nicholas@gondor:~/x$ xargs echo < list.txt 1 2 3 nicholas@gondor:~/x$ nicholas@gondor:~/x$ cat list.txt 1 2 3 nicholas@gondor:~/x$ ...
-4 votes
1 answer
2k views

How to printf ascii characters with hex or dec inside /bin/sh -c '...'?

/bin/sh would be dash what is the correct syntax for printf to display certain ascii character using hex or dex code in dash? let's say i want to printf a dollar sign ($). which hex or dec code should ...
user avatar
0 votes
0 answers
28 views

Do printf(1) implementations differ in how they handle argument overflow? [duplicate]

Every instance I've found of printf(1) (that is, the command-line version; as opposed to printf(3), the C library) repeats its format-string to consume more when presented with too many arguments, ...
0 votes
3 answers
868 views

Using printf with AWK to format my output correctly

I'm trying to format some output using printf with awk. Basically, I want the final format to look like this (everything is tab-separated.) chr10 100000624 100000625 10:100000625_A_G ...
  • 55
5 votes
3 answers
1k views

Using `printf` to output chars given some ASCII numbers

I've been trying to make printf output some chars, given their ASCII numbers (in hex)... something like this: #!/bin/bash hexchars () { printf '\x%s' $@ ;} hexchars 48 65 6c 6c 6f Expected output: ...
user avatar
2 votes
2 answers
1k views

/bin/sh evaluates string while Bash does not

When running the script below, I get two different outputs, depending on whether or not the shell used is sh or bash: regex(){ echo 's/\(.* \)\(!\{0,1\}\)has(/\1\2MOCK_has(/g' } replace_builtins(...
  • 1,060
0 votes
1 answer
102 views

write to file with echo/printf [duplicate]

How to write the following to a file printf "*\n!.gitignore\n" > $datadir/.gitignore -bash: !.gitignore\n: event not found
  • 1,699
1 vote
2 answers
119 views

ksh88 AIX merge two files based on partial string match in one column

Using ksh88 on AIX machine, I've tried a whole slew of things that are ultimately not working. File_A has 2 columns without headers: the user group that created the directory in column 1, and the full ...
  • 15

1
2 3 4 5 6