Removing 0 from a specific position - if it exists


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing 0 from a specific position - if it exists
# 1  
Old 03-03-2011
Removing 0 from a specific position - if it exists

I have a file that I need to parse using a script. The dates in the file are displayed in the format:
Code:
Mar 2, 2011 9:09:31 PM

I have tried using the date command %e and %l but it pads an extra space for the day and hour if they are single digits.
So this I used a normal date command:
Code:
date=`date '+%b %d, %Y %r'`

and I'm trying to remove the 0s at position 5 and 14 (eg: Mar 03, 2011 11:38:35 AM)
Is there someway that I can do this. I tried the sed command
Code:
sed -e s/.//5 -e s/.//13

but it will remove anything at position 5 and 14. I want it to remove the value only if it is 0 (zero).

Any help is greatly appreciated.

Last edited by pludi; 03-03-2011 at 02:41 PM..
# 2  
Old 03-03-2011
Code:
sed 's/^\(....\)0/\1/' input

could also be written
Code:
sed 's/^\(.\{4\}\)0/\1/' input

Code:
sed 's/^\(.\{13\}\)0/\1/' input

My advice : Take care : since you remove the 0 instead of replacing it with a <space> character you should FIRST remove the 0 that is the most on the RIGHT

Consider the following example :

Code:
Mar 03, 2011 10:38:35 AM

the 0 of the 10 is currently at the 15th position :

if you remove the 0 on fifth position, all character will be shifted to left so that the 0 of the 10 will be on the 14th position

Look the difference :
Code:
# echo "Mar 03, 2011 10:38:35 AM" | sed 's/^\(....\)0/\1/;s/^\(.\{13\}\)0/\1/'
Mar 3, 2011 1:38:35 AM

Code:
# echo "Mar 03, 2011 10:38:35 AM" | sed 's/^\(.\{13\}\)0/\1/;s/^\(....\)0/\1/'

Mar 3, 2011 10:38:35 AM

Another solution could be to replace the unexpected 0 with a space character, so the characters will keep their positions.

Last edited by ctsgnb; 03-03-2011 at 03:45 PM..
This User Gave Thanks to ctsgnb For This Post:
# 3  
Old 03-03-2011
HOW ABOUT THIS?
Code:
date '+%b %d, %Y %r' |awk -F"[ ,:]" '{$2+=0;$6+=0;print $1" " $2", "$3" "$4" "$5":"$6":"$7" "$8}'

# 4  
Old 03-03-2011
Thanks, this works like a charm.
I really didn't think about the part where we will have to remove the right 0 first, and then remove the left one.
Sometimes you just miss the obvious.

Thanks a whole bunch.
# 5  
Old 03-03-2011
Alternatively, since in this format a leading zero is always preceded by a space, the following should also work:

Code:
sed 's/ 0/ /g'

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 6  
Old 03-03-2011
wow! plain and simple. And to think i was complicating this so much.
# 7  
Old 03-03-2011
Quote:
Originally Posted by crazyideas
I have a file that I need to parse using a script. The dates in the file are displayed in the format:
Code:
Mar 2, 2011 9:09:31 PM

I have tried using the date command %e and %l but it pads an extra space for the day and hour if they are single digits.
can be fixed directly by sed:

Code:
your_date_command |sed 's/  */ /g'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count specific character of a file in each line and delete this character in a specific position

I will appreciate if you help me here in this script in Solaris Enviroment. Scenario: i have 2 files : 1) /tmp/TRANSACTIONS_DAILY_20180730.txt: 201807300000000004 201807300000000005 201807300000000006 201807300000000007 201807300000000008 2)... (10 Replies)
Discussion started by: teokon90
10 Replies

2. Linux

Removing a character at specific position in a column

Hi, I have a file like this (about 8 columns in total, this being the 2nd column) gi_49482297_ref_YP_039521.1_ gi_49482297_ref_YP_039521.1_ gi_49482315_ref_YP_039539.1_ gi_49482315_ref_YP_039539.1_I want to remove the _ at the end of the line. And at later stages I would want to replace the... (5 Replies)
Discussion started by: Syeda Sumayya
5 Replies

3. Shell Programming and Scripting

Remove only specific char on every line when exists

Hi I need to remove "|" char when it's the last char of the line. Input file: generoso|desprendido|altruista| abnegar|ceder|sacrificar| abocetado-da|esbozado| apuntado|insinuado|incompleto abocetar|esbozar|bosquejar| diseņar|delinear ------------------------ output need --- ... (11 Replies)
Discussion started by: lookoo
11 Replies

4. Shell Programming and Scripting

Insert | in specific position

Hi , I have a file which has line similar to below 13123324234234234234234234234234234 3454546456dfhgfhgh454645654asdasfsdsddfgdgdfg 345345345mnmnbmnb346mnb4565464564564645645 Not for each line for specific position I need to insert some '|' Positions are fixed. Like 3,5,9,11 So the... (5 Replies)
Discussion started by: Anupam_Halder
5 Replies

5. Shell Programming and Scripting

Print in a specific position

HI My input is 12345678901234567890123456789012345678 pay 234 56789.23 7788934.5 2456 calcul 123 456 678 98 i want to print the line starting with "pay" as $2 should commence from 7 irresptive of its length and $3 to start from 14 and $4 to start from 24 and $5 from 36.... (4 Replies)
Discussion started by: Indra2011
4 Replies

6. Shell Programming and Scripting

substitute a string on a specific position for specific lines

I woud like to substitue a string on a specific position for specific lines I've got a file and I would like to change a specific string from "TOCHANGE" to "ABCABCAB" For every line (except 1,2, 3 and the last one) , I need to check between the 9th and the 16th digits. For the 3rd line, I... (7 Replies)
Discussion started by: BSF
7 Replies

7. Shell Programming and Scripting

using awk removing newline and specific position

Hello Friends, Input File looks as follows: >FASTA Header1 line1 line2 line3 linen >FASTA Header2 Line1 Line2 linen >FASTA Header3 and so on ....... Output: Want something as: >FASTA Header1 line1line2line3linen >FASTA Header2 (5 Replies)
Discussion started by: Deep9000
5 Replies

8. Shell Programming and Scripting

Using sed to replace specific character and specific position

I am trying to use sed to replace specific characters at a specific position in the file with a different value... can this be done? Example: File: A0199999123 A0199999124 A0199999125 Need to replace 99999 in positions 3-7 with 88888. Any help is appreciated. (5 Replies)
Discussion started by: programmer22
5 Replies

9. Shell Programming and Scripting

While file exists with specific extension

Hi, I need some help to see if I this is posible with a while loop. I need to run a script once a day that will take all of the *.TXT files in a folder and rename them to a specific file name structure with a .dat extension. I wrote this script, but I get an error that says " line 3: too many... (2 Replies)
Discussion started by: strpwr
2 Replies

10. Shell Programming and Scripting

Removing spaces at particular position

I have a file with delimiter ~ ABC~12~43~TR ~890~poi~YU ~56~65 What I want is to remove spaces from column 4,7 and other columns as it is So, the final file becomes ABC~12~43~TR~890~poi~YU~56~65 (7 Replies)
Discussion started by: superprogrammer
7 Replies
Login or Register to Ask a Question