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
need for loop to include fields as one line prkfriryce Shell Programming and Scripting 8 03-23-2007 12:49 PM
fill a NIL into the blank field happyv Shell Programming and Scripting 8 03-23-2007 04:49 AM
field separator as regexp Abhishek Ghose Shell Programming and Scripting 2 02-15-2006 08:49 PM
awk: How to check if field is blank? yongho Shell Programming and Scripting 7 06-09-2005 11:35 AM
How do I specify tab as field separator for sort? SSteve UNIX for Dummies Questions & Answers 8 04-26-2005 04:39 PM

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 07-12-2005
ReV ReV is offline
Registered User
  
 

Join Date: Jun 2005
Posts: 34
how to include field separator if there are blank fields?

Hi,

I have the following data in the format as shown (note: there are more than 1 blank spaces between each field and the spaces are not uniform, meaning there can be one blank space between field1 and field2 and 3 spaces between field3 and field4, in this example, # are the spaces in between the fields):
________________________________________
num1a###num2a#num3a#####num4a##num5a
num1b###num2b##########num4b#######
#######num2c#num3c#####num4c##num5c
#######num2d#num3d#####num4d##num5d
num1e###num2e#num3e#####num4e##num5e
num1f########################num5f
________________________________________

i need to include ; as the field delimiter and the required output is:
________________________________________
num1a;num2a;num3a;num4a;num5a
num1b;num2b;;num4b;
;num2c;num3c;num4c;num5c
;num2d;num3d;num4d;num5d
num1e;num2e;num3e;num4e;num5e
num1f;;;;num5f
________________________________________

I tried using
sed -e 's/ */;/g'
but it will take consecutive blank fields as one blank field. Can anyone help?

Last edited by ReV; 07-12-2005 at 07:45 AM..
  #2 (permalink)  
Old 07-12-2005
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
From what you are saying and given your output

num1f;;;;num5f

Code:
sed -e 's/[ ][ ]*/;/g'
I tried using
sed -e 's/ */;/g'
but it will take consecutive blank fields as one blank field. Can anyone help?


So, shouldnt it take the consecutive blank fields and convert it to ; ?

What's the output you are getting with your sed script ?

Do you mean, for each space encountered, you want to replace it with a ;

Try this

Code:
tr '[:space]' ';' < inputfile


Vino

Last edited by vino; 07-12-2005 at 07:42 AM..
  #3 (permalink)  
Old 07-12-2005
ReV ReV is offline
Registered User
  
 

Join Date: Jun 2005
Posts: 34
Quote:
Originally Posted by vino
From what you are saying and given your output

num1f;;;;num5f

Code:
sed -e 's/[ ][ ]*/;/g'
I tried using
sed -e 's/ */;/g'
but it will take consecutive blank fields as one blank field. Can anyone help?


So, shouldnt it take the consecutive blank fields and convert it to ; ?

What's the output you are getting with your sed script ?

Vino
Hi,

num1f;;;;num5f
is my required output.
But when I use sed -e 's/ */;/g', my output is:
num1f;num5f

I hope I am clear with my description
  #4 (permalink)  
Old 07-12-2005
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
sed 's/ /;/g' file
tr ' ' ';' < file
  #5 (permalink)  
Old 07-12-2005
ReV ReV is offline
Registered User
  
 

Join Date: Jun 2005
Posts: 34
Hi,

No, I only need one ; in between each field. But however, there can be one space or 2 spaces in between 2 fields.

input:
field1[space][space][space]field2[space][space]field3[space]field4
field1[space][space][space]emptyfield[space][space]emptyfield[space]field4

required output:
field1;field2;field3;field4
field1;;;field4

but from the script that I have tried, I get:
field1;field2;field3;field4
field1;field4
  #6 (permalink)  
Old 07-12-2005
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Quote:
Originally Posted by ReV
Hi,

No, I only need one ; in between each field. But however, there can be one space or 2 spaces in between 2 fields.

input:
field1[space][space][space]field2[space][space]field3[space]field4
field1[space][space][space]emptyfield[space][space]emptyfield[space]field4

required output:
field1;field2;field3;field4
field1;;;field4

but from the script that I have tried, I get:
field1;field2;field3;field4
field1;field4

what's the difference between '[space]' and 'emptyfield'?
  #7 (permalink)  
Old 07-12-2005
ReV ReV is offline
Registered User
  
 

Join Date: Jun 2005
Posts: 34
the problem here is the command will take empty field as part of the space(delimiter) and the output will skip the empty field.
Sponsored Links
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 04:47 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