Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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

10
votes
2answers
744 views

Why does `printf “%s”` concatenate two following strings together? [duplicate]

$ printf "%s" a b ab$ printf "%s%s" a b ab I have some problem understand the format specifier for printf. If I am correct it is mostly the same as those for strings in the C programming language. ...
0
votes
0answers
49 views

bash script uses curl

sed -e 's/,$//' -e 'y/,/:/' test.txt | xargs -P 80 -I XX /usr/bin/curl -sk https://XX/ |grep phrase |printf "XX" it does not work for some reason. I need to do the curl and print the ip visited. The ...
1
vote
2answers
34 views

Printing a file in columns with arbitrary amount of rows

I have this file: line1 line2 line3 line4 line5 line6 line7 line8 line9 And I want to print it across 3 columns with this structure so that first column has lines 1-2-3, second column has the lines ...
1
vote
1answer
55 views

printf to n column? [duplicate]

This: dots=$(printf "%0.s." {1..10}) prints this: .......... . I'd like to know how to get printf in Bash to print .s up to a specific column position, let's say 30, where the start column will ...
0
votes
3answers
331 views

How to remove filename extension from a list of filenames in bash

I am trying to remove the filename extension from a list of files I've generated with the following bash script: #!/bin/bash file_list=$(find . -type f) #assuming that the files are stored in the ...
1
vote
2answers
46 views

Reconsider spacing of columns

I have a file with the following format 1 1 1 0.02484183 57.2400000 24.6000000 2 1 2 -0.16091000 56.5200000 24.5600000 3 1 1 0.02484183 55.5700000 24....
0
votes
2answers
38 views

Using shell 'printf' where the format string is in a variable and doesn't have a fixed number of field placeholders?

