Splitting and joining {61 0} {141 0} {70 0} {62 0}.

This is a discussion on Splitting and joining {61 0} {141 0} {70 0} {62 0}. within the shell forums in Operating Systems category; Hi, all: (1) I have a file curly_brace_pairs.txt like shown below: {61 0} {141 0} {70 0} {62 0} {142 0} {71 0} {63 0} {63 63} {143 0} I wish to split it into lines, while each line has only one pair of curly brace, such like: {61 0} {141 0} {70 0} {62 0} {142 0} {71 0} {63 0} {63 63} {143 0} I tried several ways to do it, but never succeeded: # sed s/\} \{/\n/ curly_brace_pairs.txt sed: command garbled: s/\} \{/\n/ # sed s/} {/\n/ curly_brace_pairs.txt {61 0n141 0} {70 0} {62 0} {142 ...

Go Back   Database Forum > Operating Systems > shell

Database Forums

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-25-2008, 10:50 AM
Default Splitting and joining {61 0} {141 0} {70 0} {62 0}.

Hi, all:

(1) I have a file curly_brace_pairs.txt like shown below:

{61 0} {141 0} {70 0} {62 0} {142 0} {71 0} {63 0} {63 63} {143 0}

I wish to split it into lines, while each line has only one pair of
curly brace, such like:
{61 0}
{141 0}
{70 0}
{62 0}
{142 0}
{71 0}
{63 0}
{63 63}
{143 0}

I tried several ways to do it, but never succeeded:

