Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 乘或加更多的零_Bash_Awk - Fatal编程技术网

Bash 乘或加更多的零

Bash 乘或加更多的零,bash,awk,Bash,Awk,该文件包含YYYYMMDDHMMSS格式的日期。它的长度是8,10,12,14。 我希望输出为 cat file.txt 20150101 2015010103 2015010106 201501010901 2015010112 20150101150130 2015010118 20150101210150 如果缺少HHMMs,则输入000000。 我试图以数组和逐行方式读取整个文件,如下所示: 20150101000000 20150101030000 20150101060000 2

该文件包含YYYYMMDDHMMSS格式的日期。它的长度是8,10,12,14。 我希望输出为

cat file.txt

20150101
2015010103
2015010106
201501010901
2015010112
20150101150130
2015010118
20150101210150
如果缺少HHMMs,则输入000000。
我试图以数组和逐行方式读取整个文件,如下所示:

20150101000000
20150101030000
20150101060000
20150101090100
20150101120000
20150101150130
20150101180000
20150101210150
mapfile-s0-tstrdate

任何直接awk解决方案?

您可以使用for循环:

mapfile -s 0 -t strdate < file.txt
for pp in ${strdate[@]};do
  if [ `echo ${#pp}` == 8 ]; then
  newTime=$(( $pp*10000 ))
  ...
  fi
done

~$awk'{x=$0;for(i=length(x);i您可以在awk中使用
sprintf

~$ awk '{x=$0;for(i=length(x);i<14;i++)x=x"0";print x}' file.txt
20150101000000
20150101030000
20150101060000
20150101090100
20150101120000
20150101150130
20150101180000
20150101210150
或者,以下
printf
也适用:

awk '{s=sprintf("%-14s", $1); gsub(/ /, "0", s); print s}' file
20150101000000
20150101030000
20150101060000
20150101090100
20150101120000
20150101150130
20150101180000
20150101210150
这应该行得通

awk '{printf "%-d%0" (14-length($1)) "s\n", $1, ""}' file
20150101000000
20150101030000
20150101060000
20150101090100
20150101120000
20150101150130
20150101180000
20150101210150

awk'{while(length($0)
awk'{i=length($0);while(i++这里有一个有趣的方法,可以通过
coreutils
bash
重定向来实现:

awk '{i=length($0);while(i++<14)$0=$0"0"}1' File
使用Python:

20150101000000
20150101030000
20150101060000
20150101090100
20150101120000
20150101150130
20150101180000
20150101210150
sed下的相同逻辑:

#!/usr/bin/env python
import sys
for line in sys.stdin:
    print "%.14s" % (line.rstrip() + "0"*14)
sed-r的/$/00000000000000/;s/(.{14})。*/\1/'
20150101000000
20150101030000
20150101060000
20150101090100
20150101120000
20150101150130
20150101180000
20150101210150
$ cat file
20150101
2015010103
2015010106
201501010901
2015010112
20150101150130
2015010118
20150101210150

$ python -c $'import sys\nfor line in sys.stdin:\n\tprint "%.14s" % (line.rstrip() + "0"*14)' < file
20150101000000
20150101030000
20150101060000
20150101090100
20150101120000
20150101150130
20150101180000
20150101210150
#!/usr/bin/env python
import sys
for line in sys.stdin:
    print "%.14s" % (line.rstrip() + "0"*14)
sed -r  's/$/00000000000000/;s/(.{14}).*/\1/' < file