Add additional numbers to file


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

Code:
awk -F" |," 'BEGIN{OFS=",";t="\""}{gsub("\"","",$0)
split($1,a,"/")
if($3=="PM"){split($2,b,":")
print t (length(a[1])==1?"0"a[1]:a[1])"/"(length(a[2])==1?"0"a[2]:a[2])"/"a[3]" "b[1]+12":"b[2]":"b[3] t OFS t $4 t OFS t $5 t}
else{print t (length(a[1])==1?"0"a[1]:a[1])"/"(length(a[2])==1?"0"a[2]:a[2])"/"a[3]" "$2 t OFS t $4 t OFS t $5 t}}' infile

# 9  
Old 03-03-2010
Code:
awk -F[\"/\ :] '{if ($8~/PM/) $5=$5+12}; 
                     {printf "\"%02d\/%02d\/%d %02d:%02d:%02d\",\"%s\",\"%s\",\"%s\"\n",$2,$3,$4,$5,$6,$7,$10,$12,$14}'

# 10  
Old 03-04-2010
just sed

Code:
cat input7.txt | sed 's@\(["/]\)\([0-9]\)/@\10\2/@
s@\(["/]\)\([0-9]\)/@\10\2/@
s@\( \)\([0-9]\):@\10\2:@'

This could probably be simplified with a look ahead expression for the '/' character in dates.
# 11  
Old 03-05-2010
...just because i love my good friend awk, here's that as a one liner

Code:
cat testfile | awk '{ split($2, b, ":"); if (b[2]+12 == 24) b[2]=00; split($1, d, "\/"); split(d[1], e, "\""); printf("\"%02d\/%02d\/%d %02d:%02d:%d %s\n", e[2], d[2], d[3], b[1]+12, b[2], b[3],$3); }'


Or in easy to read form:
Code:
cat testfile | awk '{ 
split($2, b, ":"); 
if (b[2]+12 == 24) b[2]=00; 
split($1, d, "\/"); 
split(d[1], e, "\""); 
printf("\"%02d\/%02d\/%d %02d:%02d:%d %s\n", 
e[2], d[2], d[3], b[1]+12, b[2], b[3],$3); }'

Turns this:
Code:
"7/3/2009 7:07:12 PM","12345676","ok","8674"

Into this:
Code:
"07/03/2009 19:07:12 PM","12345676","ok","8674"


You could even run it against tail -f with with tee to correct it live:
Code:
tail -f testfile | tee -a newfile | awk '{ split($2, b, ":"); if (b[2]+12 == 24) b[2]=00; split($1, d, "\/"); split(d[1], e, "\""); printf("\"%02d\/%02d\/%d %02d:%02d:%d %s\n", e[2], d[2], d[3], b[1]+12, b[2], b[3],$3); }'


I dunno, it was already solved but i feel like awk is still a cool tool

Last edited by radoulov; 03-05-2010 at 03:10 PM.. Reason: Please use code tags!
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