Find the answer to your Linux question:
Results 1 to 6 of 6
Hello, I would assume the expression ^$ should match a null string. Yet when I run: echo -n | sed 's/^$/nullstring/' I get no output. Can anyone tell me why?...
  1. #1
    Just Joined!
    Join Date
    Jan 2007
    Posts
    19

    null string matching in sed?

    Hello,

    I would assume the expression ^$ should match a null string. Yet when I run:

    echo -n | sed 's/^$/nullstring/'

    I get no output.

    Can anyone tell me why?

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    I would assume the expression ^$ should match a null string.
    It doesn't. It matches an empty line.
    when I run:

    echo -n | sed 's/^$/nullstring/'

    I get no output.

    Can anyone tell me why?
    Yes. The output is not an empty line. The output is completely empty: that is, no lines at all.

    Hope this helps.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Just Joined!
    Join Date
    Jan 2007
    Posts
    19
    thanks, yes it helps, sort of.

    yes, I see that I can type:

    echo | sed 's/^$/nullstring/' (newline not suppressed in echo command)

    and I get a substitution and output.

    It doesn't make sense to me though, since the doc for GNU sed (which is what I am using) states that when a line is read into the pattern space, the trailing newline is removed.

    My "problem" is this: I am using sed in a shell script to process text that is generated from fields in a document. Each field is processed separately, using a while loop and sequentially going from one field to the next with each pass. When there are no more fields, ie, no more output form my awk '{ print $'$var' } command, I want to exit the loop. Since the output of the awk command is piped to sed anyway, I was trying to use

    sed 's/^$/output/w file'
    test -s file

    to flag the loop to exit.

    So two questions,

    1) is there a way to get sed to flag when there is no input
    2) do you see another way(s) to accomplish this simply.

    I am able to get the job done just by using some reverse logic, (sed 's/.*/output/' - flagging everytime there IS a string, and then clearing the file each time at the top of the loop) but it is a bit clumsier, and more important, I would like have more light on the subject.

    Appreciate your answers, thanks again.

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    the doc for GNU sed (which is what I am using) states that when a line is read into the pattern space, the trailing newline is removed.
    That statement is correct. But sed puts the newline back on when sending a line to standard output.
    1) is there a way to get sed to flag when there is no input
    There's no way that I can see.
    2) do you see another way(s) to accomplish this simply.
    Probably. I know as little about awk as I possibly can, but doesn't it have a way to tell you how many fields are on a line? If so, make that many passes. Maybe something like this:
    Code:
    #!/bin/bash
    
    # ...
    
    field_number=0
    
    while true
    do
      field_number=$(( $field_number + 1 ))
     
      if [[ $field_number -gt $number_of_fields ]]
      then
        break
      fi
    
      # ...
    
    done
    
    # ...
    --
    Bill

    Old age and treachery will overcome youth and skill.

  5. #5
    Just Joined!
    Join Date
    Jan 2007
    Posts
    19
    Thanks, Bill. Sounds like a good solution. Don't know if it is more effecient than the one I am using, but I appreciate the alternative.

  6. #6
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Quote Originally Posted by wje_lf View Post
    I know as little about awk as I possibly can, but doesn't it have a way to tell you how many fields are on a line?
    awk's NF variable:
    Code:
    echo | awk '{print NF}'
    Note that echo -n won't give you zero since it doesn't yield an input line to awk.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...