I am trying to remove leading zeroes from a BASH array... I have an array like:
echo "${DATES[#]}"
returns
01 02 02 03 04 07 08 09 10 11 13 14 15 16 17 18 20 21 22 23
I'd like to remove the leading zeroes from the dates and store back into array or another array, so i can iterate in another step... Any suggestions?
I tried this,
for i in "${!DATES[#]}"
do
DATESLZ["$i"]=(echo "{DATES["$i"]}"| sed 's/0*//' )
done
but failed (sorry, i'm an old Java programmer who was tasked to do some BASH scripts)
Use parameter expansion:
DATES=( ${DATES[#]#0} )
With bash arithmetic, you can avoid the octal woes by specifying your numbers are base-10:
day=08
((day++)) # bash: ((: 08: value too great for base (error token is "08")
((day = 10#$day + 1))
echo $day # 9
printf "%02d\n" $day # 09
You can use bash parameter expansion (see http://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html) like this:
echo ${DATESLZ[#]#0}
If: ${onedate%%[!0]*} will select all 0's in front of the string $onedate.
we could remove those zeros by doing this (it is portable):
echo "${onedate#"${onedate%%[!0]*}"}"
For your case (only bash):
#!/bin/bash
dates=( 01 02 02 08 10 18 20 21 0008 00101 )
for onedate in "${dates[#]}"; do
echo -ne "${onedate}\t"
echo "${onedate#"${onedate%%[!0]*}"}"
done
Will print:
$ script.sh
01 1
02 2
02 2
08 8
10 10
18 18
20 20
21 21
0008 8
00101 101
Related
I am pretty new to bash scripting. I have my bash script below and I want to include an if statement when month (ij==09) equals 09 then "i" should be from 01 to 30. I tried several ways but did not work.
How can I include an if statement in the code below to achieve my task.? Any help is appreciated.
Thanks.
#!/bin/bash
for ii in 2007
do
for i in 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #Day of the Month
do
for ij in 09 10 # Month
do
for j in 0000 0100 0200 0300 0400 0500 0600 0700 0800 0900 1000 1100 1200 1300 1400 1500 1600 1700 1800 1900 2000 2100 2200 2300
do
cdo cat DAS_0125_H.A${ii}${ij}${i}.${j}.002_var.nc outfile_${ii}${ij}${i}.nc
done
done
done
done
The smallest change is adding a continue for day 31 in month 9.
You must test "09" as a string (or as 10#09).
(I also changed cdo ... into echo cdo ...)
for ii in 2007
do
for i in 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #Day of the Month
do
for ij in 09 10 # Month
do
if [[ "${ij}" == "09" ]] && [[ "${i}" == "31" ]]; then continue; fi
for j in 0000 0100 0200 0300 0400 0500 0600 0700 0800 0900 1000 1100 1200 1300 1400 1500 1600 1700 1800 1900 2000 2100 2200 2300
do
echo "cdo cat DAS_0125_H.A${ii}${ij}${i}.${j}.002_var.nc outfile_${ii}${ij}${i}.nc"
done
done
done
done
It would be easier to read when you use loop through the variales with a seq. You do not want to use `for ((i=1;i<=31;i++)) in view of the leading zeroes.
Also use verbose variable names.
for year in 2007
do
for day in {01..31} # Day of the Month
do
for month in {09,10} # Month
do
if [[ "${month}" == "09" ]] && [[ "${day}" == "31" ]]; then continue; fi
for hour in {00..23}
do
echo cdo cat DAS_0125_H.A${year}${month}${day}.${hour}00.002_var.nc outfile_${year}${month}${day}.nc
done
done
done
done
When the files already exist, you can consider
ls DAS_0125_H.A2007{09,10}{01..31}.{00..23}00.002_var.nc |
sed -r 's/.*([0-9]{8})/cdo cat & outfile_\1.nc/'
When this will show the commands you want, you can execute them by
source <(ls DAS_0125_H.A2007{09,10}{01..31}.{00..23}00.002_var.nc |
sed -r 's/.*([0-9]{8})/cdo cat & outfile_\1.nc/')
all the numbers inside the le that could be days in a month|
1-31. Assume that numbers 1-9 may or may not be preceded by a 0. The one or two digits of the numbers must be sandwiched by non-numeric characters. Print each matching number on its
own line.
So basically I read all the numbers 1-31 or 01-31 and output them on their own line.
What I know is that
I can use a split to maybe split this up and that would be good and I also know i could use something similar to ([1-9]|0[0-9]|1[1-9]|2[0-9]|3[0-1]) to find the numbers, but after that I'm stumped and could use some help.
Edit: to answer some questions this is in ruby and some test input / output here to help
Input-
1
10
12
18 19 20
21 22 ----asdfadsf 23t24####25,26,27,
28!
29.
30
31
32
33
01 02 03x04x05x06x07x08x09
001
002
1x1a
35
-87 76 101 10057
-13 -1.5
Output- 1
10
12
18
19
20
21
22
23
24
25
26
27
28
29
30
31
01
02
03
04
05
06
07
08
09
1
1
13
1
5
(each one of those on it's own line, but I didn't want to make it too long) I also removed a few to make it a smaller file so there is a chance i missed removing a number in the output, but I think I got them all.
This'll do it for you assuming your numbers are a single string x
x.split(/\D+/).select {|n| n.to_i >= 0 && n.to_i < 32 && n.length < 3}.join("\n")
If they are in a file, you can easily convert them to a string using IO::read(path)
x = IO.read(path)
x.split(/\D+/).select {|n| n.to_i >= 0 && n.to_i < 32 && n.length < 3}.join("\n")
How to add leading zero to bash range?
For example, I need cycle 01,02,03,..,29,30
How can I implement this using bash?
In recent versions of bash you can do:
echo {01..30}
Output:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
Or if it should be comma separated:
echo {01..30} | tr ' ' ','
Which can also be accomplished with parameter expansion:
a=$(echo {01..30})
echo ${a// /,}
Output:
01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30
another seq trick will work:
seq -w 30
if you check the man page, you will see the -w option is exactly for your requirement:
-w, --equal-width
equalize width by padding with leading zeroes
You can use seq's format option:
seq -f "%02g" 30
A "pure bash" way would be something like this:
echo {0..2}{0..9}
This will give you the following:
00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
Removing the first 00 and adding the last 30 is not too hard!
This works:
printf " %02d" $(seq 1 30)
in bash, with
$ echo {1..10}
1 2 3 4 5 6 7 8 9 10
I can get a numbers sequence, but in some case I need
01 02 03 ... 10
how I can get this ?
and how I can get ?
001 002 ... 010 011 .. 100
This will work in any shell on a machine that has coreutils installed (thanks commenters for correcting me):
seq -w 1 10
and
seq -w 1 100
Explanation:
the option -w will:
Equalize the widths of all numbers by padding with zeros as necessary.
seq [-w] [-f format] [-s string] [-t string] [first [incr]] last
prints a sequence of numbers, one per line (default), from
first (default 1), to near last as possible, in increments of incr (default
1). When first is larger than last the default incr is -1
use seq command with -f parameter, try:
seq -f "%02g" 0 10
results:
00
01
02
03
04
05
06
07
08
09
10
seq -f "%03g" 0 10
results:
000
001
002
003
004
005
006
007
008
009
010
printf "%02d " {1..10} ; echo
Output:
01 02 03 04 05 06 07 08 09 10
Similarly:
printf "%03d " {1..100} ; echo
In more recent versions of bash, simply:
echo {01..10}
And:
echo {001..100}
for i in {01..99}; do
echo $i
done
will return :
01
02
03
04
05
06
07
08
09
10
...
Replacing 01 with 001 and 99 with 999 or 100 will do what you expect also.
$ printf "%02d " {0..10}; echo
00 01 02 03 04 05 06 07 08 09 10
$ printf "%03d " {0..100}; echo
000 001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100
Just vary the field width in the format string (2 and 3 in this case) and of course the brace expansion range. The echo is there just for cosmetic purposes, since the format string does not contain a newline itself.
printf is a shell builtin, but you likely also have a version from coreutils installed, which can be used in-place.
awk only:
awk 'BEGIN { for (i=0; i<10; i++) printf("%02d ", i) }'
The following will work in bash
echo {01..10}
**EDIT seeing the answers around me I just wanted to add this, in the case we're talking about commands that will work under any terminal
yes | head -n 100 | awk '{printf( "%03d ", NR )}' ##for 001...100
or
yes | head -n 10 | awk '{printf( "%03d ", NR )}' ##for 01..10
echo 0{0..9}
You can get: 00 01 02 03 04 05 06 07 08 09
echo 0{0..9} 1{0..9}
You can get: 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19
echo 00{0..9} 0{10..99}
You can get 001 .. 099
There are so many ways to do this! My personal favorite is:
yes | grep y | sed 100q | awk '{printf( "%03d ", NR )}'; echo
Clearly, neither the sed nor the grep are necessary (the grep being far more trivial, since if you omit the sed you need to change the awk), but they contribute to the overall satisfaction of the solution! The final echo is not really necessary either, but it's always nice to have a trailing newline.
Another nice option is:
yes | nl -ba | tr ' ' 0 | sed 100q | cut -b 4-6
Or (less absurdly):
yes '' | sed ${top-100}q | nl -ba -w ${width-3} -n rz
as commented by favoretti, seq is your friend.
But there is a caveat:
seq -w uses the second argument to set the format it will use.
Thus, the command seq -w 1 9 will print the sequence 1 2 3 4 5 6 7 8 9
To print the sequence 01 .. 09 you need to do the following:
seq -w 1 09
Or for clarities sake use the same format on both ends, for instance:
seq -w 000 010 for the series 001 002 003 ... 010
And you can also use a step argument that also works in reverse:
seq -w 10 -1 01' for 10,09,08...01 orseq -w 01 2 10` for 01,03,05,07,09
cat file
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16
I tried with
column -c 4 file
to get an output with 4 columns, but it didn't work - I just get the same as the input.
Do I misunderstand the column man-page?
A second question: what format should the argument to the -s flag have?
Give this a try:
fold -w 12 file
The number 12 is the number of data columns * the number of characters in a column (two digits + one space). The -w option is for designating a screen width in terms of character columns.
The column command won't work for this because it's intended to format newspaper-style columns.
This comes close to working the way you want:
sed 's/ /\n/g' file | column -xc 35
The "35" is somewhat arbitrary, but any value from 32 to 39 will work in this case. It's related to the width of the fields (2 characters which is less than the width of a tab stop), the number of fields desired per line and the width of tab stops (8 characters). So, basically, 8 * 4 is 32.
Here's a demonstration of the -s option (which is used with -t):
$ echo -e "a;b|c\naaaaa;bbbbb|ccccc"|column -t -s ';|'
a b c
aaaaa bbbbb ccccc
Without using column, the output looks like:
$ echo -e "a;b|c\naaaaa;bbbbb|ccccc"
a;b|c
aaaaa;bbbbb|ccccc
Let's guess you want:
01 02 03 04
05 06 07 08
09 10 11 12
13 14 15 16
In this case:
$ xargs -n4 < file