Replace character in awk array

somedata | awk -F"#soils#" '{split($1,a,"NITNUM=");  print a[2]}'

how can i edit the content of array 2 above?

what i want to do is get rid of single quotes and double quotes. and then turn the "NewLine" into new lines.

the data in array 2 (a[2]) is:

"'327685650858'NewLine'4048854814312743'"NewLine1089901687312447NewLine1089901687304767NewLine6669407900051008NewLine3786114055313675

the expected result after the above is modiied should be:

327685650858
4048854814312743
1089901687312447
1089901687304767
6669407900051008
3786114055313675

here's what im currently doing to obtain the desire results:

somedata | awk -F"#soils#" '{split($1,a,"NITNUM=");  print a[2]}' | sed -e 's_"__g' -e 's_ __g' -e '/^$/d' 2>/dev/null | awk '{gsub("NewLine","\n");printf"%s",$0}' | sed -e "s/'/ /g" -e 's~ ~~g' | sed '/^$/d'

im hoping to find a portable solution for this. i cant justify running this many commands when i can just do it all with awk.

Hello SkySmart,

So for your array a's a[2] value I have taken as a line and tried following.

echo -e \"\'327685650858\'NewLine\'4048854814312743\'\"NewLine1089901687312447NewLine1089901687304767NewLine6669407900051008NewLine3786114055313675 | awk -vs1="'" '{gsub(/\"/,"",$0);gsub(s1,"",$0);num=split($0, a,"NewLine");for(i=1;i<=num;i++){print a}}'

For your code you could try to add this one into your existing code. Add -vs1="'" into your code.

'{gsub(/\"/,"",a[2]);gsub(s1,"",a[2]);num=split(a[2], array,"NewLine");for(i=1;i<=num;i++){print array}}'

Thanks,
R. Singh

1 Like

Try

{gsub ("\047|\"", _, a[2]); gsub ("NewLine", "\n", a[2]); print a[2]}
1 Like