| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
|
| 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. |
|
#2
|
| 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. |
|
#3
|
| 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. |
|
#4
|
| On 8/25/2008 9:06 AM, Bill Marcum wrote: > On 2008-08-25, Kuhl > >> >>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. |
|
#5
|
| 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. |
|
#6
|
| 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 |
|
#7
|
| On Aug 25, 9:50 pm, Kuhl > 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 |
|
#8
|
| 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.) |
|
#9
|
| 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. |
|
#10
|
| In article >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. |
![]() |
| Thread Tools | |
| Display Modes | |