Add additional numbers to file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add additional numbers to file
# 1  
Old 03-03-2010
Add additional numbers to file

I need to change the following field from:

Code:
"7/3/2009 7:07:12 PM","12345676","ok","8674"
"6/3/2009 8:07:12 PM","12345676","ok","8674"
"5/1/2009 7:07:12 PM","12345676","ok","8674"
"4/9/2009 3:07:12 AM","12345676","ok","8674"
"3/8/2009 3:07:12 PM","12345676","ok","8674"
"2/7/2009 4:07:12 PM","12345676","ok","8674"
"1/6/2009 5:07:12 PM","12345676","ok","8674"
"8/5/2009 6:07:12 PM","12345676","ok","8674"
"9/4/2009 9:07:12 PM","12345676","ok","8674"
"10/2/2009 10:07:12 PM","12345676","ok","8674"

to

Code:
"07/03/2009 19:07:12","12345676","ok","8674"
"06/03/2009 20:07:12","12345676","ok","8674"
"05/01/2009 19:07:12","12345676","ok","8674"
"04/09/2009 3:07:12","12345676","ok","8674"
"03/08/2009 15:07:12","12345676","ok","8674"
"02/07/2009 16:07:12","12345676","ok","8674"
"01/06/2009 17:07:12","12345676","ok","8674"
"08/05/2009 18:07:12","12345676","ok","8674"
"09/04/2009 21:07:12","12345676","ok","8674"
"10/02/2009 22:07:12 ","12345676","ok","8674"

I was trying sed which does not work, but I have about 1000 records, others with this type of format so it would probably need to be done in a loop?

I cannot get to add the 0 at the front of the date and cannot convert the 24hour process

I am using Korn Shell (solaris)

Last edited by zaxxon; 03-03-2010 at 06:24 AM.. Reason: use code tags please, ty
# 2  
Old 03-03-2010
This Perl code will solve your requirement ,
Code:
use strict;
use warnings;


#my $a ="\"7/3/2009 7:07:12 PM\",\"12345676\",\"ok\",\"8674\"";
open FH , "log.txt" ;  # Log file name 
my $a ;
while($a= <FH>)
{
        $a =~ /\s([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})\s+(AM|PM)/;
        my $format = $4 ;
                if ( $format eq "PM" )
                {
                        my $hr=12+$1;
                        if ( $hr ==24 )
                        {
                            $hr ="00";
                        }
my $fin= " $hr:$2:$3" ;
        $a =~ s/\s([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})\s+(AM|PM)/$fin/;
                }
                else {
                        $a=~ s/AM//;
                }
        print $a ;
    }

# 3  
Old 03-03-2010
apologies I don't use perl, so i don't understand the code
# 4  
Old 03-03-2010
You try the following code.

Code:
sed 's/,/ /g;s/"//g;' file > tmp
while read line
do
date=`echo $line | cut -d ' ' -f 1`

day=`echo $date | cut -d '/' -f 1`

mon=`echo $date | cut -d '/' -f 2`

year=`echo $date | cut -d '/' -f 3 `

time=`echo $line | cut -d ' ' -f 2`

format=`echo $line | cut -d ' ' -f 3`

data=`echo $line | cut -d ' ' -f 4- --output-delimiter='","'`

hour=`echo $time | cut -d ':' -f 1 `

Minsec=`echo $time | cut -d ':' -f 2-`

if [[ $format == 'PM' ]]
then
let hour+=12
fi
hour=`printf "%02d" $hour`
mon=`printf "%02d" $mon`
day=`printf "%02d" $day`
echo \"$day/$mon/$year $hour:$Minsec\",\"$data\"
done < tmp
rm -rf tmp


Last edited by vivekraj; 03-03-2010 at 07:11 AM..
# 5  
Old 03-03-2010
Try this: easy to understand. "file" is your input file.

Code:
#!/bin/sh

while read line
do
newstr=`echo $line | awk '{print $1}' | sed 's/"//g'`
day=`echo $newstr | cut -d "/" -f2`
month=`echo $newstr | cut -d "/" -f1`
year=`echo $newstr | cut -d "/" -f3`

timestr=`echo $line | awk '{print $2}'`
time_ind=`echo $line | awk '{print $3}' | cut -c 1-2`
hour=`echo $timestr | cut -d ":" -f1 `
min=`echo $timestr | cut -d ":" -f2 `
sec=`echo $timestr | cut -d ":" -f3 `

if [ "${time_ind}" == "PM" ]; then
   new_hour=$(( $hour + 12 ))
else
  new_hour=$hour
fi

