How to use printf with strings as arguments? - awk

This code in awk doesn't work with printf :
var1 = "%-15s %-15s\n"
var2 = "\"oooo\",\"nnnn\""
var3 = var1 var2
printf var3
Updated :
no var3
printf var1,var2
And I want it works like :
printf "%-15s %-15s","oooo","nnnn"
because I'm building a visual results file of a parsing and I don't know in advance the number of arguments.
Is there any solution in this way?

I don't believe you can do the entire string like that as the commas are treated as string literals in the printf function when supplied through a single variable like that. You could do:
var1 = "\"%-15s %-15s\n\"";
var2 = "\"oooo\"";
var3 = "\"nnnn\"";
printf var1,var2,var3;

Related

Kotlin string escape special char: $

This is my scratch:
// how to present $-$ as Kotlin string
val var1 = "\$-\$"
val var2 = "\$-$"
print("${var1.count()}: $var1")
print("${var2.count()}: $var2")
print("${var1 == var2}")
can someone explain why var2 works? (no need to escape 2nd $ char?)
A template expression in a template String in Kotlin is a dollar ($) followed by either a name or an expression in curly braces (see the String templates documentation).
The single $ sign which is followed by nothing in your String var2 does not need to be escaped because it is not a template expression.

gawk: printf output ignores OFS

When I use printf() in gawk the OFS separates output fields. However,in this script it ignores the OFS so output fields are separated by commas. Reading about the printf() function I don't see how to separate output fields with a pipe, "|".
BEGIN { FS="|", OFS="|" }
{ split ($6, a, "/"); printf "%s,%s,%s,%s,%s,%s-%s-%s,%s\n", $1"|"$2"|"$3"|"$4"|"$5"|" a[3], a[1], a[2]"|"$7; }
Thanks in advance,
Rich
The f in printf stands for "formatting" - you provide all the formatting as the first argument for printf instead of using the defaults like OFS as a separator or OFMT for numeric output as used by print.
When you write:
printf "%s,%s\n", x, y
you're specifically stating that you want a comma between 2 string fields. If you want to use printf but also want whatever value OFS has between the fields then you'd instead write:
printf "%s%s%s\n", x, OFS, y
which is equivalent to:
print x, y
In any case, I think what you're trying to do is:
BEGIN { FS=OFS="|" }
{ split ($6, a, "/"); $6=a[3] "-" a[1] "-" a[2]; print }

Bash - Update a value in a file using sed command

I am trying to update the value of key1[count] to 2 in the following file.
#file1
//some additional lines
key1 = { var1 = "1", var2 = "12", "count" = 1 }
key2 = { var1 = "32", var2 = "23", "count" = 1 }
key3 = { var1 = "22", var2 = "32", "count" = 3 }
key4 = { var1 = "32", var2 = "12", "count" = 3 }
//some additional lines
I had tried using awk - awk -i inplace '{ if ( $1 == "key1" ) $12 = 2 ; print $0 }' file1.
However, this only works with awk version 4.1.0 and higher.
With your shown samples, please try following awk code.
awk -v newVal="2" '
/^key1/ && match($0,/"count"[[:space:]]+=[[:space:]]+[0-9]+/){
value=substr($0,RSTART,RLENGTH)
sub(/[0-9]+$/,"",value)
$0=substr($0,1,RSTART-1) value newVal substr($0,RSTART+RLENGTH)
}
1
' Input_file
Explanation: Checking condition if line starts from key1 and using match function to match regex "count"[[:space:]]+=[[:space:]]+[0-9]+ to get count string with its value here. Then with matched sub-string creating value(variable), where substituting digits at last of value to NULL. Then re-assigning value of current line to before match value, matched value, new value variable and rest of the line to make it as per OP's requirement. Finally by 1 printing current line(s).
NOTE: Above will only print the output on terminal, once you are Happy with shown results append > temp && mv temp Input_file to above code.
You can use sed with the -r option for regular expression.
For your example, you can use this command.
sed -i -r 's/(key1.*"count" = )([0-9]*)(.*)/\12\3/g' <file>
More information:
Command template:
sed -i -r 's/(<bef>)(<tar>)(<aft>)/\1<new value>\3/g' <file>
Where
<bef> is a regex that targets /key1... "count" = /
<tar> is a regex that targets /1/
<aft> is a regex that targets / }/
Together, /<bef><tar><aft>/ will expand to become:
/key1 = { var1 = "1", var2 = "12", "count" = 1 }/
Since you want to replace "1" with "2"
<new value> is 2

