Stack Exchange Network

Stack Exchange network consists of 175 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++...)

-1
votes
1answer
24 views

How to modify this `printf` code to reverse it's action?

Code below can, for example ... [anony@mous-pc ~]$ (printf 'g?%s?m0\n' 008 006 004 002 ; printf 'wq\n') | ed -s file.txt ... take all of numbers within ... [anony@mous-pc ~]$ cat some.txt 005 003 ...
6
votes
3answers
391 views

Shell builtin `printf` line limit?

The /usr/bin/printf util argument list length is limited to the shell's maximum command line length, (i.e. getconf ARG_MAX, on my system that'd be 2097152); example: # try using a list that's way too ...
2
votes
1answer
222 views

How to modify this `while read; mv printf` code to rename only files of certain filetype?

Code in question: ls | cat -n | while read n f; do mv "$f" `printf "video_%03d.mp4" $n`; done The above code will rename all files/folders within the directory executed to: video_001.mp4 video_002....
1
vote
2answers
32 views

print a sequence in a file

I would like to print in a column in a new file the following 1_number_density_per_atom_1.xvg 1_number_density_per_atom_2.xvg 1_number_density_per_atom_3.xvg . . . 1_number_density_per_atom_653.xvg ...
0
votes
3answers
498 views

Using `printf` to print variable containing `%` percent sign results in “bash: printf: `p': invalid format character”

I want to use printf to print a variable. It might be possible that this variable contains a % percent sign. Minimal example: $ TEST="contains % percent" $ echo "${TEST}" contains % percent $ printf ...
1
vote
3answers
36 views

read file and based on criterion print desire output

I have input file which looks like this: H1 O1 C1 H2 H3 O2 C2 Br1 I want to have an output file which to have the following output H1 = 1 O1 = 8 C1 = 6 H2 = 1 H3 = 1 O2 = 8 C2 = 6 Br1 = 35 So when ...
3
votes
1answer
54 views

Newline character produced by `echo`, is it always there when using no options, regardless of shell?

Although I have read Stephane's answer concentrated on basically all aspects of echo vs printf, I am unsure if it also answers my question, which stands: The newline character produced by echo, is it ...
0
votes
2answers
44 views

Using printf with escape sequences?

I can change my cursor shape like this: printf "\x1b[\x31 q" # block :) printf "\x1b[\x35 q" # ibeam :) But when I assign that to a variable with %s and expand using printf, it doesn't work: CURSOR=...
0
votes
1answer
53 views

printf and echo -e are giving different results

I am using \0n escape sequence with echo -e and printf where n is the octal value of the ASCII character to be printed. When i used the statement echo -e "\0101" the shell is printing character A but ...
0
votes
1answer
56 views

printf outputs rows, not columns

I have two arrays, which I want to print out in two separate columns from a single printf call. The following: printf "%-${padding}s→ %s\n" "${lnFrom[@]}" "${lnTo[@]}" Outputs: dotfiles/git/...
13
votes
6answers
798 views

Only print output after finding pattern

There's a script (let's call it echoer) that prints to screen a bunch of information. I'd like to be able to only see lines after a pattern is found. I imagine the usage of a solution to look ...
0
votes
2answers
60 views

Writing to a file horizontally

I have a script that I want to run and then write the output to a file in a specific format Currently when I run the script and direct it to standard output it gives it to me in the format I want. The ...
0
votes
1answer
27 views

Printf format error for number

After some greping and seding I manage to get a value from a csv cell and assign it to a variable (DBR). Here come some odd findings echo $DBR echo "$DBR", printf "%f" $DBR output: 9.1 ,.1 : ...
5
votes
1answer
49 views

%c formatted string shows different behaviour in the shell and in C

If i write a very simple line of code in C: printf("Ascii char for %d is %c\n",65,65); It simply prints A, as ascii value of 65 corresponds to character A. But if I use the same code in shell and ...
14
votes
3answers
558 views

Slightly confused about whether printf in the yash shell is a built-in command or not

The yash shell has a printf built-in, according to its manual. However, this is what I see in a yash shell with default configuration: kk@eeyore ~ $ command -v printf /usr/bin/printf kk@eeyore ~ $ ...
0
votes
1answer
54 views

Find + -printf + sort conflict?

I need to pass a list of sorted, quoted FLAC file names to SoX for concatenation but am having trouble getting sort to work the way I expect it to. If I use: find . -maxdepth 1 -type f -iname "*....
0
votes
0answers
27 views

Filter output / print one record per event or time period

I've got a custom HID device which prints out exact coordinates of touched points at real time. It is very sensitive & currently printing out the coordinate that's being touched on multiple ...
1
vote
1answer
135 views

What characters need to be escaped when using the printf command?

I want to clarify that I am not talking about how to escape characters on the shell level of interpretation. As far as I can tell, only two character need to be escaped: % and \ To print a literal %,...
8
votes
2answers
438 views

Explanation needed on how I can repeat a character in POSIX shell

The following answer on Stack Overflow, How can I repeat a character in bash? imposes one plausible way of POSIX-ly repeating a single character, as follows. In this example let's use the equal sign ...
1
vote
1answer
123 views

Printf - print repeating minus symbols [duplicate]

how can I use printf to print a row of minus symbols? when I try: printf "-----------\\n" I get: bash: printf: - : invalid option printf: usage: printf [-v var] format [arguments] when I try: ...
1
vote
3answers
94 views

print a column with the desire format output [closed]

I have a list of the following data in a file: -0.3476890000000000 0.0743938333333333 -1.0990240000000000 0.0743938333333333 -0.2169100000000000 0.0879397500000000 and I am ...
0
votes
2answers
43 views

how to alignment the output of the last fields

the bash script for i in $script_name do echo -en "running the script - $i\t - " exec 3>&1 4>&2 var=$( { time /tmp/scripts/$i 1>&3 2>&4; } 2>&1) # Captures ...
10
votes
2answers
1k 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. ...
1
vote
2answers
38 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
66 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
985 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
48 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
113 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
236 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
645 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
224 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
331 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
27 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
2k 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
55 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
66 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
2k 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
2k 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 ...
1
vote
1answer
271 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: ...
5
votes
6answers
4k 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
494 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
48 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
280 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
373 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
76 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
1k 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
1k 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
1k 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
1k 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' ...