how to sed in this case


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to sed in this case
# 1  
Old 09-13-2007
how to sed in this case

I want to change time format

For example ,

1:1:15 = Change to => 01:01:15
10:8:20 = Change to => 10:08:20
22:10:2 = Change to => 10:10:02


Thank in advance
# 2  
Old 09-13-2007
Code:
sed 's/:/  /g;s/^/  /g;s/$/  /g;s/ \(.\) / 0\1 /g;s/  /:/g;s/^://;s/:$//'



Code:
echo "3:1:12" | sed 's/:/  /g;s/^/  /g;s/$/  /g;s/ \(.\) / 0\1 /g;s/  /:/g;s/^://;s/:$//'

03:01:12


command should be much shorter with fprint

edit: printf not fprint Smilie

Last edited by funksen; 09-13-2007 at 02:52 PM..
# 3  
Old 09-13-2007
Code:
echo "1:1:15" | awk -F: '{ printf("%02d:%02d:%02d\n", $1, $2, $3); }'

# 4  
Old 09-14-2007
MySQL Thank You

Thank u for all answer and it's work. SmilieSmilie

PS , Could both of you to explain the code that u given?

Last edited by unitipon; 09-14-2007 at 01:28 AM.. Reason: Need the explaination
# 5  
Old 09-14-2007
Basically, in the awk script, all the work is done by the printf function. Awk is needed only to split the string in three separate variables, using the colon as delimiter.
I suggest you a "man printf" and "man formats", because this function is very powerful and can accept a large amount of string format specification. In our case, we need to print left padded numbers with zeroes, two characters as total length. You achieve this with the format string "%02d", where "d" represents an integer, "0" is the padding character and 2 is the length.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Making SED case insensitive

Dears, In the below string, please let me know how to make the sed search case-incensitive. I have more such lines in my script instead of let me know any other easier option. sed -n '/dn: MSISDN=/,/^\s*$/p' full.ldif > temp ; sed -n... (4 Replies)
Discussion started by: Kamesh G
4 Replies

2. Shell Programming and Scripting

Awk-sed : Question getting case sensitive filed.

Experts, Good day!, I have following data, I want to filter 3rd Field, if it is in UPPERCASE, aa aa1 HOST1 aa bb1 host2 aa cc1 SERV1 aa dd1 SERV2 ab aa1 host3 The output should be: aa aa1 HOST1 aa cc1 SERV1 aa dd1 SERV2 (2 Replies)
Discussion started by: rveri
2 Replies

3. Shell Programming and Scripting

making sed command case insensitive

i have something like this in a file cat onlytables.sql create table NextID ( id int auto_increment, zoneID int, entityName varchar(64), nextID int, lastModified TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, primary... (6 Replies)
Discussion started by: vivek d r
6 Replies

4. UNIX for Dummies Questions & Answers

Using sed for case insensitive search

Hi, I have a file named "test_file" that has the below content. It has words in upper/lower cases PRODOPS prodOPS ProdOps PRODops escalate Shell My requirement is to replace all the "prodops" (what ever case it may be) with "productionoperations". I tried using the "i" option with... (7 Replies)
Discussion started by: sbhuvana20
7 Replies

5. Shell Programming and Scripting

sed ignoring case for search but respecting case for subtitute

Hi I want to make string substitution ignoring case for search but respecting case for subtitute. Ex changing all occurences of "original" in a file to "substitute": original becomes substitute Origninal becomes Substitute ORIGINAL becomes SUBSTITUTE I know this a little special but it's not... (1 Reply)
Discussion started by: kmchen
1 Replies

6. Shell Programming and Scripting

Ignoring case in sed search

I am getting a parameter from a user and I need to use it to search and return the matching line numbers in a file. I am using this code: recordNumber="$(sed -n '/'"$entry"'/{ = d }' unixdb1.txt)" where $entry is the passed search parameter. The problem is I need to ignore the case. ... (3 Replies)
Discussion started by: snag49ers
3 Replies

7. Shell Programming and Scripting

How to make sed case insensitive

I need to remove a pattern say, ABCD whether it is in uppercase or lowercase from a string. How to do it using SED? for example ABCDEF should output to EF abcdEF should also output to EF (2 Replies)
Discussion started by: vickylife
2 Replies

8. UNIX for Dummies Questions & Answers

Case statement/sed command

The file dbnames.txt has 5 columns, what i'm trying to do is that when the fifth column equals A, store in the variable "access" the word, "admin access". If it equals B, then "business access" etc. I think their is a problem with my sed command, because it is not substibstituting the words... (1 Reply)
Discussion started by: ross_one
1 Replies

9. UNIX for Dummies Questions & Answers

Sed - Lower case single characters

Hello, I have a file where I am supposed to convert all the single i characters to uppercase, but when I try, it converts all the i's inside of words to uppercase as well. I tried doing: cat filename | sed 's/i/I/g' but that obviously does not work. Any help would be greatly... (6 Replies)
Discussion started by: zlindner
6 Replies

10. UNIX for Dummies Questions & Answers

variables use upper case? sed : output to the same file?

Hi, Q1: are the variables in shell script usually UPPER CASE? Q2: can sed output to the same file that it's using it? eg. cat sameFile | sed 's/here/there/g' > sameFile ? I expect the sed replace all "here" to "there" and change it in sameFile. i tried that one, the sameFile... (1 Reply)
Discussion started by: gusla
1 Replies
Login or Register to Ask a Question