AWK returns number instead of string for sub/gsub

Istvan

I am parsing the output of a command, example:

0. BP-726162639-172.16.2.40-1425055855614:blk_1724943006_651672912 len=39498 Live_repl=3 [DatanodeInfoWithStorage[172.16.2.168:50010,DS-fbfe96c7-82c7-4be3-b056-5a74b3fa2f4b,DISK], DatanodeInfoWithStorage[172.16.2.170:50010,DS-5459e7a0-2874-4a84-b4a4-e7fc84be391f,DISK], DatanodeInfoWithStorage[172.16.2.162:50010,DS-86792af4-6db8-478a-97c7-2b6560f2cc19,DISK]]

The first sub works as expected but the subs after that fail:

awk '{print "block # : " gsub(/\./,"",$1) $2 " :: " sub(/len=/,"a",$3) " :: " $4}'

This prints:

block # : 1BP-726162639-172.16.2.40-1425055855614:blk_1724943006_651672912 :: 1 :: Live_repl=3

I am not sure why this is happening. Is it not ok to use multiple subs?

After understanding @ken's answer it is obvious that I need to do the substitution at the beginning and just use $1 $2 etc later in the print.

Kent

both gsub and sub worked in your codes.

The two functions will do substitutions, and return the count, how many substitutions were done. (not the string after replacement).

So the 1 before BP-... is from gsub and the other 1 between :: s, is from sub().

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Typescript function returns string instead of number

Mysql returns ***** instead of number

Material-UI Rating returns string instead of number

RestAssured returns array instead of String

Function returns string instead of dictionary

getResource returns boolean instead String[]

Crystal Formula field - Splitting a string array of numbers, summing them, and returning the total returns a boolean instead of a number

Function returns an instance of Future<String> instead of String

string.match() returns a String instead of an Array

Excel sumif returns big number instead of zero

Prime number function returns true instead of false

fork() returns positive number instead of zero

R returning number instead of string

convert number to string returns empty if number 0

Javascript: Function returns 0 instead of the number that appears odd number of times

Pandas strptime returns a string instead of DateTime object

Protractor - getText() returns an Array instead of a String

mysqli code returns int instead of a string

JsonResults returns Enum value instead of String representation

Codeigniter redirect returns string instead of passing it to the url

Why StreamReader returns string instead of byte array

JSON.parse returns string instead of object

CURL returns full string response instead of xml

Why does this function returns '' instead of a concatenated string

Flutter jsonDecode returns String instead of List

php Function returns zero instead of the expected string

Sphinx returns digits instead of string in matches array

Why SUM(`column`) returns a string instead of an integer?

jQuery returns string instead of DOM object