473,386 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

replacing comma with newline

sumittyagi
202 Expert 100+
Hi all,
In ksh script, I have a comma separated list. I want to replace every third comma with newline. Please assist me in this.

Thanks a lot!
Mar 4 '08 #1
7 10130
ashitpro
542 Expert 512MB
Hi all,
In ksh script, I have a comma separated list. I want to replace every third comma with newline. Please assist me in this.

Thanks a lot!

I'm assuming that comma separated list is in another file. I've taken that file name as test.txt.
file should look like
a,b,c,d,e,f,g,h,i,j

result file will be res.txt and result should look like
a,b,c
d,e,f
g,h,i
j

Expand|Select|Wrap|Line Numbers
  1. prev=""
  2. for i in `cat test.txt`
  3. do
  4.     counter=0
  5.     line_length=`echo $i | wc -L`
  6.     for (( j=1; j <= $line_length; j++ ))
  7.     do
  8.         char=`echo $i | cut -c $j`
  9.         if [ $char = ',' ]
  10.         then
  11.             counter=`expr $counter + 1`
  12.             val=`expr $counter % 3`
  13.             if [ $val -eq 0 ]
  14.             then
  15.                 echo -e $prev >> res.txt
  16.                 prev=""
  17.             else
  18.                 prev=$prev$char
  19.             fi
  20.         else
  21.             prev=$prev$char
  22.         fi
  23.     done
  24.     echo -e $prev >> res.txt
  25. done
  26.  
  27.  
I've tested this script using "bash"
no copy rights, you can modify code as per your choice so that it can work with "ksh"
Mar 4 '08 #2
Expand|Select|Wrap|Line Numbers
  1. awk 'ORS=NR%3?RS:"\n"' RS="," filename
Use nawk on Solaris.
Mar 4 '08 #3
sumittyagi
202 Expert 100+
Expand|Select|Wrap|Line Numbers
  1. awk 'ORS=NR%3?RS:"\n"' RS="," filename
Use nawk on Solaris.
Thanks a lot for your replies!
It worked!!!
Mar 5 '08 #4
sumittyagi
202 Expert 100+
Thanks a lot for your replies!
It worked!!!
I actually wanted to get a csv file for a result from a sql query.

The file is getting created, but I suppose unix don't treat \n as newline. It is replacing \n by a space in the file.

I also tried \r\n like this:

nawk 'ORS=NR%3?RS:"\r\n"' RS="," filename

In this case, newline is done but the file gets truncated abruptly in between when I use cat, pg or vi to see the content. But when I transferred the file to windows, the file looks well in excel, and shows all records.

Is there any way I can see the content at unix side as well.

Thanks a lot!!!
Mar 5 '08 #5
docdiesel
297 Expert 100+
Hi,

try to use 'more' or 'less' (depends on what's installed on your system; am not familiar with Solaris), or use the 'tail' command to check at least the last lines to see if everything is there.

As far as I know all Unix systems are using \n as newline character, while just the software originated in Redmond relies on \r\n. Is there a 'hexdump' command on your *nix? Try to check if you see the 0x0A hex values of the \n in the right places.

Regards,

Bernd


P.S.: With DB2 I'd try someting like
Expand|Select|Wrap|Line Numbers
  1. echo "
  2. Select col1,',',col2,'
  3. ',col3,',',col4
  4. from my.table " | db2 -x
to avoid having to transform it with shell methods. Which db are you using?
Mar 6 '08 #6
docdiesel
297 Expert 100+
Hi,

just did some tests and with DB2 and my swiss army knife (bash shell) the following works for breaking up one row into several lines:

Expand|Select|Wrap|Line Numbers
  1. $ db2 -x "select 'echo -e ', current_date,'\\\\n',current_time  from sysibm.sysdummy1 " | bash
  2. 2008-03-07
  3.  14.17.58
  4.  
This could work with other db systems, too. As far as you own a swiss army knife, or know somebody like MacGyver.

Regards,

Bernd
Mar 7 '08 #7
sumittyagi
202 Expert 100+
Hi,

just did some tests and with DB2 and my swiss army knife (bash shell) the following works for breaking up one row into several lines:

Expand|Select|Wrap|Line Numbers
  1. $ db2 -x "select 'echo -e ', current_date,'\\\\n',current_time  from sysibm.sysdummy1 " | bash
  2. 2008-03-07
  3.  14.17.58
  4.  
This could work with other db systems, too. As far as you own a swiss army knife, or know somebody like MacGyver.

Regards,

Bernd
Thanks a lot Bernd,
I will try it out!!!

Regards,
Sumit Tyagi
Mar 13 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: jamesthiele.usenet | last post by:
I recently ran into the issue with 'print' were, as it says on the web page called "Python Gotchas" (http://www.ferg.org/projects/python_gotchas.html): The Python Language Reference Manual says,...
3
by: James D. Marshall | last post by:
The issue at hand, I believe is my comprehension of using regular expression, specially to assist in replacing the expression with other text. using regular expression (\s*) my understanding is...
4
by: JustSomeGuy | last post by:
Hi. I have a comma delimited text file that I want to parse. I was going to use fscanf from the C library but as my app is written in C++ I thought I'd use the std io stream library... My Text...
32
by: FireHead | last post by:
Hello C World & Fanatics I am trying replace fgets and provide a equavivalant function of BufferedInputReader::readLine. I am calling this readLine function as get_Stream. In the line 4 where...
8
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
I have a RichTextBox (rtfTerminal below) in which I would like to replace the last line with the last line minus its last character (i.e., do a backspace). I tried the following code, wherein I...
22
by: aarklon | last post by:
Hi all, why does C language permits an extra comma in initializer list ex:- int days = { 31,28.31,30,31,30, 31,31,30,31,30,31, } i have heard it is for the purpose of automatic code...
1
by: Matt Herzog | last post by:
Hey All. I'm learning some python with the seemingly simple task of updating a firewall config file with the new IP address when my dhcpd server hands one out. Yeah, I know it's dangerous to edit...
6
by: Andrew | last post by:
I am using an XmlSerializer to save some settings and I have discovered that when a string is saved containing a cr+nl it is replaced with just a newline when loading back in. I am no expert with...
8
by: E11esar | last post by:
Hi there. I am looping through a csv file and for each line, I need to replace a comma in a string that occurs between quotes. For example: "The", "cat", "sat", "on, the", "mat" Here...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.