LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   awk question (https://www.linuxquestions.org/questions/linux-general-1/awk-question-763414/)

keenboy 10-21-2009 06:10 AM

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,

acid_kewpie 10-21-2009 06:23 AM

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

catkin 10-21-2009 06:25 AM

sed would be a good tool for that

ghostdog74 10-21-2009 06:49 AM

awk
Code:

awk '{gsub("\t",":")}1' file

keenboy 10-21-2009 07:15 AM

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,

druuna 10-21-2009 07:25 AM

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

ghostdog74 10-21-2009 07:40 AM

@OP if you want more "precise" answers, provide more information about your data files and the output you want. Help us to help yourself.

acid_kewpie 10-21-2009 10:03 AM

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


All times are GMT -5. The time now is 07:53 PM.