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

Why does printing an array with @ using printf in bash only print the first element?

I have an array snapshots=(1 2 3 4) When I run printf "${snapshots[*]}\n" It prints as expected 1 2 3 4 But when I run printf "${snapshots[@]}\n" It just prints 1 without a ...
geckels1's user avatar
  • 133
0 votes
1 answer
78 views

List all files in a directory, recursively, sorted by modification date

The main answer of Sort the files in the directory recursively based on last modified date gives a method to list all files in a directory, recursively, sorted by modification date: find -printf "...
Basj's user avatar
  • 2,519
0 votes
0 answers
15 views

How to preserve updating output in pipe, e.g. when output has `\r`

Whenever I pipe some output that is updating (say with \r), e.g. to a pager (less), or to colourise parts of it, the pipe seems to drop the lines that are updating. Any idea how to preserve the ...
suvayu's user avatar
  • 148
2 votes
1 answer
141 views

Print Variable containing backslashes

I have something like this: A=$(curl https://mysite.com) and the curl request returns the string \"Hello World\". When I now want to print A to the console using one of: echo "$A" ...
Cake's user avatar
  • 31
2 votes
1 answer
285 views

"ls" counterpart to "find" operator "-printf"?

The find command has a handy -printf operator that prints user-specified metadata for each found file/folder. Is there such an option for the ls command? As an alternative, I can feed the list of ...
user2153235's user avatar
-1 votes
1 answer
142 views

Why does printf not round numbers the way I expect in my bash? [duplicate]

prompt> printf "%.2f\n" 3.314 3.31 prompt> printf "%.2f\n" 3.315 3.32 This is fine, I got what I wanted. However if I use different numbers, I get this: prompt> printf &...
user3719454's user avatar
0 votes
2 answers
157 views

Why might I want to avoid such echo-like way of using `printf` when printing to stdout? [duplicate]

I wrote the following script when trying to understand how printf works: #!/usr/bin/bash printf "Give me your three preferences.? " read p1 read p2 read p3 printf "%s\n" "${...
John Smith's user avatar
0 votes
0 answers
46 views

How to pass a binary number to printf? [duplicate]

Such a silly question but I've been trying for at least 15 minutes already and it's late at night. How to use printf in bash to convert a binary number to hex? Here's what I've tried: $ printf %x 11 ...
Vorac's user avatar
  • 3,077
0 votes
2 answers
263 views

Bash and other shells $(...) remove the ending newline character [duplicate]

Why the shell structure $(...) removes the ending newline character? #!/bin/bash S='a b ' printf '%i [%s]\n' "${#S}" "$S" T=$(printf '%s' "$S") printf '%i [%s]\n' "...
Olivier Pirson's user avatar
0 votes
1 answer
22 views

how to write a commant for output of a particular group?

Reading file prod1.tpr, VERSION 2019.6 (single precision) Reading file prod1.tpr, VERSION 2019.6 (single precision) Select a group of reference atoms and a group of molecules to be ordered: Group ...
PRAMOD KUMAR's user avatar
0 votes
1 answer
96 views

Flush output continuously with xargs

The function output the size of the downloaded file and the speed of transfer separated with newline. Example: 20 #<< data 3.0M #<< speed 25 1.2M Now, it goes like this: output_data_func |...
decipher's user avatar
  • 302
2 votes
2 answers
300 views

Why does awk concatenate strings from different lines

Good day. I'm trying to run a ping6 on IPv6 addresses pulled from /etc/resolv.conf.dnsph. It seems though, as if awk is stringing the IPv6 addresses onto one line. $ grep ^nameserver /etc/resolv.conf....
Bjoern's user avatar
  • 31
5 votes
1 answer
682 views

How to use argument twice in printf in shell?

printf %s%s one two prints onetwo but I would like oneonetwotwo How can I do that?
minseong's user avatar
  • 817
0 votes
0 answers
33 views

printf and echo -n Work Differently - Why? [duplicate]

echo -n my_function() { echo -n "$@" } my_function hello world I get my desired output: hello world printf my_function() { printf "$@" } my_function hello world I get only ...
Bog's user avatar
  • 999
2 votes
1 answer
279 views

how to use read -s with printf

If I use "read -s" it deletes the existing line and brings the next prompt to the same line as mentioned below. Please let me know any solution to this error. I need to use "-s" ...
dbadmin's user avatar
  • 23
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....
R 9000's user avatar
  • 167
2 votes
1 answer
293 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" $(...
R 9000's user avatar
  • 167
5 votes
3 answers
938 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....
R 9000's user avatar
  • 167
0 votes
1 answer
237 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 ...
Erwann's user avatar
  • 677
0 votes
1 answer
49 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 ...
Mateus Augusto's user avatar
2 votes
4 answers
1k 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 ...
finefoot's user avatar
  • 3,080
0 votes
1 answer
188 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 "...
Leighton 's user avatar
1 vote
1 answer
141 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: ...
robertspierre's user avatar
0 votes
3 answers
559 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 &...
Christopher Karsten's user avatar
0 votes
2 answers
50 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
2k 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 │   ├─...
Random Person's user avatar
2 votes
2 answers
333 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 ...
Vita's user avatar
  • 151
1 vote
1 answer
910 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 ...
m Rout's user avatar
  • 11
1 vote
2 answers
971 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 ...
Doodling's user avatar
2 votes
1 answer
1k 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 ...
jiwopene's user avatar
  • 1,071
0 votes
0 answers
512 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 ...
студент001's user avatar
0 votes
1 answer
42 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?
Ariel Yael's user avatar
0 votes
0 answers
356 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 ...
DEKKER's user avatar
  • 948
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
646 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-...
user1757006's user avatar
2 votes
1 answer
273 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" (...
Sandburg's user avatar
  • 367
4 votes
2 answers
2k 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 ...
acjca2's user avatar
  • 310
-1 votes
1 answer
852 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 ...
youhoome's user avatar
-1 votes
4 answers
1k 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 ...
Dorian Gaensslen's user avatar
0 votes
3 answers
428 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 ...
resti's user avatar
  • 3
0 votes
1 answer
281 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. ...
tms8bltrn's user avatar
0 votes
2 answers
232 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. ...
Lucas Djeu's user avatar
0 votes
2 answers
180 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,...
tucomax's user avatar
  • 25
0 votes
4 answers
672 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 /...
christoforos giatzakis's user avatar
0 votes
1 answer
89 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 ...
cocomac's user avatar
  • 505
1 vote
2 answers
425 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 ...
Vera's user avatar
  • 1,223
0 votes
5 answers
189 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....
Nandi Indrajit's user avatar
1 vote
3 answers
1k 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 ...
Dark_Stoner's user avatar
0 votes
1 answer
404 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?
Vera's user avatar
  • 1,223
1 vote
2 answers
313 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 ...
Mic's user avatar
  • 11

1
2 3 4 5 6