I need to delete leading 0s only from a string. I found that there is no in-built function like LTRIM as in C.
I'm thinking of the below AWK script to do that:
awk -F"," 'BEGIN { a[$1] }
for (v in a) {
{if ($v == 0) {delete a[$v]; print a;} else exit;}
}'
But guess I'm not declaring the array correctly, and it throws error. Sorry new to AWK programming. Can you please help me to put it together.
Using
awk
, as requested:This provides for not trimming a plain "0" to an empty string, as well as avoiding the (probably) unwanted trimming of non-numeric fields. If the latter is actually desired behavior, the second pattern/action can be commented out. In either case, substitution is the way to go, since adding a number to a non-numeric field will generate an error.
Input:
Output:
Output trimming non-numeric fields:
Based on other recent questions you posted, you appear to be struggling with the basics of the
awk
language.I will not attempt to answer your original question, but instead try to get you on the way in your investigation of the awk language.
It is true that the syntax of awk expressions is similar to c. However there are some important differences.
I would recommend that you spend some time reading a primer on awk and find some exercises. Try for instance the Gnu Awk Getting Started.
That said, there are two major differences with
C
that I will highlight here:Awk only uses strings and numbers -- it decides based on context whether it needs to treat input as text or as a number. In some cases you may need to force conversion to string or to a number.
An Awk program always follows the same structure of a series of patterns, each followed by an action, enclosed in curly braces: pattern
{
action}
:Patterns can be regular expressions or comparisons of strings or numbers. If a pattern evaluates as true, the associated action is executed.
An empty pattern always triggers an action. The
{
action}
part is optional and is equivalent to{ print }
.An empty pattern with no action will do nothing.
Some patterns like
BEGIN
andEND
get special treatment. Before reading stdin or opening any files, awk will first collect allBEGIN
statements in the program and execute their associated actions in order.It will then start processing stdin or any files given and subject each line to all other pattern/action pairs in order.
Once all input is exhausted, all files are closed, and awk will process the actions belonging to all
END
patterns, again in order of appearance.You can use
BEGIN
action to initialize variables.END
actions are typically used to report summaries.A warning: Quite often we see people trying to pass data from the shell by partially unquoting the awk script, or by using double quotes. Don't do this; instead, use the awk
-v
option to pass on parameters into the program:If I understand correctly, and you just want to trim the leading
'0'
s from a value in bash, you can usesed
to provide precise regex control, or a simple loop works well -- and eliminates spawning a subshell with the external utility call. For example:Using
sed
:or using a herestring to eliminate the pipe and additional subshell (bash only)
Using a simple loop with string indexes:
var
will contain104
following 2 iterations of the loop.Here is a somewhat generic
ltrim
function that can be called asltrim(s)
orltrim(s,c)
, where c is the character to be trimmed (assuming it is not a special regex character) and where c defaults to " ":This can be called with 0, e.g.
ltrim($0,0)
NOTE:
This will work for some special characters (e.g. "*"), but if you want to trim special characters, it would probably be simplest to call the appropriate sub() function directly.
you can force
awk
to convert the field to a number and leading zeros by default will be eliminated.e.g.