# sed "s/\} \{/\n/" curly_brace_pairs.txt
sed: command garbled: s/\} \{/\n/

# sed "s/} {/\n/" curly_brace_pairs.txt
{61 0n141 0} {70 0} {62 0} {142 0} {71 0} {63 0} {63 63} {143 0}

# sed "s/} {/$/" curly_brace_pairs.txt
Illegal variable name.

# sed "s/} {/\$/" curly_brace_pairs.txt
Variable name must contain alphanumeric characters.

# sed "s/} {/\\n/" curly_brace_pairs.txt
{61 0\n141 0} {70 0} {62 0} {142 0} {71 0} {63 0} {63 63} {143 0}

# sed "s/} {/\\\n/" curly_brace_pairs.txt
{61 0\n141 0} {70 0} {62 0} {142 0} {71 0} {63 0} {63 63} {143 0}

What's the correct way to do it?

(2) I have a file curly_brace_lines.txt like shown below:
{61 0}
{141 0}
{70 0}
{62 0}
{142 0}
{71 0}
{63 0}
{63 63}
{143 0}

I wish to join the file into one line just as the previous file
curly_brace_pairs.txt .

I tried several ways to do it, but never succeeded:

sed 's/$/ /' curly_brace_lines.txt
sed 's/^$/ /' curly_brace_lines.txt
sed 's/\$/ /' curly_brace_lines.txt
sed 's/\n/ /' curly_brace_lines.txt

With any of the 4 commands, the result is always no change comparing
with input file curly_brace_lines.txt .

What's the correct way to do it?

Thanks.
Reply With Quote
  #2  
Old 08-25-2008, 11:00 AM
Default Re: Splitting and joining {61 0} {141 0} {70 0} {62 0}.

On Monday 25 August 2008 15:50, Kuhl wrote:

> Hi, all:
>
> (1) I have a file curly_brace_pairs.txt like shown below:
>
> {61 0} {141 0} {70 0} {62 0} {142 0} {71 0} {63 0} {63 63} {143 0}
>
> I wish to split it into lines, while each line has only one pair of
> curly brace, such like:
> {61 0}
> {141 0}
> {70 0}
> {62 0}
> {142 0}
> {71 0}
> {63 0}
> {63 63}
> {143 0}


Try with:

sed 's/} {/}\n{/g' curly_brace_pairs.txt

if your sed doesn't support \n in the rhs, then do

sed 's/} {/}\
{/g' curly_brace_pairs.txt

> (2) I have a file curly_brace_lines.txt like shown below:
> {61 0}
> {141 0}
> {70 0}
> {62 0}
> {142 0}
> {71 0}
> {63 0}
> {63 63}
> {143 0}
>
> I wish to join the file into one line just as the previous file
> curly_brace_pairs.txt .


sed ':a;$!{N;ba;};$s/\n/ /g' curly_brace_lines.txt

--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.
Reply With Quote
  #3  
Old 08-25-2008, 11:13 AM
Default Re: Splitting and joining {61 0} {141 0} {70 0} {62 0}.

On 8/25/2008 8:50 AM, Kuhl wrote:
> Hi, all:
>
> (1) I have a file curly_brace_pairs.txt like shown below:
>
> {61 0} {141 0} {70 0} {62 0} {142 0} {71 0} {63 0} {63 63} {143 0}
>
> I wish to split it into lines, while each line has only one pair of
> curly brace, such like:
> {61 0}
> {141 0}
> {70 0}
> {62 0}
> {142 0}
> {71 0}
> {63 0}
> {63 63}
> {143 0}


sed 's/} {/}\
{/g' file

> (2) I have a file curly_brace_lines.txt like shown below:
> {61 0}
> {141 0}
> {70 0}
> {62 0}
> {142 0}
> {71 0}
> {63 0}
> {63 63}
> {143 0}
>
> I wish to join the file into one line just as the previous file
> curly_brace_pairs.txt .


awk -v RS= '$1=$1' file

Regards,

Ed.

Reply With Quote
  #4  
Old 08-25-2008, 11:16 AM
Default Re: Splitting and joining {61 0} {141 0} {70 0} {62 0}.



On 8/25/2008 9:06 AM, Bill Marcum wrote:
> On 2008-08-25, Kuhl wrote:
>
>>
>>Hi, all:
>>
>>(1) I have a file curly_brace_pairs.txt like shown below:
>>
>>{61 0} {141 0} {70 0} {62 0} {142 0} {71 0} {63 0} {63 63} {143 0}
>>
>>I wish to split it into lines, while each line has only one pair of
>>curly brace, such like:
>>{61 0}
>>{141 0}
>>{70 0}
>>{62 0}
>>{142 0}
>>{71 0}
>>{63 0}
>>{63 63}
>>{143 0}
>>
>>I tried several ways to do it, but never succeeded:
>>
>># sed "s/\} \{/\n/" curly_brace_pairs.txt
>>sed: command garbled: s/\} \{/\n/
>>
>># sed "s/} {/\n/" curly_brace_pairs.txt
>>{61 0n141 0} {70 0} {62 0} {142 0} {71 0} {63 0} {63 63} {143 0}
>>

>
> sed "s/} {/
> /g" curly_brace_pairs.txt


ITYM:

sed "s/} {/}\
}/g" curly_brace_pairs.txt


>>(2) I have a file curly_brace_lines.txt like shown below:
>>{61 0}
>>{141 0}
>>{70 0}
>>{62 0}
>>{142 0}
>>{71 0}
>>{63 0}
>>{63 63}
>>{143 0}
>>
>>I wish to join the file into one line just as the previous file
>>curly_brace_pairs.txt .
>>
>>I tried several ways to do it, but never succeeded:
>>

>
> tr "\n" " " < curly_brace_lines.txt
>


That would create output with a trailing space and no ending newline so it
wouldn't be the same as the original curly_brace_pairs.txt file.

Ed.


Reply With Quote
  #5  
Old 08-25-2008, 11:17 AM
Default Re: Splitting and joining {61 0} {141 0} {70 0} {62 0}.



On 8/25/2008 9:16 AM, Ed Morton wrote:
>
> On 8/25/2008 9:06 AM, Bill Marcum wrote:


>>sed "s/} {/
>>/g" curly_brace_pairs.txt

>
>
> ITYM:
>
> sed "s/} {/}\
> }/g" curly_brace_pairs.txt


oops:

sed "s/} {/}\
{/g" curly_brace_pairs.txt

Ed.

Reply With Quote
  #6  
Old 08-25-2008, 01:07 PM
Default Re: Splitting and joining {61 0} {141 0} {70 0} {62 0}.

Kuhl wrote:
>
> (1) I have a file curly_brace_pairs.txt like shown below:
>
> {61 0} {141 0} {70 0} {62 0} {142 0} {71 0} {63 0} {63 63} {143 0}
>
> I wish to split it into lines, while each line has only one pair of
> curly brace, such like:
> {61 0}
> {141 0}
> {70 0}
> {62 0}
> {142 0}
> {71 0}
> {63 0}
> {63 63}
> {143 0}


$ echo "{61 0} {141 0} {70 0} {62 0} {142 0} {71 0} {63 0} {63 63} {143
0}" |\
perl -040pe'$\=/}/?"\n":""'
{61 0}
{141 0}
{70 0}
{62 0}
{142 0}
{71 0}
{63 0}
{63 63}
{143 0}




