printf

printf – store formatted string output in a variable

printf – store formatted string output in a variable Instead of the following printf ‘%st%st%st%sn’ ‘index’ ‘podcast’ ‘website’ ‘YouTube’ I want to store the printf output in a Results variable, how can I do this? Asked By: Porcupine || Source Bash (since 3.1), zsh (since 5.3) and ksh93 (since v- and u+m 2021-11-28) have an …

Total answers: 1

How do I pass hex characters to printf in a script command?

How do I pass hex characters to printf in a script command? How do I get printf to output hex characters when run from a script? Suppose I am at the prompt and type printf "x41x42" the output I get AB% However if I have a script containing that line i.e. cat test.sh produces printf …

Total answers: 1

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

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 "%TY-%Tm-%Td %TT %pn" | sort -n However it doesn’t list only …

Total answers: 1

Print Variable containing backslashes

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" printf ‘%s’ "$A" the dissappear and it just says "Hello World". What can I do to get "Hello World" on …

Total answers: 1

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

"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 filenames of interest to find instead of ls, but it seems like using a sledgehammer …

Total answers: 1

How to use argument twice in printf in shell?

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? Asked By: theonlygusti || Source In zsh: printf ‘%1$s%1$s’ one two Where %n$s is like %s except that it uses the nth argument instead of the next one like the …

Total answers: 1

printf "%.3f" ${variable with newlines} – error with n

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 "%.03fn" $(sed ‘s/"//g’ file.csv) But now I want to store sed ‘s/"//g’ file.csv as a variable var_sed=$(sed ‘s/"//g’ file.csv); printf "%.03fn" ${var_sed} …

Total answers: 1

Rounding many values in a csv to 3 decimals (printf ?)

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.200 0.334 0.111 For one value this is working: printf "%.3f" …

Total answers: 3

first character goes to last when formatting XYZ columns with printf awk

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 the …

Total answers: 1

Why I can pipe echo into bc, but I can't do the same with printf?

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: syntax error Asked By: robertspierre || Source Just …

Total answers: 1

How to convert seconds to hh:mm:ss.ms when getting difference between 2 times

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 "%s.%03dn", strftime("%H:%M:%S", localtime(int($now/1000))), ($now % 1000);’ From the above I want to convert the …

Total answers: 3

Shell function to center its first argument

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 whitespace from the string str=$(echo "$str" | sed -e ‘s/^[[:space:]]*//’ -e …

Total answers: 2

Is it safe to snprintf with output to NULL and size 0?

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 that does that, but in the rest of …

Total answers: 1

generate a report of sales with awk

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,Weekly_Sales,Holiday_Flag,Temperature,Fuel_Price,CPI,Unemployment 1,Store1,05-02-2010,2010,1643690.9,No,42.31,2.572,211.0963582,8.106 1,Store1,12-02-2010,2010,1641957.44,Yes,38.51,2.548,211.2421698,8.106 … … 45,Store45,12-10-2012,2012,734464.36,No,54.47,4,192.3272654,8.667 45,Store45,19-10-2012,2012,718125.53,No,56.47,3.969,192.3308542,8.667 I’m trying to group the stores and …

Total answers: 2

How to do formatted printing with jq?

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 char spaces with left alignment? echo ‘{"title" : "A sample name", "number" : …

Total answers: 3

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

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 is no newline character at the end of that …

Total answers: 8