If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Reformatting a file

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-13-10, 14:24
urs_77 urs_77 is offline
Member
 
Join Date: Jan 2003
Location: Schaumburg, IL
Posts: 79
Reformatting a file

Hello,

I have a file like shown below:

456
1
2
756
349

I want it to be reformatted like this:

456, 1, 2, 756, 349

How can I do this in a UNIX shell script? Is it possible in awk? If not, how can I do it in korn shell?
__________________
Naveen Urs
DBA Manager
IBM Certified Solutions Expert - DB2 LUW V7, V9
Reply With Quote
  #2 (permalink)  
Old 04-14-10, 04:26
pdreyer pdreyer is offline
Registered User
 
Join Date: May 2005
Location: South Africa
Posts: 1,268
Code:
sed '2,$s/^/, /' myfile | tr -d '\n'
Reply With Quote
  #3 (permalink)  
Old 04-14-10, 04:33
pdreyer pdreyer is offline
Registered User
 
Join Date: May 2005
Location: South Africa
Posts: 1,268
Code:
awk '{if (NR==1) {printf $1} else {printf ", " $1}}' myfile
Reply With Quote
  #4 (permalink)  
Old 04-14-10, 04:36
pdreyer pdreyer is offline
Registered User
 
Join Date: May 2005
Location: South Africa
Posts: 1,268
Code:
awk '{printf x $1; x=", "}' myfile
Reply With Quote
  #5 (permalink)  
Old 04-14-10, 08:41
kitaman kitaman is offline
Papabi's friend
 
Join Date: Sep 2009
Location: Ontario
Posts: 629
Korn shell.

Code:
#!/bin/ksh
separator=""
while read a
do
echo -e "$separator,$a\c"
separator=", "
done<myfile
echo -e "\n"
Depending on your version of echo, you may or may not need the "-e" option.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On