简体   繁体   中英

Awk to put date in blank column in format MMYYYY

I am trying to put date in column if column 1 is empty. So tried below code but doesn't work. Here input file is pipe separated, so need output also pipe separated

code 1

if ($1=="") {$1="date +%m%Y"}

code 2

if ($1=="") {$1=`date +%m%Y`}

code 3

if ($1=="") {$1=("Time = %m%Y", systime());}

required output

|042018|

sample input

ourceIifier|SourleName|GntCode|Dision|Suvision|ProfitCe1|Profie2|Plade|Retuiod|SuppliN
SAP|SAP_OSR_INV||||||||08AAACT2T|

Expected ouput

ourceIifier|SourleName|GntCode|Dision|Suvision|ProfitCe1|Profie2|Plade|Retuiod|SuppliN
SAP|SAP_OSR_INV|||||||042018|08AAACT2T|

my code :

awk -F"|" -v OFS="|" 'NR>1{
if (match($14,/[0-9]+\.[0-9]+\.[0-9]{4}$/)) {
split ($14, mdy, ".");
$14 = sprintf ("%02d-%02.f-%02.f", substr(mdy[3],0,4), mdy[1], mdy[2]);
if ($1=="") {$1="SAP"}
if ($2=="") {$2="SAP_OSR_INV"}
if ($9=="") {$9="date +%m%Y"}
if ($26 ~ /^[0-9]$/) {$26=sprintf("%02.f",$26)}
if ($58~/^1000$/) {$10="27AAACT2438A1ZT";}}1' input.csv > output.csv

Here in column9($9) statement "if ($9=="") {$9="date +%m%Y"}" i want to put date.

您忘记输出结果并且没有正确获取日期,这个有效:

awk -v date="$(date +"%m%Y")" '$1=="" {$1=date} {print $0}'

If your awk is gawk (GNU), to avoid an external command in a subshell:

$1=strftime("%m%d%y")

as well systime as strftime require gawk
(only really tested with mawk & original-awk)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM