Setting Environment Variable with Leading Digit in Bash

Setting environment variable with leading digit in bash

You can also bypass the bash interpreter and define the variable directly with the bash internal functions:

$ gdb --batch-silent -ex "attach $$"                              \
-ex 'set bind_variable("64bit", "1", 0)' \
-ex 'set *(int*)(find_variable("64bit")+sizeof(char*)*5) = 1' \
-ex 'set array_needs_making = 1'

$ env | grep 64
64bit=1

How to add leading zeros for for-loop in shell?

Use the following syntax:

$ for i in {01..05}; do echo "$i"; done
01
02
03
04
05

Disclaimer: Leading zeros only work in >=bash-4.

If you want to use printf, nothing prevents you from putting its result in a variable for further use:

$ foo=$(printf "%02d" 5)
$ echo "${foo}"
05

In bash, how could I add integers with leading zeroes and maintain a specified buffer

If by static versus dynamic you mean that you'd like to be able to use a variable for the width, you can do this:

$ padtowidth=3
$ for i in 0 {8..11} {98..101}; do printf "%0*d\n" $padtowidth $i; done
000
008
009
010
011
098
099
100
101

The asterisk is replaced by the value of the variable it corresponds to in the argument list ($padtowidth in this case).

Otherwise, the only reason your example doesn't work is that you use "2" (perhaps as if it were the maximum padding to apply) when it should be "3" (as in my example) since that value is the resulting total width (not the pad-only width).

How to zero pad a sequence of integers in bash so that all have the same width?

In your specific case though it's probably easiest to use the -f flag to seq to get it to format the numbers as it outputs the list. For example:

for i in $(seq -f "%05g" 10 15)
do
echo $i
done

will produce the following output:

00010
00011
00012
00013
00014
00015

More generally, bash has printf as a built-in so you can pad output with zeroes as follows:

$ i=99
$ printf "%05d\n" $i
00099

You can use the -v flag to store the output in another variable:

$ i=99
$ printf -v j "%05d" $i
$ echo $j
00099

Notice that printf supports a slightly different format to seq so you need to use %05d instead of %05g.

Simplify complex command, put it into a variable

If your date utility supports it (the one from GNU coreutils does) you can use:

date +'%A %B %-d'

The - tells date to not pad the numeric field. Demo:

$ date -d"2021/07/01" +'%A %B %-d'
Thursday July 1

Not sure I understand your second question but if you want to pass this command to a shell script (I do not really understand why you would do that), you can use the eval shell command:

$ cat foo.sh
#!/usr/bin/env bash

foo="$(eval "$1")"
echo "$foo"
$ ./foo.sh 'date -d"2021/07/01" +"%A %B %-d"'
Thursday July 1

Please pay attention to the double (") and simple (') quotes usage. And of course, you will have to add to this example script what is needed to handle errors, avoid misuses...

Note that many string comparison utilities support one form or another of extended regular expressions. So getting rid of these leading zeros or spaces can be as easy as:

grep -E 'Thursday\s+July\s+0*1' foo.txt

This would match any line of foo.txt containing

Thursday<1 or more spaces>July<1 or more spaces><0 or more zeros>1

BASH How to get first number in a string of a file

This solution may work for you:

paste <( grep -o "[0-9][0-9]*/[0-9][0-9]*/[0-9][0-9]*" list.txt  ) list.txt | sort -n -t/ -k3,3 -k1,1 -k2,2 | cut -f2-

To understand this solution, it's helpful to review the command incrementally adding pipe commands. Let's review the parts:

$ grep -o "[0-9][0-9]*/[0-9][0-9]*/[0-9][0-9]*" list.txt
4/20/2020
4/10/2020
5/25/2020
4/15/2020

The grep -o extracts only the matching part of the string.

By surrounding the grep with <() , we create a temporary named pipe so that we can use it as an input file as follows:

paste <( grep -o "[0-9][0-9]*/[0-9][0-9]*/[0-9][0-9]*" list.txt) list.txt

The paste combines each line from the two files. Essentially, we are prepending each line in list.txt with the date found on the line.

$ paste <( grep -o "[0-9][0-9]*/[0-9][0-9]*/[0-9][0-9]*" list.txt) list.txt
4/20/2020 john's birthday 4/20/2020
4/10/2020 Finish reading "the one thing" 4/10/2020
5/25/2020 verify video has been posted 5/25/2020
4/15/2020 4/15/2020 call bob

Here's an equivalent expression using paste - to read from stdin;

$ grep -o "[0-9][0-9]*/[0-9][0-9]*/[0-9][0-9]*" list.txt | paste -  list.txt
4/20/2020 john's birthday 4/20/2020
4/10/2020 Finish reading "the one thing" 4/10/2020
5/25/2020 verify video has been posted 5/25/2020
4/15/2020 4/15/2020 call bob

Now we can sort this output. We are going to use / as the delimiter and make sure we sort by year first, then month, and then day using the -k options to sort. It is also important to sort numerically (-n):

$ paste <( grep -o "[0-9][0-9]*/[0-9][0-9]*/[0-9][0-9]*" list.txt  ) list.txt | sort -n -t/ -k3,3 -k1,1 -k2,2 
4/10/2020 Finish reading "the one thing" 4/10/2020
4/15/2020 4/15/2020 call bob
4/20/2020 john's birthday 4/20/2020
5/25/2020 verify video has been posted 5/25/2020

Now we're almost done. We should delete the first column with the cut command. Here we are displaying column 2 to the end (that is, we are deleting column 1):

$ paste <( grep -o "[0-9][0-9]*/[0-9][0-9]*/[0-9][0-9]*" list.txt  ) list.txt | sort -n -t/ -k3,3 -k1,1 -k2,2  | cut -f2-
Finish reading "the one thing" 4/10/2020
4/15/2020 call bob
john's birthday 4/20/2020
verify video has been posted 5/25/2020

Remove leading digits from a string with Bash using parameter expansion

You can use BASH_REMATCH to extract the desired matching value:

$ RU="903B/100ms"
$ [[ $RU =~ ^([[:digit:]]+)(.*) ]] && echo ${BASH_REMATCH[2]}
B/100ms

Or just catch the desired part as:

$ [[ $RU =~ ^[[:digit:]]+(.*) ]] && echo ${BASH_REMATCH[1]}
B/100ms


Related Topics



Leave a reply



Submit