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
1 vote
2 answers
31 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 ...
  • 103
1 vote
1 answer
78 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
66 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
35 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 ...
  • 882
0 votes
0 answers
101 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
25 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
40 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 ...
  • 683
15 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
2 votes
2 answers
240 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
102 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" (...
  • 335
4 votes
2 answers
621 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
219 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
145 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
94 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
78 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
130 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
63 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
124 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
62 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 ...
  • 301
1 vote
2 answers
155 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 ...
0 votes
5 answers
104 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
509 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
172 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?
  • 165
1 vote
2 answers
124 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
297 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 ...
  • 494
1 vote
1 answer
67 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
387 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
514 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 ...
  • 97
-1 votes
1 answer
512 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 -- ' ' ...
  • 329
1 vote
2 answers
211 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 ...
8 votes
2 answers
13k 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
919 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
1k 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
27 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
515 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
787 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
583 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(...
  • 981
0 votes
1 answer
49 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,707
1 vote
2 answers
95 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
0 votes
3 answers
39 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 ...
  • 429
0 votes
1 answer
87 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 ...
  • 15
3 votes
2 answers
385 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....
  • 41
-3 votes
1 answer
117 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 ...
3 votes
1 answer
588 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 ...
  • 151
5 votes
2 answers
521 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 ...
  • 63
9 votes
8 answers
4k 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 ...
  • 323
5 votes
3 answers
690 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 ...
  • 3,093
0 votes
0 answers
69 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 ...
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 ...
  • 497
-1 votes
1 answer
324 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 ...
  • 337

1
2 3 4 5 6