• Re: "sed" question

    From Ottavio Caruso@21:1/5 to All on Wed Mar 6 14:53:51 2024
    Am 06/03/2024 um 14:46 schrieb Ottavio Caruso:
    I have a file that looks like this:

    $ head newsrc-news.eternal-september.org-test
    nl.newsgroups.announce: 1-1329
    free.uk.amateur-radio: 1-15497
    uk.media.radio.misc: 1-456
    uk.radio.amateur: 1-377688,377690,377692,377694-377697,377699,377701-377704 uk.radio.amateur.moderated: 1-6432
    uk.legal.moderated: 1-378428
    comp.security.ssh: 1-3221
    alt.comp.software.newsreaders: 1-481
    uk.net.news.moderation: 1-64428
    free.uk.legal: 1-275

    I want to convert all the last part of the lines from 1-whatever to 1-1

    I have tried:

    $ sed -i 's/"1-*"/'1-1'/g'  newsrc-news.eternal-september.org-test

    and

    $ sed -i 's/"1-*"/"1-1"/g'  newsrc-news.eternal-september.org-test

    $ sed -i 's/"1-*"/1-1/g'  newsrc-news.eternal-september.org-test

    but none of them does the job.

    If I do:

    $ sed -i 's/1-*/"1-1"/g'  newsrc-news.eternal-september.org-test

    then:

    $ head  newsrc-news.eternal-september.org-test
    nl.newsgroups.announce: "1-1""1-1""1-1""1-1"329
    free.uk.amateur-radio: "1-1""1-1""1-1""1-1"5497
    uk.media.radio.misc: "1-1""1-1"456
    uk.radio.amateur: "1-1""1-1"377688,377690,377692,377694-377697,377699,37770"1-1""1-1"377704 uk.radio.amateur.moderated: "1-1""1-1"6432
    uk.legal.moderated: "1-1""1-1"378428
    comp.security.ssh: "1-1""1-1"322"1-1""1-1"
    alt.comp.software.newsreaders: "1-1""1-1"48"1-1""1-1"
    uk.net.news.moderation: "1-1""1-1"64428
    free.uk.legal: "1-1""1-1"275


    which is not what I want.

    Any help will be appreciated.


    For posterity, Grant Taylor on alt.comp.software.thunderbird suggested
    this and worked:


    $ awk '{print $1, "1-1"}' newsrc-news.eternal-september.org-test > newsrc-news.eternal-september.org


    Sorry for the noise. I never got to learn awk properly.

    --
    Ottavio Caruso

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ottavio Caruso@21:1/5 to All on Wed Mar 6 14:46:34 2024
    I have a file that looks like this:

    $ head newsrc-news.eternal-september.org-test
    nl.newsgroups.announce: 1-1329
    free.uk.amateur-radio: 1-15497
    uk.media.radio.misc: 1-456
    uk.radio.amateur: 1-377688,377690,377692,377694-377697,377699,377701-377704 uk.radio.amateur.moderated: 1-6432
    uk.legal.moderated: 1-378428
    comp.security.ssh: 1-3221
    alt.comp.software.newsreaders: 1-481
    uk.net.news.moderation: 1-64428
    free.uk.legal: 1-275

    I want to convert all the last part of the lines from 1-whatever to 1-1

    I have tried:

    $ sed -i 's/"1-*"/'1-1'/g' newsrc-news.eternal-september.org-test

    and

    $ sed -i 's/"1-*"/"1-1"/g' newsrc-news.eternal-september.org-test

    $ sed -i 's/"1-*"/1-1/g' newsrc-news.eternal-september.org-test

    but none of them does the job.

    If I do:

    $ sed -i 's/1-*/"1-1"/g' newsrc-news.eternal-september.org-test

    then:

    $ head newsrc-news.eternal-september.org-test
    nl.newsgroups.announce: "1-1""1-1""1-1""1-1"329
    free.uk.amateur-radio: "1-1""1-1""1-1""1-1"5497
    uk.media.radio.misc: "1-1""1-1"456
    uk.radio.amateur: "1-1""1-1"377688,377690,377692,377694-377697,377699,37770"1-1""1-1"377704 uk.radio.amateur.moderated: "1-1""1-1"6432
    uk.legal.moderated: "1-1""1-1"378428
    comp.security.ssh: "1-1""1-1"322"1-1""1-1"
    alt.comp.software.newsreaders: "1-1""1-1"48"1-1""1-1"
    uk.net.news.moderation: "1-1""1-1"64428
    free.uk.legal: "1-1""1-1"275


    which is not what I want.

    Any help will be appreciated.

    --
    Ottavio Caruso

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kenny McCormack@21:1/5 to [email protected] on Wed Mar 6 14:59:09 2024
    In article <us9vka$fepq$[email protected]>,
    Ottavio Caruso <[email protected]> wrote:
    I have a file that looks like this:

    $ head newsrc-news.eternal-september.org-test
    nl.newsgroups.announce: 1-1329
    free.uk.amateur-radio: 1-15497
    uk.media.radio.misc: 1-456
    uk.radio.amateur: 1-377688,377690,377692,377694-377697,377699,377701-377704 >uk.radio.amateur.moderated: 1-6432
    uk.legal.moderated: 1-378428
    comp.security.ssh: 1-3221
    alt.comp.software.newsreaders: 1-481
    uk.net.news.moderation: 1-64428
    free.uk.legal: 1-275

    I want to convert all the last part of the lines from 1-whatever to 1-1

    Two comments:
    1) "sed" is almost never the right answer, regardless of what the
    question is. AWK can do everything (with maybe one or two obscure
    exceptions) that sed can do, and is a lot easier to use.

    2) Why not just:

    awk 'sub(/1-[0-9]+$/,"1-1");{ print "Line:",$0,"does not fit the expected pattern." > "/dev/stderr" }' yourfile

    --
    Many (most?) Trump voters voted for him because they thought if they
    supported Trump enough, they'd get to *be* Trump.

    Similarly, Trump believes that if *he* praises Putin enough, he'll get to *be* Putin.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ottavio Caruso@21:1/5 to All on Wed Mar 6 15:10:40 2024
    Am 06/03/2024 um 14:59 schrieb Kenny McCormack:
    In article <us9vka$fepq$[email protected]>,
    Ottavio Caruso <[email protected]> wrote:
    I have a file that looks like this:

    $ head newsrc-news.eternal-september.org-test
    nl.newsgroups.announce: 1-1329
    free.uk.amateur-radio: 1-15497
    uk.media.radio.misc: 1-456
    uk.radio.amateur: 1-377688,377690,377692,377694-377697,377699,377701-377704 >> uk.radio.amateur.moderated: 1-6432
    uk.legal.moderated: 1-378428
    comp.security.ssh: 1-3221
    alt.comp.software.newsreaders: 1-481
    uk.net.news.moderation: 1-64428
    free.uk.legal: 1-275

    I want to convert all the last part of the lines from 1-whatever to 1-1

    Two comments:
    1) "sed" is almost never the right answer, regardless of what the
    question is. AWK can do everything (with maybe one or two obscure
    exceptions) that sed can do, and is a lot easier to use.

    2) Why not just:

    awk 'sub(/1-[0-9]+$/,"1-1");{ print "Line:",$0,"does not fit the expected pattern." > "/dev/stderr" }' yourfile


    I actually got myself in trouble because I realised this is not what I
    really wanted, so I just made a lot of noise for nuffink.

    --
    Ottavio Caruso

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kenny McCormack@21:1/5 to [email protected] on Wed Mar 6 15:20:36 2024
    In article <usa1ei$hr$[email protected]>,
    Grant Taylor <[email protected]> wrote:
    On 3/6/24 08:46, Ottavio Caruso wrote:
    Any help will be appreciated.

    Copying my sed version reply from alt.comp.software.thunderbird here for >completeness:

    mv newsrc newsrc.backup
    sed 's/:.*/: 1-1/' newsrc.backup > newsrc

    sed -i 's/:.*/: 1-1/' newsrc

    --
    Most Southerners interest in, knowledge of, and participation in politics begins with
    and ends with: Screw the blacks. If a guy is onboard with that, he's our guy!

    Get them back in chains where they belong!

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Grant Taylor@21:1/5 to Ottavio Caruso on Wed Mar 6 09:17:38 2024
    On 3/6/24 08:46, Ottavio Caruso wrote:
    Any help will be appreciated.

    Copying my sed version reply from alt.comp.software.thunderbird here for completeness:

    mv newsrc newsrc.backup
    sed 's/:.*/: 1-1/' newsrc.backup > newsrc



    --
    Grant. . . .

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to Ottavio Caruso on Wed Mar 6 20:40:04 2024
    On 06.03.2024 15:53, Ottavio Caruso wrote:
    Am 06/03/2024 um 14:46 schrieb Ottavio Caruso:
    I have a file that looks like this:

    $ head newsrc-news.eternal-september.org-test
    nl.newsgroups.announce: 1-1329
    free.uk.amateur-radio: 1-15497
    uk.media.radio.misc: 1-456
    uk.radio.amateur:
    1-377688,377690,377692,377694-377697,377699,377701-377704
    uk.radio.amateur.moderated: 1-6432
    uk.legal.moderated: 1-378428
    comp.security.ssh: 1-3221
    alt.comp.software.newsreaders: 1-481
    uk.net.news.moderation: 1-64428
    free.uk.legal: 1-275

    [...]

    Any help will be appreciated.


    For posterity, Grant Taylor on alt.comp.software.thunderbird suggested
    this and worked:


    $ awk '{print $1, "1-1"}' newsrc-news.eternal-september.org-test > newsrc-news.eternal-september.org

    In this specific case of regular data you can simplify that to

    awk '$2="1-1"' sourcefile > targetfile


    Janis

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Julieta Shem@21:1/5 to Ottavio Caruso on Wed Mar 6 18:26:34 2024
    Ottavio Caruso <[email protected]> writes:

    [...]

    Sorry for the noise. I never got to learn awk properly.

    The book

    The AWK Programming Language
    Alfred V. Aho, Brian W. Kernighan, Peter J. Weinberger
    Addison-Wesley Publishing Company, 1988
    ISBN 0-201-07981-X

    is one of the most interesting books on programming ever written.
    (Along with SICP, HtDP and others.)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to Keith Thompson on Wed Mar 6 23:32:31 2024
    On 06.03.2024 22:50, Keith Thompson wrote:
    Julieta Shem <[email protected]> writes:

    The book

    The AWK Programming Language
    Alfred V. Aho, Brian W. Kernighan, Peter J. Weinberger
    Addison-Wesley Publishing Company, 1988
    ISBN 0-201-07981-X

    is one of the most interesting books on programming ever written.

    The second edition was published in 2024.

    This is a very notable fact given that the first edition was from 1988!

    Janis

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to Grant Taylor on Thu Mar 7 02:54:48 2024
    On 07.03.2024 02:34, Grant Taylor wrote:
    On 3/6/24 13:40, Janis Papanagnou wrote:
    In this specific case of regular data you can simplify that to

    awk '$2="1-1"' sourcefile > targetfile

    Interesting.

    I don't know that I've ever seen (re)defining a field value as part of
    the script.

    Assigning to a field is possible but you should be aware that the
    fields in $0 will become reorganized then... (mind the blank spaces)

    awk '$2="NEW"' <<EOT
    abc defg ghi
    jk lm no pq
    EOT

    Output:
    abc NEW ghi
    jk NEW no pq

    You see that there's then (per default) only single delimiting
    spaces left. You can change that by delimiter OFS, e.g. OFS="\t"
    would place TABs in between (but would not preserve the original
    spacing).

    If you'd want to retain the previous spaces it would require some
    other (not so trivial) handling.

    Janis

    [...]

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Grant Taylor@21:1/5 to Kenny McCormack on Wed Mar 6 19:32:56 2024
    On 3/6/24 09:20, Kenny McCormack wrote:
    sed -i 's/:.*/: 1-1/' newsrc

    Agreed /if/ the sed that the OP is using supports `-i`.

    My day job has me working across multiple platforms and try to do things
    more consistently across the platforms.



    --
    Grant. . . .

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Grant Taylor@21:1/5 to Janis Papanagnou on Wed Mar 6 19:34:55 2024
    On 3/6/24 13:40, Janis Papanagnou wrote:
    In this specific case of regular data you can simplify that to

    awk '$2="1-1"' sourcefile > targetfile

    Interesting.

    I don't know that I've ever seen (re)defining a field value as part of
    the script.

    I'll have to file that one away.

    Today I learned.

    Thank you.



    --
    Grant. . . .

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ottavio Caruso@21:1/5 to All on Thu Mar 7 09:48:17 2024
    Am 06/03/2024 um 21:26 schrieb Julieta Shem:
    Ottavio Caruso <[email protected]> writes:

    [...]

    Sorry for the noise. I never got to learn awk properly.

    The book

    The AWK Programming Language
    Alfred V. Aho, Brian W. Kernighan, Peter J. Weinberger
    Addison-Wesley Publishing Company, 1988
    ISBN 0-201-07981-X

    is one of the most interesting books on programming ever written.
    (Along with SICP, HtDP and others.)

    Smartass.

    --
    Ottavio Caruso

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ottavio Caruso@21:1/5 to All on Thu Mar 7 09:49:34 2024
    Am 06/03/2024 um 21:50 schrieb Keith Thompson:
    Julieta Shem <[email protected]> writes:
    Ottavio Caruso <[email protected]> writes:

    [...]

    Sorry for the noise. I never got to learn awk properly.

    The book

    The AWK Programming Language
    Alfred V. Aho, Brian W. Kernighan, Peter J. Weinberger
    Addison-Wesley Publishing Company, 1988
    ISBN 0-201-07981-X

    is one of the most interesting books on programming ever written.
    (Along with SICP, HtDP and others.)

    The second edition was published in 2024.

    SICP is "Structure and Interpretation of Computer Programs" by Harold
    Abelson and Gerald Jay Sussman with Julie Sussman.

    HtDP is "How to Design Programs" by Matthias Felleisen, Robert Bruce
    Findler, Matthew Flatt, and Shriram Krishnamurthi.


    For fucks sake, I know what awk is. I just never bothered learning it.

    Just because you see a foreign name, you all expect we live in a cave.

    --
    Ottavio Caruso

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ottavio Caruso@21:1/5 to All on Thu Mar 7 09:51:56 2024
    Am 07/03/2024 um 01:32 schrieb Grant Taylor:
    On 3/6/24 09:20, Kenny McCormack wrote:
    sed -i 's/:.*/: 1-1/' newsrc

    Agreed /if/ the sed that the OP is using supports `-i`.

    My day job has me working across multiple platforms and try to do things
    more consistently across the platforms.




    Thanks Grant. I am unsubscribing from this NG, so you won't catch me here.

    --
    Ottavio Caruso

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kenny McCormack@21:1/5 to [email protected] on Thu Mar 7 12:05:50 2024
    In article <usc2je$vsku$[email protected]>,
    Ottavio Caruso <[email protected]> wrote:
    ...
    For fucks sake, I know what awk is. I just never bothered learning it.

    You were responding to Leader Keith. Yes, Keith is an asshole, but he's
    our asshole and we love him for it.

    Just because you see a foreign name, you all expect we live in a cave.

    Do you even have electricity where you live?

    --
    Just like Donald Trump today, Jesus Christ had a Messiah complex.

    And, in fact, the similarities between the two figures are quite striking.
    For example, both have a ragtag band of followers, whose faith cannot be shaken.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Grant Taylor@21:1/5 to Janis Papanagnou on Thu Mar 7 17:56:04 2024
    On 3/6/24 19:54, Janis Papanagnou wrote:
    You see that there's then (per default) only single delimiting spaces
    left.

    I think I see what you're saying. I also feel like I must be missing
    something obvious, because what you're describing is what I would expect
    awk to do.

    You can change that by delimiter OFS, e.g. OFS="\t" would place TABs
    in between (but would not preserve the original spacing).

    I've dabbled with OFS, and IFS, a few times as needed. But most of the
    time I don't need to.

    If you'd want to retain the previous spaces it would require some other
    (not so trivial) handling.

    I can't think of a use case where I would want that and choose to use
    awk. -- I'm sure such use cases exist, I'm just ignorant of them.



    --
    Grant. . . .

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From vallor@21:1/5 to [email protected] on Fri Mar 8 02:51:32 2024
    On Thu, 07 Mar 2024 14:32:58 -0800, Keith Thompson <[email protected]> wrote in <[email protected]>:

    Ottavio Caruso <[email protected]> writes:
    Am 06/03/2024 um 21:50 schrieb Keith Thompson:
    Julieta Shem <[email protected]> writes:
    Ottavio Caruso <[email protected]> writes:

    [...]

    Sorry for the noise. I never got to learn awk properly.

    The book

    The AWK Programming Language Alfred V. Aho, Brian W. Kernighan,
    Peter J. Weinberger Addison-Wesley Publishing Company, 1988 ISBN
    0-201-07981-X

    is one of the most interesting books on programming ever written.
    (Along with SICP, HtDP and others.)
    The second edition was published in 2024.
    SICP is "Structure and Interpretation of Computer Programs" by Harold
    Abelson and Gerald Jay Sussman with Julie Sussman.
    HtDP is "How to Design Programs" by Matthias Felleisen, Robert Bruce
    Findler, Matthew Flatt, and Shriram Krishnamurthi.

    For fucks sake, I know what awk is. I just never bothered learning it.

    Just because you see a foreign name, you all expect we live in a cave.

    What on Earth are you talking about? I didn't even notice your name.
    I just expanded on what Julieta Shem wrote, and you somehow managed to
    take offense. (I didn't know what HtDP means; I had to look it up, and
    then I shared that information with the group.)

    Someone having a bad day?

    Anyway, thank you for the heads-up that a new edition
    of the AWK book was out. I bought it on Kindle -- would
    have bought hardcopy, but Amazon seems to only have it
    in paperback (and Kindle).

    (Coincidentally, I have a question about shell providers,
    but will post that separately.)

    --
    -v

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kenny McCormack@21:1/5 to [email protected] on Fri Mar 8 03:11:22 2024
    In article <usdufk$19m29$[email protected]>, vallor <[email protected]> wrote: ...
    Someone having a bad day?

    Yes, but with Keith, every day is a bad day.

    You get used to it.

    --
    'Islamaphobia' is a term created by fascists and used by cowards to manipulate morons.

    - Author unknown, quoted by Sam Harris -

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Julieta Shem@21:1/5 to Keith Thompson on Fri Mar 8 09:14:48 2024
    Keith Thompson <[email protected]> writes:

    Ottavio Caruso <[email protected]> writes:
    Am 06/03/2024 um 21:50 schrieb Keith Thompson:
    Julieta Shem <[email protected]> writes:
    Ottavio Caruso <[email protected]> writes:

    [...]

    Sorry for the noise. I never got to learn awk properly.

    The book

    The AWK Programming Language
    Alfred V. Aho, Brian W. Kernighan, Peter J. Weinberger
    Addison-Wesley Publishing Company, 1988
    ISBN 0-201-07981-X

    is one of the most interesting books on programming ever written.
    (Along with SICP, HtDP and others.)
    The second edition was published in 2024.
    SICP is "Structure and Interpretation of Computer Programs" by
    Harold
    Abelson and Gerald Jay Sussman with Julie Sussman.
    HtDP is "How to Design Programs" by Matthias Felleisen, Robert Bruce
    Findler, Matthew Flatt, and Shriram Krishnamurthi.

    For fucks sake, I know what awk is. I just never bothered learning it.

    Just because you see a foreign name, you all expect we live in a cave.

    What on Earth are you talking about? I didn't even notice your name.
    I just expanded on what Julieta Shem wrote, and you somehow managed
    to take offense. (I didn't know what HtDP means; I had to look it
    up, and then I shared that information with the group.)

    He doesn't know how to use NNTP. Newbies, you know.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lawrence D'Oliveiro@21:1/5 to Ottavio Caruso on Mon Mar 25 02:31:56 2024
    On Wed, 6 Mar 2024 14:53:51 +0000, Ottavio Caruso wrote:

    Sorry for the noise. I never got to learn awk properly.

    I never bothered, because Perl does everything that Awk does, just as concisely, and more besides.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)