Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

adding field separator 3

Status
Not open for further replies.

omoo

Programmer
May 30, 2005
87
US
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 field between field1 and field2 and 3 spaces between field3 and field4):
________________________________________
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 delimited 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?


 
Hi,

the number of rows to copy data1 is according to the number of rows where there are data.

if there are altogether 5 rows of data(including the data1), the 1st line shd appear for 4 rows.

i cannot seems to get your code to work because of broken pipe.
 
this is how i include ur code:

Code:
cat $file | sed -n '1{h;n};g;p' > $TMP/field.A
 
Hi

omoo wrote :
i cannot seems to get your code to work because of broken pipe.

Strange. Who gives this error message ? Please paste it in. And what happens if you don't use another 1 command, 2 rediretions and 2 variables, just the [tt]sed[/tt], as I wrote ? Are you sure the two variables values contains only alphanumeric characters, nothing to escape or quote ?

I use [tt]bash[/tt] 2.05b and [tt]sed[/tt] 4.1.2. You ?

Code:
#> cat datafile 
data1
data2 data3    data4
data5 data6
data7 data8    data9
      data10
#> cat datafile | sed -n '1{h;n};g;p' > /tmp/field.A
#> cat /tmp/field.A 
data1
data1
data1
data1

Feherke.
 
the 1st line is in this format:

07/12/2005 04:38:11

is there a problem because there are other symbols?
 
Hi

You did not paste in your output. I need that error message. And as possible, the content of output file too. In fact, the values of [tt]$file[/tt] and [tt]$TMP[/tt] could be important too. So please provide a bit more information.

Code:
#> cat datafile 
07/12/2005 04:38:11
data2 data3    data4
data5 data6
data7 data8    data9
      data10
#> cat datafile | sed -n '1{h;n};g;p' > /tmp/field.A
#> cat /tmp/field.A 
07/12/2005 04:38:11
07/12/2005 04:38:11
07/12/2005 04:38:11
07/12/2005 04:38:11

Feherke.
 
Hi,

I did not managed to get any output.

I am unable to paste my datafile in because the format is too big to fit into this window.

this is my error msg:

% cat $file | sed -n '1{h;n};g;p' > /tmp/field.A
sed: command garbled: 1{h;n};g;p
cat: write error: Broken pipe
 
Hi

Ok, now it's much better. So the [tt]cat[/tt] gives that message. The next steps in debuging :

[ol]
[li][tt]echo $file[/tt] - let's see what is in that variable[/li]
[li][tt]ls -l $file[/tt] - what kind of file is the one with that name[/li]
[li][tt]cat $file[/tt] - display the content of that file[/li]
[li][tt]sed -n '1{h;n};g;p' $file[/tt] - use the sed directly, without [tt]cat[/tt][/li]
[/ol]

Send the first two steps output anyway, the last two's only if there is an error.

Feherke.
 
Why not simply this ?
sed '1h;g' $file > $TMP/field.A

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi,

I put the steps 1 and 2 in the shell script and here is the output when I run the script:
Code:
-rw-r-----   1 tdf      hlci       40630 Jul 12 06:04 /home/e037_wip1.rpt

This is the output I get when I run step 4:
Code:
sed: command garbled: 1{h;n};g;p

I've tried the code suggested by PHV and it works! :) But I don't understand why it works though (???)

 
I am not sure if I use Solaris or others as I am a beginner in Unix.

But PHV's code work somehow, may I know how come it can work the same as feherke's code?

Thanks!
 
Hi

There seems to be two problems.

First, you used redirection through pipe, which needs more resources. That Broken pipe message most probably appears because you are not allowed to use such. Leting [tt]sed[/tt] to read the file directly as PHV and I proposed, instead of pipeing from a [tt]cat[/tt] will use less resources.

Second, that command garbled is kind of mistery for me. Most possible is that your [tt]sed[/tt] can not handle blocks of commands grouped with [tt]{ }[/tt]. Maybe it expects the brackets to be escaped, which means written as [tt]'1\{h;n\};g;p'[/tt].

The major difference is that my code prints the value of first line only for the next lines, PHV's code will print it for all lines, including the first too. Hmm... Now I'm not sure which of us misunderstood the requirement.

My code maybe will work without the brackets :

Code:
sed -n '1h;1n;g;p' $file

Feherke.
 
omoo said:
I am not sure if I use Solaris or others as I am a beginner in Unix.
Could you please run the following command and let us know the output?
Code:
uname -a
This could save some confusion.
 
PHV's code work somehow, may I know how come it can work
[tt] [/tt]copy current record to pattern space (default action)
[tt]1h [/tt]if current record is the first then copy pattern space to hold space
[tt]g [/tt]for each record replace pattern space with hold space
[tt] [/tt]print pattern space (default action)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
This is the output I've got:

SunOS athen05 5.8 Generic_117350-20 sun4u sparc SUNW,Sun-Fire-V240
 
You are using Solaris 8 (aka SunOS 5.8) on a Sun Fire V240.

Just in case anyone asks.
 
Hi

Now I found a [tt]sed[/tt] example in which are semicolons after the opening braces too. Maybe this is the way [tt]sed[/tt] for Solaris likes :

Code:
sed -n '1{;h;n};g;p' $file

Feherke.
 
Hi,

I get the same error msg:

Code:
sed: command garbled: 1{;h;n};g;p

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top