LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   sed command (https://www.linuxquestions.org/questions/linux-software-2/sed-command-387332/)

Kalyani1 11-28-2005 02:26 PM

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

MensaWater 11-28-2005 02:53 PM

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.

pixellany 11-28-2005 03:03 PM

if your format is like this:

hh:mm:ss

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

sed s/^../??/ filename

Kalyani1 11-28-2005 04:31 PM

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

pixellany 11-28-2005 04:51 PM

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"

homey 11-28-2005 05:49 PM

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

Kalyani1 11-28-2005 06:45 PM

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

homey 11-28-2005 07:55 PM

Can you post the actual file so we can see what it really looks like?

pixellany 11-28-2005 10:17 PM

sed works one line at a time.

check the manual for changing multiple occurences on one line. (man sed)

MensaWater 11-29-2005 08:43 AM

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.

Kalyani1 11-29-2005 10:35 AM

Thank you so much.

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

Thanks,
kal

Kalyani1 11-29-2005 10:54 AM

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/

pixellany 11-29-2005 11:32 AM

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.

Kalyani1 11-29-2005 11:41 AM

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.

homey 11-29-2005 12:01 PM

Since you have a comma after the first time, you could use that as a separator....
Code:

sed 's/,\(.*\) ..:/,\1 ??:/g' file.txt


All times are GMT -5. The time now is 02:37 AM.