awk | Rearrange fields of CSV file on the basis of column value

I need you help in writing awk for the below problem. I have one source file and required output of it.
Source File
a:5,b:1,c:2,session:4,e:8
b:3,a:11,c:5,e:9,session:3,c:3
Output File
session:4,a=5,b=1,c=2
session:3,a=11,b=3,c=5|3
Notes:
Fields are not organised in source file
In Output file: fields are organised in their specific format, for example: all a values are in 2nd column and then b and then c
For value c, in second line, its coming as n number of times, so in output its merged with PIPE symbol.
Please help.
Will work in any modern awk:
$ cat file
a:5,b:1,c:2,session:4,e:8
a:5,c:2,session:4,e:8
b:3,a:11,c:5,e:9,session:3,c:3
$ cat tst.awk
BEGIN{ FS="[,:]"; split("session,a,b,c",order) }
{
split("",val) # or delete(val) in gawk
for (i=1;i<NF;i+=2) {
val[$i] = (val[$i]=="" ? "" : val[$i] "|") $(i+1)
}
for (i=1;i in order;i++) {
name = order[i]
printf "%s%s", (i==1 ? name ":" : "," name "="), val[name]
}
print ""
}
$ awk -f tst.awk file
session:4,a=5,b=1,c=2
session:4,a=5,b=,c=2
session:3,a=11,b=3,c=5|3
If you actually want the e values printed, unlike your posted desired output, just add ,e to the string in the split() in the BEGIN section wherever you'd like those values to appear in the ordered output.
Note that when b was missing from the input on line 2 above, it output a null value as you said you wanted.
Try with:
awk '
BEGIN {
FS = "[,:]"
OFS = ","
}
{
for ( i = 1; i <= NF; i+= 2 ) {
if ( $i == "session" ) { printf "%s:%s", $i, $(i+1); continue }
hash[$i] = hash[$i] (hash[$i] ? "|" : "") $(i+1)
}
asorti( hash, hash_orig )
for ( i = 1; i <= length(hash); i++ ) {
printf ",%s:%s", hash_orig[i], hash[ hash_orig[i] ]
}
printf "\n"
delete hash
delete hash_orig
}
' infile
that splits line with any comma or colon and traverses all odd fields to save either them and its values in a hash to print at the end. It yields:
session:4,a:5,b:1,c:2,e:8
session:3,a:11,b:3,c:5|3,e:9

Capturing pretty-printed string output, to display multiple variables on a single line in gdb?

In my gdb session, I have typed this:
(gdb) p arg1
$17 = (svn_revnum_t *) 0xbfffea0c
(gdb) p *(arg1)
$18 = -1
Now, I would like the "pretty-printed" output for both commands to be shown in a single line, as in:
$19 = (svn_revnum_t *) 0xbfffea0c ; -1
... so I try something like this:
(gdb) p arg1, ";", *(arg1)
$19 = -1
... but obviously, it doesn't work.
Is there a way to do something like this?
I guess, if it was possible to somehow "capture" the pretty-printed output of print as a string, then I could use printf "%s ; %s" to format my output; but how would one capture the print output, then?
I think the simplest way to do this is to write a new Python command that does what you want.
Alternatively you could write a Python convenience function that uses str on its argument. Then, use that with printf.