| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| Hello everybody, I am looking for a way to print the contents of a line beginning with the n-th field until the end of the line (or until some other field). Is there a way to do this without using a loop? I mean, is there a syntax how to specify - say - the 3rd to 5th field - something like ${3..5}? I have been looking for this for a while now but it does not seem to be covered anywhere... Cheers, Wolfgang |
|
#2
| |||
| |||
| On 8/1/2008 4:34 PM, wolfgang.arendt@googlemail.com wrote: > Hello everybody, > > I am looking for a way to print the contents of a line beginning with > the n-th field until the end of the line (or until some other field). > Is there a way to do this without using a loop? I mean, is there a > syntax how to specify - say - the 3rd to 5th field - something like > ${3..5}? > I have been looking for this for a while now but it does not seem to > be covered anywhere... > > Cheers, Wolfgang $ echo "a b c d e f" | gawk --re-interval '{sub(/^[[:space:]]*([^[:space:]]*[[:space:]]*){2}/,"")}1' c d e f $ echo "a b c d e f " | gawk --re-interval '{sub(/([[:space:]]*[^[:space:]]*){2}[[:space:]]*$/,"")}1' a b c d $ echo "a b c d e f" | gawk --re-interval '{ sub(/^[[:space:]]*([^[:space:]]*[[:space:]]*){2}/,"") sub(/([[:space:]]*[^[:space:]]*){2}[[:space:]]*$/,"") }1' c d Regards, Ed. |
|
#3
| |||
| |||
| On 2 août, 00:07, Ed Morton <mor...@lsupcaemnt.com> wrote: > On 8/1/2008 4:34 PM, wolfgang.are...@googlemail.com wrote: > > > Hello everybody, > > > I am looking for a way to print the contents of a line beginning with > > the n-th field until the end of the line (or until some other field). > > Is there a way to do this without using a loop? I mean, is there a > > syntax how to specify - say - the 3rd to 5th field - something like > > ${3..5}? > > I have been looking for this for a while now but it does not seem to > > be covered anywhere... > > > Cheers, Wolfgang > > $ echo "a b c d e f" | > gawk --re-interval '{sub(/^[[:space:]]*([^[:space:]]*[[:space:]]*){2}/,"")}1' > c d e f > > $ echo "a b c d e f " | > gawk --re-interval '{sub(/([[:space:]]*[^[:space:]]*){2}[[:space:]]*$/,"")}1' > a b c d > > $ echo "a b c d e f" | > gawk --re-interval '{ > sub(/^[[:space:]]*([^[:space:]]*[[:space:]]*){2}/,"") > sub(/([[:space:]]*[^[:space:]]*){2}[[:space:]]*$/,"")}1' > > c d > > Regards, > > Ed. Ok guys, why using a beautiful command (of course we are on comp.lang.awk) while these ones do the correct job ? Exemple : echo "a b c d e f" | tr -s ' ' | cut -d' ' -f1-4 a b c d My "mentor" told me to use the simpliest before the complex. |
|
#4
| |||
| |||
| Mon, 04 Aug 2008 05:26:07 -0700, poulteki did catÂ*: > On 2 août, 00:07, Ed Morton <mor...@lsupcaemnt.com> wrote: >> On 8/1/2008 4:34 PM, wolfgang.are...@googlemail.com wrote: >> >> > Hello everybody, >> >> > I am looking for a way to print the contents of a line beginning with >> > the n-th field until the end of the line (or until some other field). >> > Is there a way to do this without using a loop? I mean, is there a >> > syntax how to specify - say - the 3rd to 5th field - something like >> > ${3..5}? >> > I have been looking for this for a while now but it does not seem to >> > be covered anywhere... >> >> > Cheers, Wolfgang >> >> $ echo "a b c d e f" | >> gawk --re-interval >> '{sub(/^[[:space:]]*([^[:space:]]*[[:space:]]*){2}/,"")}1' c d e f >> >> $ echo "a b c d e f " | >> gawk --re-interval >> '{sub(/([[:space:]]*[^[:space:]]*){2}[[:space:]]*$/,"")}1' a b c d >> >> $ echo "a b c d e f" | >> gawk --re-interval '{ >> sub(/^[[:space:]]*([^[:space:]]*[[:space:]]*){2}/,"") >> sub(/([[:space:]]*[^[:space:]]*){2}[[:space:]]*$/,"")}1' >> >> c d >> >> Regards, >> >> Ed. > > Ok guys, > why using a beautiful command (of course we are on comp.lang.awk) while > these ones do the correct job ? Probably because they don't !-) > Exemple : > echo "a b c d e f" | tr -s ' ' | cut -d' ' -f1-4 a b c d Firstly, what the OP had in mind was (as mentionned in the subject line) closer to: $ echo "a b c d e f" | tr -s ' ' | cut -d' ' -f4- d e f And, there was a catch in it, preserve the "pavement" :-) As you can see there are many blanks lost with the 'cut': $ echo "a b c d e f" | tr -s ' ' | cut -d' ' -f4- d e f > My "mentor" told me to use the simpliest before the complex. Writing on the wall is a goo story to read, my janitor told me to read before to write ;D) |
|
#5
| |||
| |||
| poulteki wrote: > On 2 août, 00:07, Ed Morton <mor...@lsupcaemnt.com> wrote: > >>On 8/1/2008 4:34 PM, wolfgang.are...@googlemail.com wrote: >> >> >>>Hello everybody, >> >>>I am looking for a way to print the contents of a line beginning with >>>the n-th field until the end of the line (or until some other field). >>>Is there a way to do this without using a loop? I mean, is there a >>>syntax how to specify - say - the 3rd to 5th field - something like >>>${3..5}? >>>I have been looking for this for a while now but it does not seem to >>>be covered anywhere... >> >>>Cheers, Wolfgang >> >>$ echo "a b c d e f" | >>gawk --re-interval '{sub(/^[[:space:]]*([^[:space:]]*[[:space:]]*){2}/,"")}1' >>c d e f >> >>$ echo "a b c d e f " | >>gawk --re-interval '{sub(/([[:space:]]*[^[:space:]]*){2}[[:space:]]*$/,"")}1' >>a b c d >> >>$ echo "a b c d e f" | >>gawk --re-interval '{ >>sub(/^[[:space:]]*([^[:space:]]*[[:space:]]*){2}/,"") >>sub(/([[:space:]]*[^[:space:]]*){2}[[:space:]]*$/,"")}1' >> >>c d >> >>Regards, >> >> Ed. > > > Ok guys, > why using a beautiful command (of course we are on comp.lang.awk) > while these ones do the correct job ? > > Exemple : > echo "a b c d e f" | tr -s ' ' | cut -d' ' -f1-4 > a b c d > > My "mentor" told me to use the simpliest before the complex. But your solution, as opposed to Ed's, changes the spacing between the fields (which might not be what one expects in any case). Janis |
|
#6
| |||
| |||
| On 4 août, 14:35, Janis Papanagnou <Janis_Papanag...@hotmail.com> wrote: > poulteki wrote: > > On 2 août, 00:07, Ed Morton <mor...@lsupcaemnt.com> wrote: > > >>On 8/1/2008 4:34 PM, wolfgang.are...@googlemail.com wrote: > > >>>Hello everybody, > > >>>I am looking for a way to print the contents of a line beginning with > >>>the n-th field until the end of the line (or until some other field). > >>>Is there a way to do this without using a loop? I mean, is there a > >>>syntax how to specify - say - the 3rd to 5th field - something like > >>>${3..5}? > >>>I have been looking for this for a while now but it does not seem to > >>>be covered anywhere... > > >>>Cheers, Wolfgang > > >>$ echo "a b c d e f" | > >>gawk --re-interval '{sub(/^[[:space:]]*([^[:space:]]*[[:space:]]*){2}/,"")}1' > >>c d e f > > >>$ echo "a b c d e f " | > >>gawk --re-interval '{sub(/([[:space:]]*[^[:space:]]*){2}[[:space:]]*$/,"")}1' > >>a b c d > > >>$ echo "a b c d e f" | > >>gawk --re-interval '{ > >>sub(/^[[:space:]]*([^[:space:]]*[[:space:]]*){2}/,"") > >>sub(/([[:space:]]*[^[:space:]]*){2}[[:space:]]*$/,"")}1' > > >>c d > > >>Regards, > > >> Ed. > > > Ok guys, > > why using a beautiful command (of course we are on comp.lang.awk) > > while these ones do the correct job ? > > > Exemple : > > echo "a b c d e f" | tr -s ' ' | cut -d' ' -f1-4 > > a b c d > > > My "mentor" told me to use the simpliest before the complex. > > But your solution, as opposed to Ed's, changes the spacing between > the fields (which might not be what one expects in any case). > > Janis Okay, sorry guys, you are the best. |
|
#7
| |||
| |||
| On 8/4/2008 7:26 AM, poulteki wrote: > On 2 août, 00:07, Ed Morton <mor...@lsupcaemnt.com> wrote: > >>On 8/1/2008 4:34 PM, wolfgang.are...@googlemail.com wrote: >> >> >>>Hello everybody, >> >>>I am looking for a way to print the contents of a line beginning with >>>the n-th field until the end of the line (or until some other field). >>>Is there a way to do this without using a loop? I mean, is there a >>>syntax how to specify - say - the 3rd to 5th field - something like >>>${3..5}? >>>I have been looking for this for a while now but it does not seem to >>>be covered anywhere... >> >>>Cheers, Wolfgang >> >>$ echo "a b c d e f" | >>gawk --re-interval '{sub(/^[[:space:]]*([^[:space:]]*[[:space:]]*){2}/,"")}1' >>c d e f >> >>$ echo "a b c d e f " | >>gawk --re-interval '{sub(/([[:space:]]*[^[:space:]]*){2}[[:space:]]*$/,"")}1' >>a b c d >> >>$ echo "a b c d e f" | >>gawk --re-interval '{ >>sub(/^[[:space:]]*([^[:space:]]*[[:space:]]*){2}/,"") >>sub(/([[:space:]]*[^[:space:]]*){2}[[:space:]]*$/,"")}1' >> >>c d >> > Ok guys, > why using a beautiful command (of course we are on comp.lang.awk) > while these ones do the correct job ? As you said, this is comp.lang.awk where people come for an awk solution. Typically if an OP says they're on a particular OS and we think there's a significantly better solution on that OS, we'll direct them to an appropriate NG where the experts in that OS can suggest and/or critique a solution so the OP gets the best solution for that OS rather than the just the best we can come up with in this NG for that OS. In this case, there's a slightly briefer sed solution and, as always, there's probably similair ruby and perl solution, but it wasn't worth mentioning. > Exemple : > echo "a b c d e f" | tr -s ' ' | cut -d' ' -f1-4 > a b c d > > My "mentor" told me to use the simpliest before the complex. OK, but if the OP is on a Windows box and has no idea about UNIX tools, implementing the above would be more complex than implementing the awk solution. Ed. |
|
#8
| |||
| |||
| In article <489711D4.50902@lsupcaemnt.com>, Ed Morton <morton@lsupcaemnt.com> wrote: .... >As you said, this is comp.lang.awk where people come for an awk >solution. Typically if an OP says they're on a particular OS and we >think there's a significantly better solution on that OS, we'll direct >them to an appropriate NG where the experts in that OS can suggest >and/or critique a solution so the OP gets the best solution for that OS >rather than the just the best we can come up with in this NG for that >OS. Agreed, totally. >In this case, there's a slightly briefer sed solution and, as always, >there's probably similair ruby and perl solution, but it wasn't worth >mentioning. Well put. >> Exemple : echo "a b c d e f" | tr -s ' ' | cut -d' ' -f1-4 a b c >> d >> >> My "mentor" told me to use the simpliest before the complex. > >OK, but if the OP is on a Windows box and has no idea about UNIX tools, >implementing the above would be more complex than implementing the awk >solution. Yes, and note that simplicity != efficiency. I always get a kick out of these: grep | tr | sed | grep | cut | ... "solutions", where a simple script in AWK (or any other capable language; note that I do not count any of the above named commands as "capable") would do the job. Once you bring in multiple processes, you can kiss efficiency goodbye. |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.