John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
Reply With Quote
  #7  
Old 08-26-2008, 12:27 AM
Default Re: Splitting and joining {61 0} {141 0} {70 0} {62 0}.

On Aug 25, 9:50 pm, Kuhl wrote:
> Hi, all:
>
> (1) I have a file curly_brace_pairs.txt like shown below:
>
> {61 0} {141 0} {70 0} {62 0} {142 0} {71 0} {63 0} {63 63} {143 0}
>
> I wish to split it into lines, while each line has only one pair of
> curly brace, such like:
> {61 0}
> {141 0}
> {70 0}
> {62 0}
> {142 0}
> {71 0}
> {63 0}
> {63 63}
> {143 0}
>
> I tried several ways to do it, but never succeeded:
>
> # sed "s/\} \{/\n/" curly_brace_pairs.txt
> sed: command garbled: s/\} \{/\n/
>
> # sed "s/} {/\n/" curly_brace_pairs.txt
> {61 0n141 0} {70 0} {62 0} {142 0} {71 0} {63 0} {63 63} {143 0}
>
> # sed "s/} {/$/" curly_brace_pairs.txt
> Illegal variable name.
>
> # sed "s/} {/\$/" curly_brace_pairs.txt
> Variable name must contain alphanumeric characters.
>
> # sed "s/} {/\\n/" curly_brace_pairs.txt
> {61 0\n141 0} {70 0} {62 0} {142 0} {71 0} {63 0} {63 63} {143 0}
>
> # sed "s/} {/\\\n/" curly_brace_pairs.txt
> {61 0\n141 0} {70 0} {62 0} {142 0} {71 0} {63 0} {63 63} {143 0}
>
> What's the correct way to do it?
>
> (2) I have a file curly_brace_lines.txt like shown below:
> {61 0}
> {141 0}
> {70 0}
> {62 0}
> {142 0}
> {71 0}
> {63 0}
> {63 63}
> {143 0}
>
> I wish to join the file into one line just as the previous file
> curly_brace_pairs.txt .
>
> I tried several ways to do it, but never succeeded:
>
> sed 's/$/ /' curly_brace_lines.txt
> sed 's/^$/ /' curly_brace_lines.txt
> sed 's/\$/ /' curly_brace_lines.txt
> sed 's/\n/ /' curly_brace_lines.txt
>
> With any of the 4 commands, the result is always no change comparing
> with input file curly_brace_lines.txt .
>
> What's the correct way to do it?
>
> Thanks.


awk 'BEGIN{ RS="{"}NR>1{ print "{"$0}' file
Reply With Quote
  #8  
Old 08-26-2008, 09:07 AM
Default Re: Splitting and joining {61 0} {141 0} {70 0} {62 0}.

Hi, all:

Thank you everyone. I have tried first 3 answers. I did not have a
chance to try further answers yet. I will try them tomorrow. Let's
have an update about what I already got.

(1) Splitting has no solution yet.
(2) Joining has one general solution using tr, and one solution
depending on the system using awk -v.

Details see below. Is there any other idea about (1) splitting?

Thanks.


(1) Splitting

On my Solaris,

# sed 's/} {/}\
{/g' curly_brace_pairs.txt
sed: command garbled: s/} {/}

# sed 's/\} \{/\}\
\{/g' curly_brace_pairs.txt
sed: command garbled: s/\} \{/\}

# sed "s/\} \{/\}\
\{/g" curly_brace_pairs.txt
sed: command garbled: s/\} \{/\}

# sed "s/} {/}\
{/g" curly_brace_pairs.txt
sed: command garbled: s/} {/}

On my Linux,

# sed 's/} {/}\
{/g' curly_brace_pairs.txt
sed: -e expression #1, char 7: Unterminated `s' command

# sed 's/\} \{/\}\
\{/g' curly_brace_pairs.txt
sed: -e expression #1, char 10: Unterminated `s' command

# sed "s/\} \{/\}\
\{/g" curly_brace_pairs.txt
sed: -e expression #1, char 10: Unterminated `s' command

# sed "s/} {/}\
{/g" curly_brace_pairs.txt
sed: -e expression #1, char 7: Unterminated `s' command

(2) Joining

On my Solaris,

# sed ':a;$!{N;ba;};$s/\n/ /g' curly_brace_lines.txt
N: Event not found.

# tr "\n" " " < curly_brace_lines.txt
{61 0} {141 0} {70 0} {62 0} {142 0} {71 0} {63 0} {63 63} {143 0}

# awk -v RS= '$1=$1' curly_brace_lines.txt
awk: syntax error near line 1
awk: bailing out near line 1

# nawk -v RS= '$1=$1' curly_brace_lines.txt
{61 0} {141 0} {70 0} {62 0} {142 0} {71 0} {63 0} {63 63} {143 0}

(There's no gawk command in my Solaris system.)

On my Linux,

# sed ':a;$!{N;ba;};$s/\n/ /g' curly_brace_lines.txt
N: Event not found.

# tr "\n" " " < curly_brace_lines.txt
{61 0} {141 0} {70 0} {62 0} {142 0} {71 0} {63 0} {63 63} {143 0}

# awk -v RS= '$1=$1' curly_brace_lines.txt
{61 0} {141 0} {70 0} {62 0} {142 0} {71 0} {63 0} {63 63} {143 0}

# gawk -v RS= '$1=$1' curly_brace_lines.txt
{61 0} {141 0} {70 0} {62 0} {142 0} {71 0} {63 0} {63 63} {143 0}

(There's no nawk command in my Linux system.)
Reply With Quote
  #9  
Old 08-26-2008, 09:21 AM
Default Re: Splitting and joining {61 0} {141 0} {70 0} {62 0}.

On Tuesday 26 August 2008 14:07, Kuhl wrote:

> On my Linux,
>
> # sed 's/} {/}\
> {/g' curly_brace_pairs.txt
> sed: -e expression #1, char 7: Unterminated `s' command


This should work. Are you sure you hit "enter" immediately after the \ in
the first line? Also, if you are using a C shell, try using a different
shell.

> On my Solaris,
>
> # sed ':a;$!{N;ba;};$s/\n/ /g' curly_brace_lines.txt
> N: Event not found.


This is likely because you're using a C shell (csh or tcsh).

> # awk -v RS= '$1=$1' curly_brace_lines.txt
> awk: syntax error near line 1
> awk: bailing out near line 1


On solaris, I believe you have to use /usr/xpg4/bin/awk to get a sane awk.

> On my Linux,
>
> # sed ':a;$!{N;ba;};$s/\n/ /g' curly_brace_lines.txt
> N: Event not found.


Try using a non-C shell.

--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.
Reply With Quote
  #10  
Old 08-26-2008, 09:37 AM
Default Re: Splitting and joining {61 0} {141 0} {70 0} {62 0}.

In article , pk wrote:
>On Tuesday 26 August 2008 14:07, Kuhl wrote:
>
>> On my Linux,
>>
>> # sed 's/} {/}\
>> {/g' curly_brace_pairs.txt
>> sed: -e expression #1, char 7: Unterminated `s' command

>
>This should work. Are you sure you hit "enter" immediately after the \ in
>the first line? Also, if you are using a C shell, try using a different
>shell.


More sensible advice:
1) Don't use sed. Sed is just asking to be disappointed.
2) Use AWK (or Perl or ...) and put the script in a file and run
it from the file. Command line tricks are a) pointless and, as
noted, b) painful under (t)csh type shells.

Or, to amplify on 2), don't try to do it at the command line. Since,
presumably, this will end up as a shell script anyway, you might as well
write it as a (sh-ish) shell script from the start.

Reply With Quote
Reply


Thread Tools
Display Modes



All times are GMT -4. The time now is 01:56 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Integrated by bbpixel2008 :: jvbPlugin R1013.368.1

Search Engine Friendly URLs by vBSEO 3.1.0
vB Ad Management by =RedTyger=
In an effort to better serve ads to our visitors, cookies are used on Mydatabasesupport.com. For more information, check out our Privacy Policy.