I am writing a shell function which makes an external API call via cURL (the external API syntax isn't under my control). I've approached it like this (simplified): #!/bin/sh template_get_entry='...
-1
votes
3answers
115 views

Use dash in printf

How can I use a dash in printf string? x=xxx printf -v args "-x=%s" "$x" printf "$var" But it gives an error: ./tmp.sh: line 2: printf: -x: invalid option printf: usage: printf [-v var] format [...
-4
votes
2answers
103 views

printf command in bash [closed]

printf "%50s\n" " I ate 4 eggs" In this example I want to assign 50 from a variable. And How can I use the variable in for loop in bash? Any Idea?
-3
votes
1answer
71 views

Printf appending fails

I Tried to run the following similar commands in CentOS (12.x): printf "\nhello" "$HOME"/.bashrc printf "\nhello" ${HOME}/.bashrc After execution I display the content of the .bashrc file and see ...
2
votes
3answers
246 views

Printf fails to intepret backslash and exclamation mark

I am writing a java program from terminal using printf and redirecting its output to a .java file but printf fails to interpret the horizontal backslash tab (\t), and when I have an exclamation mark (!...
0
votes
1answer
25 views

Remove all lines in single file NOT matching keywords aligned in first column [closed]

I need to extract a series of lines identified by a keyword in the first column and print them in the same linear order. The file is divided into sections delimited by a line of hyphens or equal ...
5
votes
3answers
1k views

How to prepend a line to all files in a directory?

I have a directory called backup with file extensions *.sql. The thing I want to do is to prepend a line to all sql files in the directory backup. I did echo 'use my_db;' >> backup/*.sql, which ...
0
votes
3answers
52 views

Aligning the third word with 20 spaces from the beginning of the second word [duplicate]

We have the following file: cat info.txt linux03.sys98.com net16777728 Speed: 1000Mb/s linux03.sys98.com net16777728 Speed: 1000Mb/s linux01.sys98.com net3f0 Speed: 1000Mb/s ...
1
vote
1answer
51 views

printf work-alike wrapper for prefixing and redirecting error messages

I have an error function in my POSIX sh shell code. It essentially looks like error () { printf 'utility: ERROR: ' >&2 printf "$@" >&2 } The second printf makes it so that I ...
2
votes
3answers
782 views

print all files in a folder

let's say ls retunrs file1 file2 dir1 dire2 ..., I want to print you have file1 file2 dir1 dire2 ... in currnent folder. How can I do that? ls | xargs -i echo 'you have {} in current folder' prints ...
0
votes
2answers
877 views

printf escape %q string vs variable [duplicate]

I would like to escape path with white space which works fine when I use it directly like this: $ printf '%q' "/path/to/first dir/dir/" /path/to/first\ dir/dir/ When I use a variable instead of ...
0
votes
1answer
169 views

Using printf when string has backslash

I'm trying to both printf and export the variable used for setting PS1 but everything I've tried returns error in printf or puts literal \u in prompt. v='\u\$'; printf "$v"; export PS1="$v" -bash: ...
4
votes
5answers
2k views

Bash: How to generate random float number using $RANDOM

Is it possible to generate real random numbers with a specific precision and in a specific range using the Integer Random Generator $RANDOM? For example how we can generate real number with 4 ...
1
vote
1answer
351 views

How to printf literal characters from/to file in bash?

I want to filter a file by character (for the purpose of removing invalid xml characters which I cannot control the generation of) but I cannot seem to even be able to copy individual characters from ...
0
votes
1answer
45 views

Inconsistent shell behavior involving endline characters [duplicate]

Why does: [ "a" != "$(printf "\na")" ] && echo true || echo false print true while: [ "a" != "$(printf "\n")a" ] && echo true || echo false print false?
2
votes
0answers
133 views

I think printf broke my newlines in terminal [duplicate]

After running a bash script, my (gnome-)terminal ended up printing each new line where the last one ended (but on the next row). I think it was printf that broke things, since it works if I replace ...
5
votes
6answers
1k views

printf, awk … How to format a number with space to the thousands

Is there a simple way to separate a very large number per thousands with printf, awk, sed ? So 10000000000000 become 10 000 000 000 000 Thanks
0
votes
2answers
228 views

find, printf and odd behaviour with ssh

So, this works: find /dir/ -type f -printf "%p|%TY-%Tm-%Td|%TH:%TM|%s|%u|%U\n" > /dir/output.txt But this doesn't: ssh -o StrictHostKeyChecking=no "servername" find /dir/ -type f -printf "%p|%TY-...
2
votes
1answer
72 views

Differences in whitespaces between bash script and interactive shell

Please advice why this happens. from a Linux bash shell: ps PID TTY TIME CMD 20406 pts/0 00:00:01 bash 26896 pts/0 00:00:00 ps I run the follwing str="a b c d" printf "%s\n" ` echo ...
6
votes
2answers
772 views

Repeat a variable with printf

if I use the following command: printf "%.0s┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃\n" {1..3} I get an output like this: ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃...
4
votes
1answer
836 views

printf and how to add dots “…” between variables and their values

We have the following output cat gh.log machine status ============================================================= connection_ght 400 pach_po 72 timeout 311 ...
0
votes
2answers
830 views

Best way to create table-like CLI display in Bash?

I'm working on a Bash script for massive USB flash memory testing (using f3 under the hood). I need to display a table-like interface with multiple columns and cells. Currently I'm using printf with ...
3
votes
2answers
835 views

How to output in columns using Bash built-in `printf` where source contains colours?

The code is: #!/bin/bash ips[0]='192.168.0.1' ips[1]='192.168.0' ips[2]='255.255.255.256' ips[3]='123.123.123.123.123' ips[4]='a.b.c.d' ips[5]='255.255.255.255' ips[6]='0.0.0.0' ips[7]='192.168.1.1' ...
12
votes
1answer
894 views

How do I stop Bash from interpreting octal code instead of integer?

I'm writing a a Horoscope RSS script as an intro to Unix/Linux project, and ran into an issue my Professor couldn't fix in the time I was able to stay after class. Since I couldn't figure out how to ...
0
votes
3answers
569 views

How can I echo with escaped spaces?

I want to echo a variable with spaces into a text file. a=a\ b\ c echo $a > /tmp/a The above results in a file with a b c in it. What if I need the file to read a\ b\ c? I've tried printf %q $a ...
6
votes
4answers
749 views

printf: multibyte characters

When trying to format printf output involving strings containing multi-byte characters, it became clear that printf does not count literal characters but the number of bytes, which makes formatting ...
1
vote
1answer
2k views

Formatting numerical output in bash to have exactly 4 decimal places

I have a command that outputs a number to a log file, and I don't like the way it looks when the number changes the number of decimal places because it ruins the alignment and makes everything look ...
0
votes
1answer
239 views

How to use rename command with printf

As I was trying to fix the episode numbers of a TV series I toyed with the rename function and trying to pass the return from printf as the rename parameters, but could not do it. I tried just about ...
0
votes
1answer
416 views

Bash printf formatting not working!

My script is: #!/bin/bash num1=32.55 num2=145.958 printf "%5.2f\n" $num1 printf "%5.2f\n" $num2 The output is: 32.55 145.96 Instead of: 32.55 145.96 What's going on? If it's any help, I'm ...
11
votes
6answers
2k views

Padding trailing whitespaces in a string with another character

I'd like to output hello world over 20 characters. printf "%-20s :\n\n" 'hello world!!' # Actual output hello world!! : # Wanted output hello world!!========: However, I don't want to ...
5
votes
1answer
801 views

Does printf have an “engineering” notation format for numbers?

Say I have this number: 105000000, in this form I can't easily see how big it is, so using printf at a prompt I try using the "scientific" notation: % printf "%.3E" 105000000 1.050E+08 This is ...
2
votes
3answers
1k views

Accurate timestamping in Bash with printf (not a trivial problem)

Shell: Bash. Goal: obtain time t in milliseconds since some fixed point in time, suitable for timestamping stuff with printf. Condition: the solution must pass the all-in-one-line-of-text test. ...
2
votes
2answers
822 views

printf in shell script can't do \x%x

where ECHO-VAR produces \xFF\xFF\xFF\x00 ($fb_COLOR15) these work on the command line: CP="`ECHO-VAR`" printf $CP | dd status=none bs=4 count=$(( ( 1360 * 100 ) + 100 )) > /dev/fb0 printf "`ECHO-...
-2
votes
2answers
1k views

How to align fields with printf

the following syntax print the output1 down echo "$status" output1: component_name : TEZ_CLIENT recovery_enabled : true component_name : WEBHCAT_SERVER recovery_enabled : true ...
1
vote
2answers
946 views

Counting files, directories, etc. in given directory and formatting output on screen

Just started a scripting class and could use some help. I am writing a script to inspect the file names of every file and directory in a given directory and accumulate a count of Every file, ...
0
votes
1answer
322 views

Difference in the result of echo and printf

Why do I see a difference in the results for the following code: read a printf "%.3f\n" $(bc -l <<< "$a") RESULT: 17.929 and read a echo "scale=3; $a" | bc -l RESULT: 17.928 My input ...
0
votes
1answer
465 views

Why are only some letters considered invalid numbers in bash's printf builtin?

I was messing around and forgot to delete a section of a printf statement and got this output: # printf '%*s%c' {A..Z} bash: printf: A: invalid number bash: printf: D: invalid number bash: printf: G: ...
3
votes
1answer
2k views

How does printf with multiple arguments separated by space work

I have the following printf function: $ printf '%s %s %s\t%s\n' 100644 blob 8e1e f1.txt 100644 blob 9de7 f2.txt | git mktree Can anyone please explain what it produces and why? I expected to have ...
0
votes
2answers
122 views

Order of printf?

I'm wondering why the following prints the first command last? file=$(printf .tar.gz & printf test) echo $file It outputs test.tar.gz but I'd expect .tar.gztest.
6
votes
3answers
7k views

Formatted shell script output

How can I fix the output width using printf? This is an example script: #!/bin/bash OK=$(printf '\t%+50s\n' OK) FAIL=$(printf '\t%+50s\n' FAIL) for i in a aa aaa aaaa aaaaaa aaaaaaaaaaaaaaaaa ...
1
vote
1answer
25 views

printing second variable as many times as first variable

I have two variables. $a and $b in a for loop. $a has several lines of values. Example. $a= ````1 ````23 ````4 ````5 ````6 and $b has a single value. Example. $b= ````x I want my output as ...
2
votes
1answer
544 views

Disabling the security hardening options for a `nix-shell` environment

In an attempt to build GCC (6.3) on NixOS (16.09), within a nix-shell, I get this: make[1]: Entering directory '<my-build-path>/coreboot/util/crossgcc/build-i386-elf-GCC/build-x86_64-pc-linux-...
0
votes
1answer
513 views

Using printf with netcat

I've made a small command to send a TCP message to an "IP-relay-unit" that can toggle the outputs. printf "setstate,1:1,1\r" | nc ip.ip.ip.ip port This actually works fine, sometimes. Since the ...
0
votes
1answer
474 views

How to format the output of “wpa_cli scan result” command in a well aligned column?

How would you suggest to align all SSID in one single column in the wpa_cli scan_result command? To be clear, I want to transform this output # wpa_cli scan_results d8:fb:5e:a5:38:20 [WPA2-PSK-...