?
Solved

awk help needed

Posted on 2003-02-19
8
Medium Priority
?
858 Views
Last Modified: 2013-12-26
i need a awk script that searches for commas in every row of the text file and replaces the comma with colon and the sets the field delimiter as comma.

cheers
shyam
 
0
Comment
Question by:shyam78
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 5

Expert Comment

by:Nisus091197
ID: 7985726
Hi,

sed will do it for you very easily:

sed -e 's/,/:/g' input.txt > output.txt

where input.txt is the file with all the commas and output.txt is the output file.

Regards, Nisus
http://www.omnimodo.com
0
 
LVL 1

Expert Comment

by:brains
ID: 7986728
to set the field delimiter to comma in awk add BEGIN { FS=","} as the first awk command, for example...

echo "test this, 1, 2, 3" | awk ' BEGIN { FS=","} {print $2}'

0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7987071
awk -F, 'BEGIN{OFS=":";ORS=","}{gsub(",",":");print}' file
# works with gawk only (probably mawk too)
0
Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

 

Author Comment

by:shyam78
ID: 7990011
thanks guys.
the query helped to get rid of comma inside every column values.
(ie commas where inside the column values...

but the main thing that i want is to get comma as the delimiter for every column in the row.
at present the delimiter is tab.


0
 
LVL 5

Accepted Solution

by:
Nisus091197 earned 200 total points
ID: 7997533
Hi,

You change the delimiter of awk by using the -F paramater.

example:

text file, mmm, containing "1,2,3,4,5,6"

$ awk -F, '{print $3}' mmm
3
$

Nisus
0
 
LVL 9

Expert Comment

by:HamdyHassan
ID: 8017858
sed -e 's/,/:/g' input.txt > tmp.txt
sed -e 's/ /,/g' tmp.txt > output.txt

             
note that, you need to press <TAB> at the second sed command  s/<TAB>/,/g
0
 
LVL 18

Expert Comment

by:liddler
ID: 9826957
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by Nisus

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

liddler
EE Cleanup Volunteer
0

Featured Post

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month13 days, 17 hours left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question