The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
converting date format mohan705 Shell Programming and Scripting 7 05-22-2008 11:10 AM
converting config file to csv format zer0dvide Shell Programming and Scripting 7 03-21-2008 10:33 AM
converting PDF to text, rtf doc format saurya_s UNIX for Advanced & Expert Users 1 04-23-2004 03:25 PM
Error when converting to Postscript format zing UNIX for Dummies Questions & Answers 1 06-19-2003 05:07 PM
Converting BMP to BM (or other unix format) EJ =) UNIX Desktop for Dummies Questions & Answers 1 06-12-2002 08:42 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-18-2006
gthokala gthokala is offline
Registered User
  
 

Join Date: Jun 2005
Posts: 21
converting text to csv format

I am trying to check each line and based on first two digits, the comma needs to be place. I checked in the earlier post where the text is converted to csv with a tab delimited.
Here is the test file that needs to be changed to csv

11 051701
22 051701
330123405170105170112345
0100001123456789012987654321
0100002123456789012987654321
0100003123456789012987654321
0100004123456789012987654321
0100017123456789012987654321
.......


if the first 2 digits are 11, then a comma is placed after 2nd column and just before the 051701. If the first two digits are 01 then the commas should be place after 01, another comma after 00001, another after 123456789012. In short the output should look like this

11, ,051701
22, ,051701
33,01234,051701,051701,12345
01,00001,123456789012,987654321
01,00002,123456789012,987654321
01,00003,123456789012,987654321
01,00004,123456789012,987654321
01,00017,123456789012,987654321
.......


Any help is appreciated. Thanks
~
  #2 (permalink)  
Old 05-18-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
here's something to start with:

nawk -f gt.awk myFile.txt

gt.awk:
Code:
/^11/ && NF == 2 { $1=$1 "," ;$2="," $2; print; next }
/^01/ {
   $0 = substr($0,1,2) "," substr($0,3,5) "," substr($0,8,12) "," substr($0, 13)
   print
}
  #3 (permalink)  
Old 05-19-2006
gthokala gthokala is offline
Registered User
  
 

Join Date: Jun 2005
Posts: 21
Thumbs up

Thanks a lot. I was able to modify accordingly with my text file and was able to get the desired csv format file. Appreciate and thank you for your quick response.
  #4 (permalink)  
Old 06-07-2006
gthokala gthokala is offline
Registered User
  
 

Join Date: Jun 2005
Posts: 21
converting text to csv format

I was able to get the csv file without any problem. I have one quick question. Is it possible to take the 2nd column on line 3, '01234' and have it on each and every line where you see '01' as the first two characters, like for example

11, ,051701
22, ,051701
33,01234,051701,051701,12345
01,00001,123456789012,987654321
01,00002,123456789012,987654321
01,00003,123456789012,987654321
01,00004,123456789012,987654321
01,00017,123456789012,987654321
.......


to

11, ,051701
22, ,051701
33,01234,051701,051701,12345
01234,01,00001,123456789012,987654321
01234,01,00002,123456789012,987654321
01234,01,00003,123456789012,987654321
01234,01,00004,123456789012,987654321
01234,01,00017,123456789012,987654321
.......
  #5 (permalink)  
Old 06-07-2006
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,944
this one,

Code:
sed -n /^01/p filename | sed "s/^01/`awk -F"," '{ if(NR==3) { print $2 } }' filename`/g"
  #6 (permalink)  
Old 06-07-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
[not tested]
nawk -f gt.awk filename

gt.awk:
Code:
BEGIN {
   FS=OFS=","
}
FNR==3 { num=$2 }
FNR != 3 && $1 == "01" { $1 = num OFS $1 }
1

Last edited by vgersh99; 06-07-2006 at 10:49 AM..
  #7 (permalink)  
Old 06-07-2006
gthokala gthokala is offline
Registered User
  
 

Join Date: Jun 2005
Posts: 21
Thanks for your reply. Actually the suggestion you gave is replacing the value from '01' to '01234' and eliminating the other rows. Is there any way to keep the other rows as is and concatenate the '01234' from '33' line to '01' lines. This should be included in the code that was suggested earlier by vgersh99 in gt.awk
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:52 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0