Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...What a great service! This is the best site I've ever seen!!! It totally restores my faith in humanity when people take time out to help other people..."

Geography

Where in the world do Tek-Tips members come from?

replacing tab with comma and enclosing double quotes

unixwhoopie (Programmer)
4 Jun 08 15:23
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.
PHV (MIS)
4 Jun 08 15:26
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: How Do I Get Great Answers To my Tek-Tips Questions?
FAQ181-2886: How can I maximize my chances of getting an answer?

unixwhoopie (Programmer)
4 Jun 08 21:51
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

 
feherke (Programmer)
5 Jun 08 2:12
Hi

So you want to transform this :

1       2       3
4       5       6
7       8       9


into this :

"1",2,3
"4",5,6
"7",8,9


CODE

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

# or

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

# or

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

Feherke.
http://rootshell.be/~feherke/

unixwhoopie (Programmer)
5 Jun 08 15:33
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.
p5wizard (IS/IT--Management)
5 Jun 08 16:00
try vi? winky smile

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

feherke (Programmer)
6 Jun 08 3:55
Hi

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

Feherke.
http://rootshell.be/~feherke/

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close