Question about sed. Inserting text in field?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Question about sed. Inserting text in field?
# 1  
Old 02-11-2007
Question about sed. Inserting text in field?

Hi,
I have tried to develop a sed script that inserts date and time in the third field in the first and second row below. The third row is an example and it shows where the date and time should be inserted. The script should check if the row already has date and time in the third field and if it is missing it should insert date and time.

1 106 texttexttext texttextexttext
4 1023 texttext texttext texttext

9 1234 07-02-05 16:06:46:743 texttext textextext text

Here is the script I have managed to develop so far. The script just insert date and time in the beginning of a line that miss the date and time and it will not be inserted in the third field. I would apreciate help to get date and time inserted correctly.

sed '/[0-9][0-9]*[0-9]* [0-9][0-9]*[0-9]*[0-9]* [0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]:[0-9][0-9][0-9]/!s/^/07-02-05 16:06:46:743: &/g'


/ Richard
# 2  
Old 02-11-2007
A few assumptions here:

1 ) The field separator is always a single space
2 ) Field one is left justified to the start of the line

That being said, just modify your sed command to position the date correctly removing the trailing ampersand:

sed '/[0-9][0-9]*[0-9]* [0-9][0-9]*[0-9]*[0-9]* [0-9][0-9]-[0-9][0-9]
-[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]:[0-9][0-9][0-9]/!s/^\([^ ]* [^ ]*\)
/\1
07-02-05 16:06:46:743 /g'


Given:
1 106 texttexttext texttextexttext
4 1023 texttext texttext texttext
9 1234 07-02-05 16:06:46:743 texttext textextext text

You get:
1 106 07-02-05 16:06:46:743 texttexttext texttextexttext
4 1023 07-02-05 16:06:46:743 texttext texttext texttext
9 1234 07-02-05 16:06:46:743 texttext textextext text

There is a lot of room for improvement to make the script more flexible, but this should get you going. Good luck!
# 3  
Old 02-12-2007
The script works just fine.
Thanks
Richard
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Inserting a field without disturbing field separator on other fields

Hi All, I have the input as below: cat input 032016002 2.891 97.109 16.605 27.172 24.017 32.207 0.233 0.021 39.810 0.077 0.026 19.644 13.882 0.131 11.646 0.102 11.449 76.265 23.735 16.991 83.009 8.840 91.160 0.020 99.980 52.102 47.898 44.004 55.996 39.963 18.625 0.121 1.126 40.189... (15 Replies)
Discussion started by: am24
15 Replies

2. UNIX for Dummies Questions & Answers

Inserting the last field first

I have a list of more than 1000 files on the following format. roman_pottery_in_the_archaeological_record_2007.pdf power_politics_and_religion_in_timurid_iran_2007.pdf toward_a_theory_of_human_rights_religion_law_courts_2006.pdf i_was_wrong_the_meanings_of_apologies_2008.pdf I want to... (2 Replies)
Discussion started by: kristinu
2 Replies

3. UNIX for Dummies Questions & Answers

Inserting text into a file with awk or sed

Hello, I've been trying to get a script working that fetches weather-data and converts it into an .ics file. The script works so far put I'm stuck at the point where I need to add specific static data. A thorough search through the forum did not point me into the right direction. #!/bin/bash... (3 Replies)
Discussion started by: Schubi
3 Replies

4. Shell Programming and Scripting

Help with sed and inserting text from another file

I need to insert text from one file into another file after specific term. I guess sed is the best method of doing this and I can insert a specified text string using this script but I am not sure how to modify it to insert text from another file: #!/bin/sh sed 's/\<VirtualHost... (17 Replies)
Discussion started by: barrydocks
17 Replies

5. Shell Programming and Scripting

Inserting some text if a field in the last column changes

Hi, I have a file which looks like this: A 01 00 B 02 00 C 04 00 D 00 01 E 01 01 F 02 01 G 01 04 H 02 04 I want to insert some text if the field if the last column changes. It should look like this: Value 00 A 01 00 B 02 00 C 04 00 Value 01 (6 Replies)
Discussion started by: wenclu
6 Replies

6. Shell Programming and Scripting

Inserting string in between field in comma separated file

Hello Mates, I have one txt file having commo seperated values. I have to insert string "FALSE" in 2nd field from the end. E.G SE18 6RN,,,,5439070,1786840,,1000002148671600,123434 Out put should be: SE18 6RN,,,,5439070,1786840,FALSE,1000002148671600,123434 Can some one help me to... (8 Replies)
Discussion started by: krsnadasa
8 Replies

7. Shell Programming and Scripting

Inserting a new field inbetween two exisitng field

I have a '|' delimited file. My file looks like below 23|nationalhoilday|feb12||||||||||||||california|northdistrict|| In the same way, each record has 164 fields. I have to insert one more field after the 85th field. Expected output... (3 Replies)
Discussion started by: machomaddy
3 Replies

8. Shell Programming and Scripting

Inserting text with SED

Hi guys, I need to insert @test.com after each entry in my .txt file. 1 2 3 4 1@test.com 2@test.com 3@test.com 4@test.com Tried to use cat test.txt |sed 's/$/@test.com/'but it does this instead: 1 @test.com 2 (6 Replies)
Discussion started by: spirm8
6 Replies

9. Shell Programming and Scripting

Inserting text to file, sed and variable not acting right

I want to put text stored in a variable into a file on the 7th line. I'm having trouble with this line: sed '7i\'$text'' $file It works perfectly for a single word, but fails if there are two words because of the space. I've tried several forms of quoting and this is the only one that... (2 Replies)
Discussion started by: fubaya
2 Replies

10. Shell Programming and Scripting

awk or sed to add field in a text file

Hi there, I have a csv file with some columns comma sepated like this : 4502-17,PETER,ITA2,LEGUE,92,ME - HALF,23/05/10 15:00 4502-18,CARL,ITA2,LEGUE,96,ME - HALF,20/01/09 14:00 4502-19,OTTO,ITA2,LEGUE,97,ME - MARY,23/05/10 15:00 As you can see the column n. 7 is a timestamp column, I need... (23 Replies)
Discussion started by: capnino
23 Replies
Login or Register to Ask a Question