Operating System - HP-UX
1828340 Members
3152 Online
109976 Solutions
New Discussion

Removing field separators in a txt file

 
SOLVED
Go to solution
Chern Jian Leaw
Regular Advisor

Removing field separators in a txt file

HI,

I have a text file as in:

avbg,yuyu,ppp,ii,
jkdsk,opop,ye82,jj,
jkds,llkl ....(and more...)

Is there a utility in UNIX which moves each entries that are separated by the commas to a new line, by discarding the commas, besides using AWK...??

If so, please provide an example.

Thanks.
9 REPLIES 9
John Carr_2
Honored Contributor
Solution

Re: Removing field separators in a txt file

Hi

cat testfile | tr -s "," "\n"

cheers
John.
Trond Haugen
Honored Contributor

Re: Removing field separators in a txt file

As I love vi:
:g/,/s//^M/g
The ^M is CTRL-V CTRL-M

Or with sed:
cat your_file | tr "," "\012"

Regards,
Trond
Regards,
Trond Haugen
LinkedIn
Steve Steel
Honored Contributor

Re: Removing field separators in a txt file

Hi

For Your interest

file=$1
export IFS=,
cat $file|while read line
do
for f in $line
do
echo $f
done
done

Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Robin Wakefield
Honored Contributor

Re: Removing field separators in a txt file

Hi,

Well, you did say besides AWK, so here's a perl method:

cat file|perl -F, -ane 'foreach $f (@F){chomp $f;print "$f\n"};'

Rgds, Robin
Stefan Schulz
Honored Contributor

Re: Removing field separators in a txt file

Hi,

the fastest way is to use tr:

tr ',' '\n' < oldfile > newfile

If you are allready in vi to do some more work on this file:

:1,$s/,/^M/g

Hope this helps

Regards Stefan
No Mouse found. System halted. Press Mousebutton to continue.
Darrell Allen
Honored Contributor

Re: Removing field separators in a txt file

Hi,

Here's the sed method:
sed 's/,//g' file

Note that it is actually entered as 2 lines whether in a script or from a shell prompt (command line).

"tr" is faster. Also note the difference with and without tr's "-s" option. With "-s", repeated characters to be translated (commas in your case) are treated as one occurance. That is, consecutive commas will be replaced with one newline. Without "-s", each comma will be replaced with a newline.

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
H.Merijn Brand (procura
Honored Contributor

Re: Removing field separators in a txt file

Robin, you're almost there :)
use @F in string context and set it's separator to the newline:

perl -naF, -e '$"="\n";print"@F"'

shorter and more obfuscated (set list separator to default input record separator):

perl -naF, -e '$"=$/;print"@F"'
Enjoy, Have FUN! H.Merijn
Darrell Allen
Honored Contributor

Re: Removing field separators in a txt file

Hi again,

I keep forgetting that 2 line sed examples don't get posted correctly. Didn't check my reply until now. It should look more like:
sed 's/,/
/g' file

There should be a "\" character at the end of the first line.

Please see the attached.

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
Mahima Kaushik_2
Frequent Advisor

Re: Removing field separators in a txt file

Hi Chern,
This note is just to remind you to assign points to the forum members who have been responding to your questions (you have at least 3 thread open currently). It would not only be nice for them since they have been helping you but also be helpful for others to know whether your problem has been solved or not.
Thanks!
Mahima
Aim high!