Add leading 0 in sed substitution

bash add leading zero to variable
sed zero or one
bash format number
bash zero pad string
sed format number
sed how to match
how to add trailing zeros in unix
sed wildcard

I have input data:

foo 24
foobar 5 bar
bar foo 125

and I'd like to have output:

foo 024
foobar 005 bar
bar foo 125

So I can use this sed substitutions:

s,\([a-z ]\+\)\([0-9]\)\([a-z ]*\),\100\2\3,
s,\([a-z ]\+\)\([0-9][0-9]\)\([a-z ]*\),\10\2\3,

But, can I make one substitution, that will do the same? Something like:

if (one digit) then two leading 0
elif (two digits) then one leading 0


I doubt that the "if - else" logic can be incorporated in one substitution command without saving the intermediate data (length of the match for instance). It doesn't mean you can't do it easily, though. For instance:

$ N=5
$ sed -r ":r;s/\b[0-9]{1,$(($N-1))}\b/0&/g;tr" infile
foo 00024
foobar 00005 bar
bar foo 00125

It uses recursion, adding one zero to all numbers that are shorter than $N digits in a loop that ends when no more substitutions can be made. The r label basically says: try to do substitution, then goto r if found something to substitute. See more on flow control in sed here.

Zero-fill numbers to 2 digits with sed, How can I add a 0 if there is only a single digit, e.g. 1 in the "day" part? I need this date format: YYYYMM DD. share. I would like to add leading zeros to a string of digits so as to have exactly three digits. Consider for example echo "hello_1" | sed -E 's/([0-9]+)/00\1/' # hello_001 echo "hello_12" | sed -E

Use two substitute commands: the first one will search for one digit and will insert two zeroes just before, and the second one will search for a number with two digits and will insert one zero just before. GNU sed is needed because I use the word boundary command to search for digits (\b).

sed -e 's/\b[0-9]\b/00&/g; s/\b[0-9]\{2\}\b/0&/g' infile

EDIT to add a test:

Content of infile:

foo 24 9
foo 645 bar 5 bar
bar foo 125

Run previous command with following output:

foo 024 009
foo 645 bar 005 bar
bar foo 125

How to zero pad a number or a variable?, In this article, we will see the different ways in which we can do zero padding of a number or a variable or in other words, to put leading zeros to  This article is part of the on-going Unix Sed Tips and Tricks series. In our previous sed articles we learned — sed printing, sed deletion, sed substitute , sed file write, and sed multiple commands. In this article, let us review some interesting workarounds with the “s” substitute command in sed with several practical examples.

You seem to have the sed options covered, here's one way with awk:

BEGIN      { RS="[ \n]"; ORS=OFS=""   }
/^[0-9]+$/ { $0 = sprintf("%03d", $0) } 
           { print $0, RT             }

Bash: Removing leading zeroes from a variable (Example), sed. Following example performs string replacement using sed. old="0004937" # sed removes leading zeroes from stdin new=$(echo $old  I basically would like to add zeros before the 1st and 2nd set of numbers but not the 3rd since they already contain 3 digits. I'd like them all to be 3 digits, so two zeros should be added to the 1st set and one zero should be added to the 2nd set. I am not necessarily wanting to add the zeros to the very beginning of the file.

This might work for you (GNU sed):

echo '1.23 12,345 1 12 123 1234 1' | 
sed 's/\(^\|\s\)\([0-9]\(\s\|$\)\)/\100\2/g;s/\(^\|\s\)\([0-9][0-9]\(\s\|$\)\)/\10\2/g' 
1.23 12,345 001 012 123 1234 001

or perhaps a little easier on the eye:

sed -r 's/(^|\s)([0-9](\s|$))/\100\2/g;s/(^|\s)([0-9][0-9](\s|$))/\10\2/g'

Adding Leading Zeros for date in a file, If the format is not correct then a leading zero should be added to make it in mm/​dd/yyyy I tried few awk printf commands but nothing works out for multiple Replace every instance of a single digit followed by a slash by this  Zeros attached to the beginning of a number in this way are called leading zeros. In decimals, this idea of zeros that don’t add value to a number can be extended to trailing zeros. A trailing zero is any zero that appears to the right of both the decimal point and every digit other than zero. 34.8 34.80 34.8000.

I find the following sed approach to pad an integer number with zeroes to 5 (n) digits quite straighforward:

sed -e "s/\<\([0-9]\{1,4\}\)\>/0000\1/; s/\<0*\([0-9]\{5\}\)\>/\1/"
  1. If there is at least one, at most 4 (n-1) digits, add 4 (n-1) zeroes in front
  2. If there is any number of zeroes followed by 5 (n) digits after the first transformation, keep just these last 5 (n) digits

When there happen to be more than 5 (n) digits, this approach behaves the usual way -- nothing is padded or trimmed.





Removing Zeros in front of a number, Fixed with file- removing leading zeros and adding the space. Hi All, i have a fixed Tagged: sed;replace;find;fixed width;position;, shell scripts. Discussion  To display leading zeroes, apply a custom number format by performing these steps: Select a cell(s) where you want to show leading zeros, and press Ctrl+1 to open the  Format Cells  dialog. Under  Category, select  Custom. Type a format code in the  Type  box.

magic of sed -- find and replace "text" in a string or a file · GitHub, sed 'n;d'. # insert a blank line above every line which matches "regex". sed '/regex​/{x;p;x;}' delete leading whitespace (spaces, tabs) from front of each line. # aligns all text flush left gsed ':a;s/\B[0-9]\{3\}\>/,&/;ta' # GNU sed. sed -e :a -e 's/\(. #!/bin/sh sed ' /WORD/ { i\ Add this line before a\ Add this line after c\ Change the line to this one }' Click here to get file: Leading tabs and spaces in a sed script. Sed ignores leading tabs and spaces in all commands. However these white space characters may or may not be ignored if they start the text

Using sed to prepend zero to four-digit numbers, You wish to prepend to only four-digit numbers, so you need to match beginning and ending, like this: sed 's/^[0-9]\{4\}$/0&/'. I sometimes use  Help with adding leading zeros to a filename Hi i need help in adding leading zero to filenames e.g file name in my folder are 1_234sd.txt 23_234sd.txt the output i need is 001_234sd.txt 023_234sd.txt can i do this shell scripting please help (2 Replies)

sed, a stream editor, Add the commands contained in the file script-file to the set of commands to be run while processing the A length of 0 (zero) means to never wrap long lines. The s command (as in substitute) is probably the most important in sed and has a lot of different options. Leading whitespace after the a command is ignored.