1828221 Members
1941 Online
109975 Solutions
New Discussion

Re: file content

 
Pando
Regular Advisor

file content

dear gurus,

I have a file that contains the following line

...
...
ENDTIME,"" -
...
...

ENDTIME record format is not correct so i need to manipulate it.

I have created a script like this:

endtime=$(awk -F "," '/ENDTIME/ {print $2}' $FILE)
if [ $endtime = "/"" - /" ]
then
lot=$(basename $FILE)
lot=`echo $edlot|sed 's/\.tdf//'`
lotdate=$(echo $edlot|awk -F _ '{print $4}')
lottime=$(echo $edlot|awk -F _ '{print $5}')
echo $year$lotdate"-"$lottime
exit
fi

but nothing seems to happen.

I need to replace the endtime record if it has erraneous content.

Maximum points to all correct answers.
11 REPLIES 11
Muthukumar_5
Honored Contributor

Re: file content

Your file format is like this,

...
...
ENDTIME,"" -
...
...

how do you want to generate from this? plz specify the output you are executing. And more, your script is having more problem (without exact shell format). Revert back with details to get HUGE response(s).

-Muthu
Easy to suggest when don't know about the problem!
Pando
Regular Advisor

Re: file content

Hi Muthu,


Basically i have a filename named BGC5043_6678_DC2_1005_154042_S3.TDF.
The content of the file has a line ENDTIME,"" - , in it.
All i want is to replace "" - with the correct time format (yearmmdd-hhmmss). which is actually within the filename (4th & 5th field).
The shell format is POSIX.
Muthukumar_5
Honored Contributor

Re: file content

Script:

#!/bin/ksh
# Use format as: script.ksh
# EXample:
# ./script.ksh BGC5043_6678_DC2_1005_154042_S3.TDF

FILE=${1}
YD=$(echo "${FILE}" | awk -F"_" '{ print $4 }')
THS=$(echo "${FILE}" | awk -F"_" '{ print $5 }')

perl -pi -e "s%"" -%$YD-$THS%g" ${FILE}

# END #
exit 0


It will work.

# perl -pi -e "s%"" -%$YD-$THS%g" ${FILE}
# cat *.TDF
...
...
ENDTIME,""1005-154042
...
...
#

-Muthu
Easy to suggest when don't know about the problem!
Pando
Regular Advisor

Re: file content

Hi Muthu,

In your output file, ENDTIME,""1005-154042
the "" should be 2005.
Muthukumar_5
Honored Contributor

Re: file content

A single line script:


# perl -pi -e 'BEGIN{($YD,$HMS)=(split /_/,$ARGV[0])[3,4];}{s%ENDTIME,"".*$%ENDTIME,$YD-$HMS%g;}'

# perl -pi -e 'BEGIN{($YD,$HMS)=(split /_/,$ARGV[0])[3,4];}{s%ENDTIME,"".*$%ENDTIME,$YD-$HMS%g;}' BGC5043_6678_DC2_1005_154042_S3.TDF

# cat BGC5043_6678_DC2_1005_154042_S3.TDF
...
...
ENDTIME,1005-154042
...
...

-Muthu
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: file content

Use this script:

#!/bin/ksh
# Use format as: script.ksh
# EXample:
# ./script.ksh BGC5043_6678_DC2_1005_154042_S3.TDF

FILE=${1}
Y=$(date +'%Y')
MD=$(echo "${FILE}" | awk -F"_" '{ print $4 }')
THS=$(echo "${FILE}" | awk -F"_" '{ print $5 }')

perl -pi -e "s%ENDTIME,"".*$%ENDTIME,$Y$YD-$THS%g" ${FILE}

# END #
exit 0

# ksh script.ksh
will do it.

-Muthu
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: file content

Single line:

# perl -pi -e 'BEGIN{$Y=`date +'%Y'`;chomp($Y);($YD,$HMS)=(split /_/,$ARGV[0])[3,4];}{s%ENDTIME,"".*$%ENDTIME,$Y$YD-$HMS%g;}'

It will update into the file itself.

hth.
Easy to suggest when don't know about the problem!
Pando
Regular Advisor

Re: file content

Hi Muthu,

One more thing before i made my final points.
Is it possible to test if the ENDTIME field contains the date and time format like yyyymmdd-hhmmss?
I am asking this this because some files have the correct format and others do not have.
My plan is to correct the wrong format then process it. If it is already correct then it will process them as is.
I need to process 10,000 files in my hp-ux system. Thanks!
Muthukumar_5
Honored Contributor

Re: file content

# perl -pi -e 'BEGIN{$Y=`date +'%Y'`;chomp($Y);($YD,$HMS)=(split /_/,$ARGV[0])[3,4];}{s%ENDTIME,"".*$%ENDTIME,$Y$YD-$HMS%g;}'

Will change when ENDTIME is having format as,

ENDTIME,""

To make check if ENDTIME,YYYYMMDD-HHMMSS

# perl -pi -e 'BEGIN{$Y=`date +'%Y'`;chomp($Y);($YD,$HMS)=(split /_/,$ARGV[0])[3,4];}{s%ENDTIME,"".*$%ENDTIME,$Y$YD-$HMS%g if /ENDTIME,\d{6}-\d{6}/;}'

-Muthu
Easy to suggest when don't know about the problem!
H.Merijn Brand (procura
Honored Contributor

Re: file content

It's kinda non-perl to call external (unportable) date commands to generate a date stamp where perl has fine (and much faster) builtins to do that

> Is it possible to test if the ENDTIME
> field contains the date and time format
> like yyyymmdd-hhmmss?

either

# perl -e'BEGIN{@d=localtime;$ENDTIME=sprintf"%4d%02d%02d-%02d%02d%02d",$d[5]+1900,$d[4]+1,@d[3,2,1,0]} ...'

or (but slower) using POSIX

# perl -MPOSIX=strftime -e'BEGIN{ENDTIME=strftime"%Y%m%d-%H%M%S",localtime} ...'

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Pando
Regular Advisor

Re: file content

Hi Muthu,

Can you explain to me the line below. I think this would check if ENDTIME,YYYYMMDD-HHMMSS
,if not what would happen?

# perl -pi -e 'BEGIN{$Y=`date +'%Y'`;chomp($Y);($YD,$HMS)=(split /_/,$ARGV[0])[3,4];}{s%ENDTIME,"".*$%E
NDTIME,$Y$YD-$HMS%g if /ENDTIME,\d{6}-\d{6}/;}'