Home > Mobile >  awk to strore string format in variable
awk to strore string format in variable

Time:10-09

In the below awk when I echo f it is empty, but if I remove the $f I get the desired results, however the new formatting is not stored in the $d variable. Basically I am trying to convert the string in $d variable into a new formatted variable $f. Thank you :).

file

ID,1A
DATE,220102

awk

d=$(awk -F, '/Date/ {print $2}' file) | f=$(date -d "$d"  '%Y-%m-%d')

f --- desired ---

2022-01-02

CodePudding user response:

You need to use it this way to return a value from awk and set a shell variable:

f=$(date -d "$(awk -F, '/DATE/ {print $2}' file)"  '%Y-%m-%d')

echo "$f"
2022-01-02

CodePudding user response:

With awk:

awk 'BEGIN{FS=","; OFS="-"} $1=="DATE"{ print "20" substr($2,1,2), substr($2,3,2), substr($2,5,2) }' file

Output:

2022-01-02

See: 8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR

CodePudding user response:

With your shown samples please try following awk code. Written and tested in GNU awk. Using awk's match function capability to use regex ^DATE,([0-9]{2})([0-9]{2})([0-9]{2})$ for getting required output. This creates 3 capturing groups and stores matched values into array named arr once this match is done then printing 20 and all 3 values of arrays separated by - as per required output.

awk -v OFS="-" '
match($0,/^DATE,([0-9]{2})([0-9]{2})([0-9]{2})$/,arr){
  print "20" arr[1],arr[2],arr[3]
}
'  Input_file

CodePudding user response:

While the other answers provide a more efficient method of reformatting the date (and assuming OP has no need for d in follow-on code), I'm going to focus solely on a couple issues with OP's current code:

  • in the awk script need to match for (all caps) DATE instead of Date
  • current code attempts to pipe the output from d=$(...) to the f=$(...) portion of code; while this does 'work' in that f will be assigned 2022-01-02 the problem is that the assignment to f is performed in a subprocess and upon exiting the subprocess f is effectively 'unassigned'; what OP really needs is to separate the d=$(...) and f=$(...) commands from each other so that both assignments occur in the current shell, and this can be done by replacing the pipe with a semicolon.

If we make these two simple edits:

# old code:

d=$(awk -F, '/Date/ {print $2}' file) | f=$(date -d "$d"  '%Y-%m-%d')
              ^^^^                   ^^^

# new code:

d=$(awk -F, '/DATE/ {print $2}' file) ; f=$(date -d "$d"  '%Y-%m-%d')
              ^^^^                   ^^^

OP's code will now generate the desired result:

$ echo "${f}"
2022-01-02

CodePudding user response:

the string approaches :

{n,g}awk -F'^[^,]*,' 'gsub("^....|..", "-&", $(_=!(NF*=NF==NR)))\
             ($ _ = substr($ _,  _ _--))^_'  OFS=20
mawk -F'^[^,]*,' '$(gsub("^....|..", "-&", 
                  $!(NF*=NF==NR))*(_=!NF)) = substr($_,  _ _)' OFS=20

mawk2 'gsub("^....|..", "-&", 
       $!(NF*=NF==NR))   sub(".",_)^_' FS='^. ,' OFS=20

the numeric approach :

mawk -F',' 'NF==NR && ($!NF = sprintf("20%.*s-%.*s-%0*.f", _ =_^=_<_,
                   __ = $NF, _  , substr(__,_), --_, __%(_ _*_*_)^_))'
2022-01-02
  •  Tags:  
  • awk
  • Related