Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

Help required on setting date variable

SOLVED
Go to Solution
Highlighted
Frequent Advisor

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
1 ACCEPTED SOLUTION
Acclaimed Contributor Acclaimed Contributor

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
6 REPLIES
Acclaimed Contributor Acclaimed Contributor

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 Acclaimed Contributor

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

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 Acclaimed Contributor

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

Re: Help required on setting date variable

Thanks all for your help . Will assign the points..
Gaby
Acclaimed Contributor Acclaimed Contributor

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.