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?...
- 05-29-2008 #1Just 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?
- 05-29-2008 #2It doesn't. It matches an empty line.I would assume the expression ^$ should match a null string.
Yes. The output is not an empty line. The output is completely empty: that is, no lines at all.when I run:
echo -n | sed 's/^$/nullstring/'
I get no output.
Can anyone tell me why?
Hope this helps.--
Bill
Old age and treachery will overcome youth and skill.
- 05-30-2008 #3Just 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.
- 05-30-2008 #4That statement is correct. But sed puts the newline back on when sending a line to standard output.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.
There's no way that I can see.1) is there a way to get sed to flag when there is no input
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:2) do you see another way(s) to accomplish this simply.
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.
- 05-30-2008 #5Just 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.
- 05-31-2008 #6Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044


Reply With Quote
