LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-21-2009, 06:10 AM   #1
keenboy
Member
 
Registered: Jan 2008
Location: Cullompton
Distribution: Kubuntu
Posts: 36

Rep: Reputation: 15
awk question


Hello,

I have a tab separated text file which I'd like to separate by commas instead.

I use the following command:

Code:
awk -v OFS=':' '$1=$1' file
The issue I have is the spaces also get separated as well as the tabs which mean that column data with spaces in them are split into different columns.

How do I just replace the TABs but not the spaces?

Thanks,
 
Old 10-21-2009, 06:23 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
Make tab the input delimiter:

-F\\t

or

-v FS='\t'

simpler and more readable to not use awk though;

sed -e 's/\t/:/g' file

Last edited by acid_kewpie; 10-21-2009 at 06:25 AM.
 
Old 10-21-2009, 06:25 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
sed would be a good tool for that
 
Old 10-21-2009, 06:49 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
awk
Code:
awk '{gsub("\t",":")}1' file
 
Old 10-21-2009, 07:15 AM   #5
keenboy
Member
 
Registered: Jan 2008
Location: Cullompton
Distribution: Kubuntu
Posts: 36

Original Poster
Rep: Reputation: 15
Thanks, ghostdog74

Unfortunately this doesn't seem to work for me.

Quote:
awk '{gsub("\t",":")}1' file
The result is the file still tab separated not comma separated.

Is there anything else I can try?

Thanks,
 
Old 10-21-2009, 07:25 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

These all work for me:

cat infile | tr "\t" ":"

sed 's/\t/:/g' infile --> acid_kewpie's solution from post #2

awk '{gsub("\t",":")}1' infile --> ghostdog74's solution from port #4
 
Old 10-21-2009, 07:40 AM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
@OP if you want more "precise" answers, provide more information about your data files and the output you want. Help us to help yourself.
 
Old 10-21-2009, 10:03 AM   #8
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
why do some people only ever read the last reply??? What was wrong with the two solutions i gave??
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
awk question on handling *.CSV "text fields" in awk jschiwal Programming 8 05-27-2010 06:23 AM
awk question groys Linux - Newbie 3 05-25-2009 04:29 AM
[SOLVED] awk question vikas027 Programming 2 06-05-2008 03:18 AM
Awk question blanny Linux - Software 6 05-02-2006 08:03 AM
awk question jonlake Linux - Software 2 05-04-2005 03:58 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration