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!
7 10132
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 -
prev=""
-
for i in `cat test.txt`
-
do
-
counter=0
-
line_length=`echo $i | wc -L`
-
for (( j=1; j <= $line_length; j++ ))
-
do
-
char=`echo $i | cut -c $j`
-
if [ $char = ',' ]
-
then
-
counter=`expr $counter + 1`
-
val=`expr $counter % 3`
-
if [ $val -eq 0 ]
-
then
-
echo -e $prev >> res.txt
-
prev=""
-
else
-
prev=$prev$char
-
fi
-
else
-
prev=$prev$char
-
fi
-
done
-
echo -e $prev >> res.txt
-
done
-
-
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"
- awk 'ORS=NR%3?RS:"\n"' RS="," filename
Use nawk on Solaris.
- awk 'ORS=NR%3?RS:"\n"' RS="," filename
Use nawk on Solaris.
Thanks a lot for your replies!
It worked!!!
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!!!
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 - echo "
-
Select col1,',',col2,'
-
',col3,',',col4
-
from my.table " | db2 -x
to avoid having to transform it with shell methods. Which db are you using?
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: - $ db2 -x "select 'echo -e ', current_date,'\\\\n',current_time from sysibm.sysdummy1 " | bash
-
2008-03-07
-
14.17.58
-
This could work with other db systems, too. As far as you own a swiss army knife, or know somebody like MacGyver.
Regards,
Bernd
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: - $ db2 -x "select 'echo -e ', current_date,'\\\\n',current_time from sysibm.sysdummy1 " | bash
-
2008-03-07
-
14.17.58
-
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
|
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...
|
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: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |