0

I have a text file(.txt) including two columns. I want to add three zeros (000) behind the number (like 324 would be turned into 000324) if the numbers are three digits and add two zeros (00) behind the number if the numbers are 4 digits already (like 1348 would be turned into 001348). How I can do this in bash?

324 359
1348 1384
user8034918
  • 441
  • 1
  • 9
  • 20
  • So you want to zero-pad to 6 digits? Cause that's actually easier. – wjandrea Dec 07 '19 at 23:40
  • Yes thanks for stating it bettter – user8034918 Dec 07 '19 at 23:44
  • Does this answer your question? [How to zero pad a sequence of integers in bash so that all have the same width?](https://stackoverflow.com/questions/8789729/how-to-zero-pad-a-sequence-of-integers-in-bash-so-that-all-have-the-same-width) – wjandrea Dec 07 '19 at 23:45
  • Nope I have so many random numbers in the text file I should have stated that in the question – user8034918 Dec 07 '19 at 23:47
  • 1
    Oh yeah sorry, that question's about a sequence. But the `printf` solution mentioned in the top answer should work in your case. – wjandrea Dec 07 '19 at 23:48

4 Answers4

1

If it is ok to use "awk",

$ cat input.txt 
Subject02/Scene1/Color/rgb1 3 1348 1384
$ awk '{ printf("%s %s %06d %06d\n", $1,$2,$3,$4) }' input.txt 
Subject02/Scene1/Color/rgb1 3 001348 001384
Yuji
  • 525
  • 2
  • 8
  • Thanks @Yuji. The file I wan to modify has four columns. The first and second column I don't want to touch it and I want to modify third and fourth column and keep the first and second as it is. I should have elaborated better in my initial question – user8034918 Dec 07 '19 at 23:57
  • `Sbj02/Scn1/Clr/rgb1 3 1348 1384` Here is example of only one line. In here I don't want second column which is `3` to be changed also the first column. I just want third and fourth column to be changed as I said – user8034918 Dec 08 '19 at 01:02
  • Only third and fourth columns are modified. – Yuji Dec 08 '19 at 01:06
1

With bash:

printf "%.6d %.6d\n" $(< file)

Output:

000324 000359
001348 001384
Cyrus
  • 84,225
  • 14
  • 89
  • 153
0

Use printf with %06d, which means "zero-pad to 6 digits as a decimal number".

while read -r n0 n1; do
    printf '%06d %06d\n' "$n0" "$n1"
done <<EOF
324 359
1348 1384
EOF

Output:

000324 000359
001348 001384
wjandrea
  • 28,235
  • 9
  • 60
  • 81
0

awk can also be used to reformat an unknown number of integers per-line zero-padded to 6-digits by looping from 1-NF and outputting the reformatted value. You can determine the delimiter to be placed between them by conditionally outputting the delimiter of choice in your printf format string. For space separated values you could use:

$ awk '{for (i=1; i<=NF; i++) printf (i>1)?" %.6d":"%.6d", $i; print ""}' file
000324 000359
001348 001384

That will eliminate your fixed format dependence on two-integer values per line.

Example Input File

$ cat file2
324 359 12
1348 1384 991
11 101 1001

Example Use/Output

$ awk '{for (i=1; i<=NF; i++) printf (i>1)?" %.6d":"%.6d", $i; print ""}' file2
000324 000359 000012
001348 001384 000991
000011 000101 001001
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85