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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

replacing tab with comma and enclosing double quotes

Status
Not open for further replies.

unixwhoopie

Programmer
May 6, 2003
45
0
0
US
Hello, I am trying to the following. can you let me know how?

1. I have a tab delimited file. I need to enclose the first field in double quotes.

2. I need to replace the tab with comma.

Thanks for the help.
 
What have you tried so far and where in your code are you stuck ?
Tips:
man sed
man awk

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
1,$s/\t/,/g will replace tab with ,

however issue is with placing quotes for the first field

I am trying to place " at the end of the first field for all lines but this is not working:

1,$s/,/",/1

 
Hi

So you want to transform this :

[tt]1 2 3
4 5 6
7 8 9[/tt]

into this :

[tt]"1",2,3
"4",5,6
"7",8,9[/tt]

Code:
sed 's/[^\t]*/"&"/;s/\t/,/g' /input/file > /output/file

[gray]# or[/gray]

sed 's/^/"/;s/\t/",/;s/\t/,/g' /input/file > /output/file

[gray]# or[/gray]

awk -F'\t' -vOFS=, '{$1="\""$1"\""}1' /input/file > /output/file
Tested with GNU [tt]sed[/tt] and [tt]gawk[/tt].

Feherke.
 
That worked. Thanks very much.

I have one more question. How can I format date field in a file. I have the 4th field as date and I want all lines to be unique format: dd/mm/yyyy hh:mi:ss

How can I do this? Thanks again.
 
try vi? ;-)

If you know which types of date formats occur and are able to distinguish between them, then it can be done with awk functions substr() and/or sub(), gsub() or gensub() if/when available.


HTH,

p5wizard
 
Hi

Or if you have [tt]gawk[/tt], you may use its [tt]mktime()[/tt] and [tt]strftime()[/tt] functions. But we can not tell you how, unless you show us some sample input.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top