if [ $day -lt 10 ];then
  new_day=`echo "0$day"`
else
  new_day=$day
fi

if [ $month -lt 10 ]; then
  new_month=`echo "0$month"`
else
  new_month=$month
fi

part_str=`echo $line | awk '{print substr($0,index($0,"M\"")+ 2)}'`
echo "\"$new_month/$new_day/$year $new_hour:$min:$sec\"$part_str"

done < file

cheers,
Devaraj Takhellambam
# 6  
Old 03-03-2010
hi thanks for all the replies

Thanks to Devaraj, your code worked great
# 7  
Old 03-03-2010
MySQL

You can easily do this with the help of sed and date commands.

Try the following,

Code:
while read line
do
line=`sed -r "s/^\"([0-9]{1}\/)(.+)/\"0\1\2\n/g" <<<$line`
time=`sed -r "s/^\".+\s([^\"]+\s[^\"]+)\".+/\1/g" <<<$line`
hour=`date -d "$time" | cut -d ' ' -f 5`
result=`sed -r "s/^(\".+\s)([^\"]+\s[^\"]+)(\".+)/\1$hour\3/g" <<<$line`
echo $result
done <"inputfile"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

find -exec How to add additional parameter when calling a funtion

Hello Current working script is : # # my_script BEGIN # function a_function { FIRST_PARAM="$1" DO_SOMETHING "$FIRST_PARAM" } export -f a_function START_HERE="/home/some_user/Documents" find $START_HERE" -exec bash -c 'a_function "$0" ' {} \; (5 Replies)
Discussion started by: jcdole
5 Replies

2. Shell Programming and Scripting

awk to extract multiple values from file and add two additional fields

In the attached file I am trying to use awk to extract multiple values and create the tab-delimited desired output. In the output R_Index is a the sequential # and Pre_Enrichment is defaulted to .. I can extract from the values to the side of the keywords, but most are above and I can not... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. AIX

Add additional swap place on AIX server

Can anyone help me the detailed procedure and commands to follow to add additional swap on aix server . My system shows following as of now , #lsattr -E -l sys0 -a realmem realmem 13893632 Amount of usable physical memory in Kbytes False #pstat -s PAGE SPACE: USED PAGES FREE... (7 Replies)
Discussion started by: gull05
7 Replies

4. Shell Programming and Scripting

How to add line numbers (multiples of 5: 0,5,10,15,20) to a text file?

Hi, I need to number the lines in my text file. I know how to do this with standard numbering (1,2,3,4, etc) but I need to count in multiples of 5, beginning 0,5,10,15... example existing file: abcd efg hijklm nopqrs desired output 0 abcd 5 efg 10 hijklm 15 ... (11 Replies)
Discussion started by: livbaddeley
11 Replies

5. Shell Programming and Scripting

add a additional column in csv file

Thanks for allwoing me to discuss in this forum GIVEN BELOW A simple shell script which will ask for the user to input a PC name and it will produce the output in CSV with the PC name #! /bin/bash read -p "enter the PC name :" pc #checking for netstat report netstat -pant |sed '1,2d'... (1 Reply)
Discussion started by: ayyappancheta
1 Replies

6. UNIX for Dummies Questions & Answers

Add floating point numbers from file

How do I use bash to add all the floating point numbers saved in a file like this? 490.47 244.61 263.07 131.59 246.81 115.20 (3 Replies)
Discussion started by: locoroco
3 Replies

7. Shell Programming and Scripting

Add numbers in a file

Hello, How to add numbers that are read from a file /tmp/test The content of the file look like 1234 234 432 1235 123 I read the file content in a for loop f=/tmp/test for i in `cat $f` do . . done Santhosh (11 Replies)
Discussion started by: mvsanthosh
11 Replies

8. AIX

How to add/remove additional DNS and IP to AIX

Hello, How to add/remove additional DNS and IP to AIX ? I wanted to add 3 more new DNS and IP addresses to existing AIX 5.2. (1 Reply)
Discussion started by: balareddy
1 Replies

9. Shell Programming and Scripting

how to add line numbers in text file

Hi all How to add line numbers in text file.. ex abcd cdef result 1. abcd 2. cdef thx in advance (4 Replies)
Discussion started by: suryanarayana
4 Replies

10. Shell Programming and Scripting

Add leading zeroes to numbers in a file

Hello, I am (trying) to write a script that will check to see how many users are logged on to my machine, and if that number is more than 60 I need to kill off all the oldest sessions that are over 60. So far I have been able to check how many users are on and now I am at the part where I have to... (3 Replies)
Discussion started by: raidzero
3 Replies
Login or Register to Ask a Question