The UNIX and Linux Forums  

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

Reply
 
LinkBack Thread Tools 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 04:45 AM.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 07-12-2005
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,639
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 04:42 AM.
Reply With Quote
  #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
Reply With Quote
  #4 (permalink)  
Old 07-12-2005
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 2,999
sed 's/ /;/g' file
tr ' ' ';' < file
Reply With Quote
  #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
Reply With Quote
  #6 (permalink)  
Old 07-12-2005
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 2,999
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'?
Reply With Quote
  #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.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 09:03 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0