Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

print output to same file

Status
Not open for further replies.

chz013

Programmer
Jan 5, 2003
73
US
Hi
I have a data file, named a_data, which contains like this:
3100340001|02.02.29|28800|03/20/2002|

I manage to write a simple awk script that converts
03/20/2002 to 2002-03-20:
ie
mlc_year = substr($4,7,4)
mlc_month = substr($4,1,2)
mlc_day = substr($4,4,2)
mlc_full_date = mlc_year"-" mlc_month "-" mlc_day
$4 = mlc_full_date
print $4

How do I write back to a_data file such that it contains
3100340001|02.02.29|28800|2002-03-20|

Any help is fully appreciated.
Thanks



 
Vlad
which problem does this solve using

#!/usr/bin/nawk -f

OR

#!/usr/xpg4/bin/awk -f

Yes I'm using Solaris SUNOS 5.8
 
well.... let me put it this WAY:

not using Solaris's OLD awk fixes MOST awk-related problems regarding old/incompatible/functionality-lacking features. ;)

In your particular instance, the OLD awk doesn't support FS as a 'full-blown' REGEX.

Use either of the awk interpreters I've mentioned above.


vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Vlad
thanks for the advice
using #!/usr/xpg4/bin/awk -f

prevents the error &quot;too many columns&quot;


 
Boy, I could be rich I'd charged a penny for every awk/nawk question under Solaris.........

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Vlad
Thanks for your help in previous sections.

Using the above awk file to change date format,
how do I change the awk script such that
everytime is called, a new output file is created.

ie for example,

i have 3 files.
abc_st_1130
abc_st_1200
abc_st_1330

filename listing contains this:
abc_st_1130
abc_st_1200
abc_st_1330

In each file, it contains a structure like this:
3100340001|02.02.29|28800|03/20/2002|

My script looks like this:

while read st_file ; do
myawk.awk st_file
done < listing

In myawk.awk, what do I add or change so that after
executing myawk.awk st_file,
a new file with such naming convention, changed_dateformat_abc_st_1130 is created. In it,
it should already contain
3100340001|02.02.29|28900|2002-03-20|

At the end of the execution, I should have
changed_dateformat_abc_st_1130
changed_dateformat_abc_st_1200
changed_dateformat_abc_st_1330




 
You can either do it in awk directly OR in the shell wrapper. Here's a sample how to do in a wrapper. I assume your file pathnames can be anything: absolute, relative or whatever.

#-------------- Myscript.sh
#!/bin/ksh
newPrefix='changed_dateformat_'

while read st_file ; do
myawk.awk st_file > &quot;$(dirname ${st_file})/${newPrefix}$(basename ${st_file})&quot;
done < listing
#----------------------------


#!/usr/xpg4/bin/awk -f

BEGIN {
FS=&quot;(\\|)|(,)&quot;
OFS=&quot;|&quot;
{
split($4, arr, &quot;/&quot;);
$4=arr[3] &quot;-&quot; arr[1] &quot;-&quot; arr[2]
print
}



vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top