how to include field separator if there are blank fields?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to include field separator if there are blank fields?
# 1  
Old 07-12-2005
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 08:45 AM..
# 2  
Old 07-12-2005
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 08:42 AM..
# 3  
Old 07-12-2005
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 Smilie
# 4  
Old 07-12-2005
sed 's/ /;/g' file
tr ' ' ';' < file
# 5  
Old 07-12-2005
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  
Old 07-12-2005
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  
Old 07-12-2005
the problem here is the command will take empty field as part of the space(delimiter) and the output will skip the empty field.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print . in blank fields to prevent fields from shifting

The below code works great, kindly provided by @Don Cragun, the lines in bold print the current output. Since some of the fields printed can be blank some of the fields are shifted. I can not seem too add . to the blank fields like in the desired output. Basically, if there is nothing in the field... (10 Replies)
Discussion started by: cmccabe
10 Replies

2. Shell Programming and Scripting

Inserting a field without disturbing field separator on other fields

Hi All, I have the input as below: cat input 032016002 2.891 97.109 16.605 27.172 24.017 32.207 0.233 0.021 39.810 0.077 0.026 19.644 13.882 0.131 11.646 0.102 11.449 76.265 23.735 16.991 83.009 8.840 91.160 0.020 99.980 52.102 47.898 44.004 55.996 39.963 18.625 0.121 1.126 40.189... (15 Replies)
Discussion started by: am24
15 Replies

3. Shell Programming and Scripting

awk to parse field and include the text of 1 pipe in field 4

I am trying to parse the input in awk to include the |gc= in $4 but am not able to. The below is close: awk so far: awk '{sub(/\|]+]++/, ""); print }' input.txt Input chr1 955543 955763 AGRN-6|pr=2|gc=75 0 + chr1 957571 957852 AGRN-7|pr=3|gc=61.2 0 + chr1 970621 ... (7 Replies)
Discussion started by: cmccabe
7 Replies

4. Shell Programming and Scripting

Field separator

Hello All, I have a file, but I want to separate the file at a particular record with comma"," in the line Input file APPLE6SSAMSUNGS5PRICEPERPIECEDOLLAR600EACH010020340URX581949695US to Output file APPLE6S,SAMSUNGS5,PRICEPERPIECE,DOLLAR600EACH,010020340URX581949695,US This is for... (11 Replies)
Discussion started by: m6248m
11 Replies

5. Shell Programming and Scripting

Regex to include up to blank line.

Hi guys I am trying to figure out how to match a pattern with a regex up to a full blank line. I will show you what I mean with this example: example A movie name: ted movie name: TMNT movie name: Jinxed example B movie names: Gravity Faster Turbo song titles: dont hello problem (8 Replies)
Discussion started by: acoding
8 Replies

6. Shell Programming and Scripting

Need a separator string between fields in cut -c command

Hi All, I'm trying to view data using cut command for a fixed length file using the below command: cut -c 1-3,4-5 FALCON_PIS_00000000.dat I want to mention a separator say | (pipe) in between 1-3 and 4-5. Please let me know how to achieve this. Thanks in Advance, (3 Replies)
Discussion started by: HemaV
3 Replies

7. UNIX for Dummies Questions & Answers

change field separator only from nth field until NF

Hi ! input: 111|222|333|aaa|bbb|ccc 999|888|777|nnn|kkk 444|666|555|eee|ttt|ooo|ppp With awk, I am trying to change the FS "|" to "; " only from the 4th field until the end (the number of fields vary between records). In order to get: 111|222|333|aaa; bbb; ccc 999|888|777|nnn; kkk... (1 Reply)
Discussion started by: beca123456
1 Replies

8. Shell Programming and Scripting

Field separator X'1F'

Hi, I have a flat file with fields separated by a X'1F' i have to fetch 4th field from second line. please help me how to achieve it. I tried with below command and its not working. cut -f4 -d`echo -e '\x1f'` filename.txt I am using SunOS. Thanks in advance. (2 Replies)
Discussion started by: rohan10k
2 Replies

9. Shell Programming and Scripting

awk, comma as field separator and text inside double quotes as a field.

Hi, all I need to get fields in a line that are separated by commas, some of the fields are enclosed with double quotes, and they are supposed to be treated as a single field even if there are commas inside the quotes. sample input: for this line, 5 fields are supposed to be extracted, they... (8 Replies)
Discussion started by: kevintse
8 Replies

10. AIX

printer tray and blank separator issues

Hi, I experience some issues with my new print queues that I created in AIX (smitty). This following print queue is dedicated to print on tray 1 and the user have to put the paper manually, the jobs should be executed without separator page. But when we print on this queue, the job is executed... (1 Reply)
Discussion started by: imad77
1 Replies
Login or Register to Ask a Question