From: superpollo on 10 Jan 2010 09:43 mop2 ha scritto: > On Sun, 10 Jan 2010 10:15:10 -0200, superpollo <utente(a)esempio.net> wrote: > >> hi. >> >> i have a text file, and i just wanted to get, say lines 3 thru 7, or >> just line 7. my first guess is: >> >> cat | tail -n+3 | head -n+$((7-3+1)) >> >> but can it be made better? > > > > $ p(){ sed -n $1${2//$2/,$2}p;} > $ seq 9 > 1 > 2 > 3 > 4 > 5 > 6 > 7 > 8 > 9 > $ seq 9|p 3 7 > 3 > 4 > 5 > 6 > 7 > $ seq 9|p 7 > 7 > $ maybe simpler: :( massimo2(a)192.168.1.152:~/temp$ q(){ sed -n $1,$2p;} :) massimo2(a)192.168.1.152:~/temp$ seq 9 | q 3 7 3 4 5 6 7 :) massimo2(a)192.168.1.152:~/temp$ seq 9 | q 3 sed: -e expression #1, char 3: unexpected `,' :( massimo2(a)192.168.1.152:~/temp$ seq 9 | q 3 3 3 :) massimo2(a)192.168.1.152:~/temp$ bye
From: Ed Morton on 10 Jan 2010 11:00 On 1/10/2010 8:12 AM, superpollo wrote: > mop2 ha scritto: >> On Sun, 10 Jan 2010 10:15:10 -0200, superpollo <utente(a)esempio.net> >> wrote: >> >>> hi. >>> >>> i have a text file, and i just wanted to get, say lines 3 thru 7, or >>> just line 7. my first guess is: >>> >>> cat | tail -n+3 | head -n+$((7-3+1)) >>> >>> but can it be made better? >> >> >> >> $ p(){ sed -n $1${2//$2/,$2}p;} >> $ seq 9 >> 1 >> 2 >> 3 >> 4 >> 5 >> 6 >> 7 >> 8 >> 9 >> $ seq 9|p 3 7 >> 3 >> 4 >> 5 >> 6 >> 7 >> $ seq 9|p 7 >> 7 >> $ > > beautiful. > > i guess i must study sed better (i only use it for s///). No, don't study sed, you're using it for what it's best for. For anything else you'd be better spending your time studying awk as anything that sed can do with a simple syntax, awk can also do with a simple syntax but awk can also do a whole lot more with a simple syntax that sed requires a complicated syntax for. In this case: $ p(){ sed -n $1${2//$2/,$2}p;} $ q(){ awk -v s="$1" -v e="${2:-$1}" 'NR==s,NR==e'; } $ seq 9|p 3 7 3 4 5 6 7 $ seq 9|q 3 7 3 4 5 6 7 $ seq 9|p 7 7 $ seq 9|q 7 7 $ seq 9|p bob sed: can't find label for jump to `obp' $ seq 9|q bob Just consider which syntax is easier to understand and which behavior is more intuitive for unexpected input. Also consider how you'd modify the scripts to handle unexpected input. Here's awk: $ q(){ awk -v s="$1" -v e="${2:-$1}" 'BEGIN{ if (s!~/^[0-9]+$/) printf "Expected digits for start line nr, got \"%s\" instead.\n",s; exit} NR==s,NR==e'; } $ seq 9|q bob Expected digits for start line nr, got "bob" instead. Consider how you'd trivially modify the sed script to do the same. Regards, Ed.
From: superpollo on 10 Jan 2010 11:06 Ed Morton ha scritto: > On 1/10/2010 8:12 AM, superpollo wrote: >> mop2 ha scritto: >>> On Sun, 10 Jan 2010 10:15:10 -0200, superpollo <utente(a)esempio.net> >>> wrote: >>> >>>> hi. >>>> >>>> i have a text file, and i just wanted to get, say lines 3 thru 7, or >>>> just line 7. my first guess is: >>>> >>>> cat | tail -n+3 | head -n+$((7-3+1)) >>>> >>>> but can it be made better? >>> >>> >>> >>> $ p(){ sed -n $1${2//$2/,$2}p;} >>> $ seq 9 >>> 1 >>> 2 >>> 3 >>> 4 >>> 5 >>> 6 >>> 7 >>> 8 >>> 9 >>> $ seq 9|p 3 7 >>> 3 >>> 4 >>> 5 >>> 6 >>> 7 >>> $ seq 9|p 7 >>> 7 >>> $ >> >> beautiful. >> >> i guess i must study sed better (i only use it for s///). > > No, don't study sed, you're using it for what it's best for. For > anything else you'd be better spending your time studying awk as > anything that sed can do with a simple syntax, awk can also do with a > simple syntax but awk can also do a whole lot more with a simple syntax > that sed requires a complicated syntax for. In this case: you mean, sed is overengineered? > > $ p(){ sed -n $1${2//$2/,$2}p;} > $ q(){ awk -v s="$1" -v e="${2:-$1}" 'NR==s,NR==e'; } > > $ seq 9|p 3 7 > 3 > 4 > 5 > 6 > 7 > $ seq 9|q 3 7 > 3 > 4 > 5 > 6 > 7 > > $ seq 9|p 7 > 7 > $ seq 9|q 7 > 7 > > $ seq 9|p bob > sed: can't find label for jump to `obp' > $ seq 9|q bob > > Just consider which syntax is easier to understand and which behavior is > more intuitive for unexpected input. Also consider how you'd modify the > scripts to handle unexpected input. Here's awk: > > $ q(){ awk -v s="$1" -v e="${2:-$1}" 'BEGIN{ if (s!~/^[0-9]+$/) printf > "Expected digits for start line nr, got \"%s\" instead.\n",s; exit} > NR==s,NR==e'; } > $ seq 9|q bob > Expected digits for start line nr, got "bob" instead. > > Consider how you'd trivially modify the sed script to do the same. > > Regards, > > Ed. yes, i guess awk is more of a complete programming environment. but still, i would use sed *after* i checked input in my script that uses the sed script in turn (maybe using echo | grep). thanks bye
From: Ed Morton on 10 Jan 2010 11:17 On 1/10/2010 10:06 AM, superpollo wrote: > Ed Morton ha scritto: >> On 1/10/2010 8:12 AM, superpollo wrote: >>> mop2 ha scritto: >>>> On Sun, 10 Jan 2010 10:15:10 -0200, superpollo <utente(a)esempio.net> >>>> wrote: >>>> >>>>> hi. >>>>> >>>>> i have a text file, and i just wanted to get, say lines 3 thru 7, or >>>>> just line 7. my first guess is: >>>>> >>>>> cat | tail -n+3 | head -n+$((7-3+1)) >>>>> >>>>> but can it be made better? >>>> >>>> >>>> >>>> $ p(){ sed -n $1${2//$2/,$2}p;} >>>> $ seq 9 >>>> 1 >>>> 2 >>>> 3 >>>> 4 >>>> 5 >>>> 6 >>>> 7 >>>> 8 >>>> 9 >>>> $ seq 9|p 3 7 >>>> 3 >>>> 4 >>>> 5 >>>> 6 >>>> 7 >>>> $ seq 9|p 7 >>>> 7 >>>> $ >>> >>> beautiful. >>> >>> i guess i must study sed better (i only use it for s///). >> >> No, don't study sed, you're using it for what it's best for. For >> anything else you'd be better spending your time studying awk as >> anything that sed can do with a simple syntax, awk can also do with a >> simple syntax but awk can also do a whole lot more with a simple >> syntax that sed requires a complicated syntax for. In this case: > > you mean, sed is overengineered? > >> >> $ p(){ sed -n $1${2//$2/,$2}p;} >> $ q(){ awk -v s="$1" -v e="${2:-$1}" 'NR==s,NR==e'; } >> >> $ seq 9|p 3 7 >> 3 >> 4 >> 5 >> 6 >> 7 >> $ seq 9|q 3 7 >> 3 >> 4 >> 5 >> 6 >> 7 >> >> $ seq 9|p 7 >> 7 >> $ seq 9|q 7 >> 7 >> >> $ seq 9|p bob >> sed: can't find label for jump to `obp' >> $ seq 9|q bob >> >> Just consider which syntax is easier to understand and which behavior >> is more intuitive for unexpected input. Also consider how you'd modify >> the scripts to handle unexpected input. Here's awk: >> >> $ q(){ awk -v s="$1" -v e="${2:-$1}" 'BEGIN{ if (s!~/^[0-9]+$/) printf >> "Expected digits for start line nr, got \"%s\" instead.\n",s; exit} >> NR==s,NR==e'; } >> $ seq 9|q bob >> Expected digits for start line nr, got "bob" instead. >> >> Consider how you'd trivially modify the sed script to do the same. >> >> Regards, >> >> Ed. > > yes, i guess awk is more of a complete programming environment. but > still, i would use sed *after* i checked input in my script that uses > the sed script in turn (maybe using echo | grep). > Then you still don't need to study sed any further. I'm just saying if you're going to study _something_ to be able to do more text processing than you currently use sed for, then you'd be better off studying awk than sed. If you're happy using a mixture of various shell commands with sed for simple substitutions on a single line, then go for it, just don't bother learning to use sed for anything more complicated than that. Ed.
From: mop2 on 10 Jan 2010 11:22 On Sun, 10 Jan 2010 14:06:29 -0200, superpollo <utente(a)esempio.net> wrote: > still, i would use sed *after* i checked input in my script that uses > the sed script in turn (maybe using echo | grep). > $ p(){ [ "${1//[0-9]/}${2//[0-9]/}" ]&&echo only numbers >&2&&return 1;sed -n $1${2//$2/,$2}p;} $ seq 9|p 4 4 $ seq 9|p 4 6 4 5 6 $ seq 9|p 4 ops only numbers $
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 Prev: new shell in "sh" Next: Mixet.se® Köp & Sälj - Radannonser |