Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using sed to do string substitution

Status
Not open for further replies.

pdtt

Programmer
Sep 24, 2002
35
CA
Hi,

I am new to sed. I have to modify a tab delimitted text file that is not formatted correctly for my use. I have to do a replacement.
I have date fields that are formatted like this:
mm/dd/yyyy. If the date field is null, the file is outputting / / .

If I know what number tab the field starts with, can I wrap the good dates with double quotes so they end up like this: "mm/dd/yyyy" and can I strip out the / / fields and replace them with ""?

Any help is much appreciated.

Thank you
 
I don't understand what you want. Post some sample input and output data.

CaKiwi

"I love mankind, it's people I can't stand" - Linus Van Pelt
 
Well, if I'm reading your question right, try this. Let's say your date is between the third and fourth tab, and if it's not a valid date, it is "
Code:
<space>/<space>/<space>
&quot;. You could do something like this. Say our sample tab delimited file is called file.dat and looks like this...
Code:
   one     ab      fish    04/07/60        yellow
   five    jT      shoe    05/03/03        blue
   two     Ks      knee     / /    yellow
   nine    wM      corn    07/04/76        orange
...running the following command...
Code:
   sed 's/<tab>/<tab>&quot;/3;s/<tab>/&quot;<tab>/4;s= / / ==g' file.dat
...where you actually hit the tab key where I put
Code:
<tab>
. So it would look more like...
Code:
   sed 's/ /       &quot;/3;s/  /&quot;      /4;s= / / ==g' file.dat
This should convert the file to...
Code:
   one     a       fish    &quot;04/07/60&quot;      yellow
   five    j       shoe    &quot;05/03/03&quot;      blue
   two     K       knee    &quot;&quot;      yellow
   nine    w       corn    &quot;07/04/76&quot;      orange
...which I think is what you want.

Hope this helps.

 
Yes, SamBones, this is what I want. Thank you very much
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top