2022 Developer Survey is open! Take survey.

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
16 votes
2 answers
2k 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
  • 38.9k
2 votes
2 answers
87 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-...
user avatar
2 votes
1 answer
61 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" (...
user avatar
  • 325
4 votes
2 answers
423 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 ...
user avatar
-2 votes
1 answer
42 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 ...
user avatar
-1 votes
4 answers
83 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 ...
user avatar
0 votes
3 answers
33 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 ...
user avatar
  • 3
0 votes
1 answer
54 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. ...
user avatar
0 votes
2 answers
89 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. ...
user avatar
0 votes
2 answers
48 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,...
user avatar
  • 25
0 votes
4 answers
62 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 /...
user avatar
0 votes
1 answer
49 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 ...
user avatar
  • 239
1 vote
2 answers
85 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 ...
user avatar
0 votes
5 answers
77 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....
user avatar
1 vote
3 answers
332 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 ...
user avatar
0 votes
1 answer
106 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?
user avatar
  • 155
1 vote
2 answers
92 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 ...
user avatar
  • 11
1 vote
4 answers
236 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 ...
user avatar
  • 454
1 vote
1 answer
42 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 ...
user avatar
4 votes
2 answers
272 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[$...
user avatar
4 votes
3 answers
282 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 ...
user avatar
  • 77
-1 votes
1 answer
278 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 -- ' ' ...
user avatar
  • 317
1 vote
2 answers
121 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 ...
user avatar
7 votes
2 answers
7k 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 ...
user avatar
-1 votes
1 answer
476 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$ ...
user avatar
-4 votes
1 answer
608 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
26 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, ...
user avatar
0 votes
3 answers
305 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 ...
user avatar
  • 55
5 votes
3 answers
609 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
1 vote
2 answers
166 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(...
user avatar
  • 921
0 votes
1 answer
40 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
user avatar
  • 1,667
1 vote
2 answers
81 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 ...
user avatar
  • 15
0 votes
3 answers
33 views

Unable to get the nth column data due to changing output of uptime command

Below is the output of uptime from Solaris where I am extracting the third last column: uptime 8:30pm up 23 day(s), 17:46, 5 users, load average: **2.79**, 1.79, 1.53 I always need to get the ...
user avatar
  • 297
0 votes
1 answer
65 views

Block Form text to Table using awk (openwrt file--> # cat /etc/config/dhcp)

I'm trying to "Pretty Print" the reserved static ip "devices" after i ssh into my openwork router. input file: config dnsmasq         option domainneeded '0'         option ...
user avatar
  • 15
3 votes
2 answers
266 views

Why does printf byte formatting fail when executed under `dash`?

The ASCII hex code for a zero is 0x30. Hence, you can print a zero by doing printf '\x30', and it will print a zero. If you put this into a shell script called myScript.sh, and then execute ./myScript....
user avatar
  • 41
-3 votes
1 answer
103 views

Can anyone tell me the secret section of the manual for printf to appear [closed]

I am trying to view Section 6 named "Games and Screensavers" of the Manual. I've tried searching geeksforgeeks.org for the answer as my class suggested but I have not been able to figure it ...
user avatar
3 votes
1 answer
303 views

printf quoted string

I am trying to create a table in bash - I have collected all headings into a var - however when I print these strings enclosed in "'s are treated as new columns and not as one by the printf ...
user avatar
  • 151
5 votes
2 answers
430 views

Replacing date with EPOCH in column output

I have a set of data like so: user1 1234 01/01/2020 user2 2345 02/02/2020 I need to convert the third column (date) to EPOCH while retaining the username and id number. user1 1234 1577862000 user2 ...
user avatar
  • 63
9 votes
8 answers
3k views

repeating a character using printf and appending a newline at the end

In order to repeat a character N times, we could use printf. E.g to repeat @ 20 times, we could use something like this: N=20 printf '@%.0s' $(seq 1 $N) output: @@@@@@@@@@@@@@@@@@@@ However, there ...
user avatar
  • 323
4 votes
3 answers
520 views

Padding unicode strings with bash's printf

I tried padding a Unicode string with bash's printf, and saw that, while printf "%2s" a yields the expected " a", a Unicode variant printf "%2s" ä yields a ...
user avatar
  • 2,933
0 votes
0 answers
59 views

Get the last modified file's epoch time in current directory

I need to get the epoch time which belongs last modified file in current directory. I have some limitations: must be compatible with RHEL and AIX systems AIX does not allow -printf option with find ...
user avatar
2 votes
0 answers
18 views

Bash Printf Argument $1 vs "$1" [duplicate]

I have a shell script that prints the first argument passed to it in the command line. I was experimenting with $1 and "$1". Interestingly to me, when I pass "John Doe" I get ...
user avatar
  • 497
-1 votes
1 answer
161 views

what does this do? printf '\t%s\n' "$@"

I'm writing a larger bash script, and I've found this snippet to test if a directory exists case insensitive Now, if I now have one directory called subs this script will print the path, and if I have ...
user avatar
  • 327
0 votes
1 answer
416 views

How to print variable value as a hex number?

I can find seemingly every variation of hex manipulation by printf except this one. I am trying to send html hex colour values to a text file, built mostly using printf. I can calculate the separate R,...
user avatar
0 votes
1 answer
99 views

Using awk Printf to print a full number

I'm using FSL (a neuroimaging programme) and I have a for loop to analyse all my participants. The line in question is : fslstats <FILENAME> -M -V | awk '{print $1 * $3 }' How do I get the awk ...
user avatar
1 vote
1 answer
114 views

why printf executes string instead of formatting

I'm newbie in bash scripting I want to format command with printf to prepare execution and assign into variable. But it is executing immediately. It is listing /opt/cinar/packages/libcnrhttp2_* ...
user avatar
  • 111
0 votes
1 answer
123 views

List contents of a directory and all sub directories including file details, i.e. size, date created, date modified, and date last opened

First I should apologize in advance - I have no idea what I'm doing when it comes to the Mac terminal... I need to do an inventory of all files in a directory/folder. There are several sub ...
user avatar
  • 1
0 votes
2 answers
260 views

set format with printf for $0

I compute the row average of the following data set however I have trouble setting the correct format for $0 so as to produce the file-output presented below. I currently have: awk '{T=0; for (i=1;i&...
user avatar
0 votes
1 answer
98 views

Locating printf object/module

I would like to know which kernel module is called when printf is executed in my C code. How can I find that? Let's say, when I use ldd I can find which libraries are attached to my binary file. I am ...
user avatar
  • 995
0 votes
2 answers
127 views

Output Bash argument with multiple single quotes to file verbatim (without losing any single quotes)

I'm trying to: Accept input in bash and store it verbatim to a file cat the contents of the file. Any special characters should also be written verbatim to the file. Sample Input ./verbatim.sh -a ...
user avatar
  • 1,335

1
2 3 4 5