LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 11-28-2005, 02:26 PM   #1
Kalyani1
Member
 
Registered: Oct 2005
Posts: 43

Rep: Reputation: 15
sed command


Hi All,

I have timefile. In the timefile I have bunch of times.
Ex: 16:23:45 (hour:min:time)
14:45:45
17:56:45

I would like to substitue hour filed (wholefile )with Question marks.

Thanks,
Kal
 
Old 11-28-2005, 02:53 PM   #2
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,950
Blog Entries: 5

Rep: Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755
This can be done simply with awk.

awk -F: '{print "??:"$2":"$3}' filename

The -F tells it to use colon ( as the field separator. The print then says to print out "??:" (a literal string) followed by the second field (minutes) a colon (another string) and finally the third field (seconds).

Since hours is the first field you don't print it at all - you just print the first literal string noted above.

filename of course is the name of the file that contains your times.
 
Old 11-28-2005, 03:03 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
if your format is like this:

hh:mm:ss

then tell sed to replace 2 characters at beginning of line with "??"

sed s/^../??/ filename
 
Old 11-28-2005, 04:31 PM   #4
Kalyani1
Member
 
Registered: Oct 2005
Posts: 43

Original Poster
Rep: Reputation: 15
Thank you so much

I have a Date and time

Eg: 2005-01-02 12:06:26 (I have to substitute the Question marks in the hour field)

How to write the command.

Thanks,
Kalyani
 
Old 11-28-2005, 04:51 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
See to post on how to do it with awk---ie the part about replacing particular fields.

sed looks only for text and its position in the line
awk is MUCH more powerful and therefor harder to learn

I have found excellent tutorials on both---just go to Google and type "sed" or "awk"
 
Old 11-28-2005, 05:49 PM   #6
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
Quote:
sed looks only for text and its position in the line
awk is MUCH more powerful and therefor harder to learn
I suppose that could be debated just as long a fruitless as "best distro"
Code:
sed 's/..:/??:/' file.txt
 
Old 11-28-2005, 06:45 PM   #7
Kalyani1
Member
 
Registered: Oct 2005
Posts: 43

Original Poster
Rep: Reputation: 15
Thank u somuch for the reply.

2005B2005-01-02 12:06:26, 2005-01-02 12:06:26

I used the sed command, it works fine, but it's changing the first time. I would like change the second time. I don't know is it possible or not.

Thanks,
Kalyani
 
Old 11-28-2005, 07:55 PM   #8
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
Can you post the actual file so we can see what it really looks like?
 
Old 11-28-2005, 10:17 PM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
sed works one line at a time.

check the manual for changing multiple occurences on one line. (man sed)
 
Old 11-29-2005, 08:43 AM   #10
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,950
Blog Entries: 5

Rep: Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755
Quote:
2005B2005-01-02 12:06:26, 2005-01-02 12:06:26
Assuming all your records are like the above (that is the time never occurs at the beginning of a line and always has a space before it then doing the following will work:

Code:
sed 's/ ..:/ ??:/g' filename
Notice in the above there is a space after the first slash (/) and before the two dots (..) and also a space after the second slash (/) and before the two question marks (??). This is essentially the same syntax you were given earlier but the spaces are there to make them part of the pattern and also "g" has been added after the third slash (/). The "g" tells it to do the change globally rather than just on the fist occurence.

You could also adapt the awk syntax I gave earlier but given your line I think the sed is probably a better way to go in this case.

FYI: In my opinion awk has more power because it has a full programming capability but for simple text manipulation its probably more correct to say that its a matter of preference than absolutes.

Last edited by MensaWater; 11-29-2005 at 08:46 AM.
 
Old 11-29-2005, 10:35 AM   #11
Kalyani1
Member
 
Registered: Oct 2005
Posts: 43

Original Poster
Rep: Reputation: 15
Thank you so much.

The sed command is works fine globally. Is there any way to change only second time occurrence.

Thanks,
kal
 
Old 11-29-2005, 10:54 AM   #12
Kalyani1
Member
 
Registered: Oct 2005
Posts: 43

Original Poster
Rep: Reputation: 15
This is My timefile.txt

MasterID,DropDateTime,DropLocation
20035B2005-01-12 16:01:32,2005-01-12 16:01:32,LYGames/ChangeReaction/Source/
20090A2005-01-31 09:07:06,2005-01-31 09:07:06,LYGames/Clue/Source/
20072A2005-02-14 16:28:26,2005-02-14 16:28:26,LYGames/EnchantedUnicorn20L/Source/
10508D2004-11-11 07:33:44,2004-11-11 07:33:44,LYGames/AfricanTreasure/Source/
20028B2005-02-03 12:14:30,2005-02-03 12:14:30,LYGames/BlackNWhite7s5R15L/Source/
10614C2005-01-19 11:10:10,2005-01-19 11:10:10,LYGames/BlackWhiteDJ/Source/
20026A2004-09-23 09:22:04,2004-09-23 09:22:04,LYGames/Blazing7s5rProgr/Source/
20162A2005-01-05 10:36:58,2005-01-05 10:36:58,LYGames/BonusFrenzy4R15L150Cr/Source/
20062A2005-01-05 13:37:14,2005-01-05 13:37:14,LYGames/BonusFrenzy4R15L450Cr/Source/
20013A2004-10-14 15:19:38,2004-10-14 15:19:38,LYGames/CopperDropperProg9L-old/Source/
20036B2004-11-02 10:54:08,2004-11-02 10:54:08,LYGames/FloridaDice3R3L/Source/
20037B2004-11-02 11:58:24,2004-11-02 11:58:24,LYGames/FloridaDice3R5L/Source/
10159B2005-03-01 14:37:00,2005-03-01 14:37:00,LYGames/FortuneFever5R20L/Source/
10161B2005-03-02 07:47:20,2005-03-02 07:47:20,LYGames/FortuneFever5R9L/Source/
20089A2004-10-14 16:19:04,2004-10-14 16:19:04,LYGames/GoodCents5R9L-Prog/Source/
10604C2005-02-09 08:31:22,2005-02-09 08:31:22,LYGames/GrandAve3R3L/Source/
10605C2005-02-09 08:41:10,2005-02-09 08:41:10,LYGames/GrandAve3R5L/Source/
10155B2005-02-03 14:13:34,2005-02-03 14:13:34,LYGames/HonkyTonk5R20L/Source/
10157B2005-02-04 15:37:34,2005-02-04 15:37:34,LYGames/HonkyTonk5R9L/Source/
20184B2005-01-20 12:50:10,2005-01-20 12:50:10,LYGames/HotLines3L/Source/
20074B2005-01-20 11:40:34,2005-01-20 11:40:34,LYGames/HotLines5L/Source/
10189C2005-01-06 14:29:06,2005-01-06 14:29:06,LYGames/JackpotCelebration5R9L-Prog/Source/
10151B2005-02-03 13:16:58,2005-02-03 13:16:58,LYGames/JumpinJoeys5R20L/Source/
10365B2005-02-07 09:07:20,2005-02-07 09:07:20,LYGames/JumpinJoeys5R9L/Source/
10376C2004-10-20 06:42:06,2004-10-20 06:42:06,LYGames/LittleGreenMen/Source/
10606C2005-02-09 08:52:18,2005-02-09 08:52:18,LYGames/MamboMadness3R3L/Source/
 
Old 11-29-2005, 11:32 AM   #13
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Still not sure exactly what you want to do....

you can write a sed script to make multiple commands easier to follow--other wise prefix every command with "-e" (see the manual)
Your file has some unique patterns: Try this:

sed -e 's/[A-Z]200./ ,20xx/' -e 's/,200./ ,20xx/' < file > newfile
The first expression after -e looks for the year preceded by any capital letter
The second expression looks for the year preceded by a comma
Note the quotes required because there are spaces in the expressions

Try this on your file and then go read one of the tutorials---including learning regular expressions. I recommend a good book on scripting, also.
 
Old 11-29-2005, 11:41 AM   #14
Kalyani1
Member
 
Registered: Oct 2005
Posts: 43

Original Poster
Rep: Reputation: 15
It's changing year filed.
In the year filed Eg: 2005 it's changed to 20xx.

I would like to change the second time occurrence only.
 
Old 11-29-2005, 12:01 PM   #15
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
Since you have a comma after the first time, you could use that as a separator....
Code:
sed 's/,\(.*\) ..:/,\1 ??:/g' file.txt
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sed command rharris72 Programming 6 11-20-2005 08:26 PM
sed command pazvant Linux - Software 2 05-09-2004 12:58 PM
sed command linuxdev Linux - Newbie 9 02-24-2004 04:50 PM
sed Command linuxdev Linux - Newbie 3 02-09-2004 11:27 AM
sed command kwigibo Linux - General 3 04-21-2002 04:11 PM


All times are GMT -5. The time now is 04:18 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration