Re: Help required on setting date variable (175 Views)
Reply
Frequent Advisor
Gaby1110
Posts: 56
Registered: ‎08-17-2009
Message 1 of 7 (175 Views)
Accepted Solution

Help required on setting date variable

Hi,

Need your help.I need to get the date from the second line of a file

[devuser@vfabuser ~]$ head -n2 ETCTRN4010000A.092309.023021001.txt | tail -n1

02,3009483,121000248,1,090922,,,/
[devuser@vfabuser ~]$

If you see above, I need 090922 stored to a variable as ‘22-SEP-2009’

Please help.

Thanks
Gaby1110
Acclaimed Contributor
Dennis Handly
Posts: 25,272
Registered: ‎03-06-2006
Message 2 of 7 (175 Views)

Re: Help required on setting date variable

# get month abbreviations in abmon
eval $(locale -k abmon | sed 's/";"//g')

awk -v abmon=$abmon -F, '
NR == 2 {
# print $5
year=substr($5,1,2) + 2000
mon=substr($5,3,2)
day=substr($5,5,2)
print day "-" substr(abmon, 3*(mon)-2, 3) "-" year
}' ETCTRN4010000A.092309.023021001.txt
Acclaimed Contributor
Dennis Handly
Posts: 25,272
Registered: ‎03-06-2006
Message 3 of 7 (175 Views)

Re: Help required on setting date variable

To get it into a variable, wrap the whole thing in $()
x=$(
awk -v abmon=$abmon -F, '
...
}' ETCTRN4010000A.092309.023021001.txt)
Frequent Advisor
Gaby1110
Posts: 56
Registered: ‎08-17-2009
Message 4 of 7 (175 Views)

Re: Help required on setting date variable

Thanks Dennis.

I have created a script named test.sh. Could you please let me know if this is correct?

more test.sh
# get month abbreviations in abmon
eval $(locale -k abmon | sed 's/";"//g')
x=$(
awk -v abmon=$abmon -F, '
NR == 2 {
# print $2
year=substr($2,1,2) + 2000
mon=substr($2,3,2)
day=substr($2,5,2)
print day "-" substr(abmon, 3*(mon)-2, 3) "-" year
}'ETCTRN4010000A.092309.023021001.txt)

Thanks
Gaby1110
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 5 of 7 (175 Views)

Re: Help required on setting date variable

Hi:

> Could you please let me know if this is correct?

Why don't you try running it? "Correct" is whatever faithfully produces the desired output without false results in this case. You can't hurt anything by trying!

That said, the last line needs a space added. Instead of:

}'ETCTRN4010000A.092309.023021001.txt)

You should have:

}' ETCTRN4010000A.092309.023021001.txt)

That is, there is one or more whitespaces avter the closing tick for the 'awk' script, before the name of the file you want to process.

Regards!

...JRF...
Frequent Advisor
Gaby1110
Posts: 56
Registered: ‎08-17-2009
Message 6 of 7 (175 Views)

Re: Help required on setting date variable

Thanks all for your help . Will assign the points..
Gaby
Acclaimed Contributor
Dennis Handly
Posts: 25,272
Registered: ‎03-06-2006
Message 7 of 7 (175 Views)

Re: Help required on setting date variable

>I have created a script named test.sh. Could you please let me know if this is correct?

I would include this at the top of your script:
#!/usr/bin/ksh
(Or sh.)

You initially had field 5 and now you have 2.

Note that once you get it into a variable, you would have to use that variable in your script.
Or you could make this script echo the value and then you can use this script inside other scripts.

So how do you intend to use that script? I assumed you would take my script fragment and insert it into another script.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.