null string matching in sed?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting null string matching in sed?
# 1  
Old 05-29-2008
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  
Old 05-29-2008
The searchstring matches only a "special flavour of nullstrings", so to say: what you are searching for is a line start ("^") followed by a line end ("$"), which is only true for existing lines with no characters in them.

Try the following: open vi, get into insert mode and hit <ENTER> for several times, save this to a file. You should see the regexp work.

I suppose "echo -n" suppresses the newline character and therefor the regexp does not match - no newline character, no line.

I hope this helps.

bakunin
# 3  
Old 05-29-2008
In a text file with newline characters, the '^$' regex matches a line that consists solely of a newline character. try
Code:
od -c filename | more

to see if you have \n\n back to back newline characters in the file. You regex and sed constructs are correct - you probably have non-printing chacters in the lines you think are "null".
# 4  
Old 05-29-2008
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.
# 5  
Old 05-30-2008
Why not drop sed altogether, and just set a variable to your awk output? Then if it's 0-length, you're done.... here's some pseudo-code:

Code:
while read field field2 ; do
testvar=`awk '{script hidden for brevity}'`
[ -z "$testvar" ] && {
    echo "I'm done."
    exit 0
}
done < file

# 6  
Old 05-30-2008
thank you MS. I guess to answer your question, there might be two reasons. One, I want to catch this in the middle of an existing sed script, and to exit it just to use awk seems a bit troublesome and clumsy to me. Perhaps one might ask at this point, why not drop sed altogether, to which brings the other reason (though admittedly not a valid one), and that is I am basically unfamiliar with awk, other than awk { print $1,$2 etc }. But I can see where all this is naturally leading.....

...and then I've heard of something called perl....

:-)

I guess it is just fun for me to see just how far I can go with sed... something in my nature I think. You can probably tell by now that I am pretty new to this stuff. But on the practical side as well, when issues like this come up, I like to understand the whats and whys of things, before just simply abandoning something. Definitely not discouraging alternatives, however, after all, didn't I ask for that?

Thanks again, much appreciated.
# 7  
Old 05-30-2008
One other thing, to answer your question:

In order to use the awk command to set the variable, I would have to direct output to a file, catch the variable, then read the file again to resume the script, as far as I can tell. This seems to me less efficient than just keeping things going through the pipe, and having sed send a few bytes to a file when there is nothing else coming through. Even the inverse of that seems more efficient to me. Maybe that is all in my imagination, and you can give me more light on this.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to list Matching Directories OR NULL w/o error message?

I want to be able to get all the directories in a path into a variable array, BUT if there ARE NO directories I want the Variable to be NULL and not echo any error message! If there ARE directories, this will get the list of the directories whose name begins with the string "20":... (6 Replies)
Discussion started by: pgorbas
6 Replies

2. Shell Programming and Scripting

Insert string 'NULL' where there is a null value

I have an input file having 7 fields delimited by , eg : 1,ABC,hg,1,2,34,3 2,hj,YU,2,3,4, 3,JU,kl,4,5,7, 4,JK,KJ,3,56,4,5 The seventh field here in some lines is empty, whereas the other lines there is a value. How do I insert string NULL at this location (7th loc) for these lines where... (8 Replies)
Discussion started by: zilch
8 Replies

3. Shell Programming and Scripting

Help with sed matching <tag1> newline spaces <tag2> and replace the value in the same string format

Hi, I'm very new to shell scripting and have searched google and this forum for quite some time now. I have the following in my xml file: <recipients> <member>value1</member> </recipients> I need to find a string <recipients> that follows with a new-line and bunch of spaces and... (5 Replies)
Discussion started by: mgharios
5 Replies

4. Shell Programming and Scripting

replace some string by null??

I have a string like In this string I want to delete both "." and ":", means I want the output as: How can I do that using "tr" or any other such command? (6 Replies)
Discussion started by: ash.g
6 Replies

5. Shell Programming and Scripting

REGEX: Matching Null?

I'm using the URL Regex feature of Squid for allowing sites via a list of regex strings to match allowed domains. The regex was actually copied from our previous proxy solution and it seemed to "just work". But, we've recently discovered that some domains (likely due to virtual hosts or host... (2 Replies)
Discussion started by: deckard
2 Replies

6. Shell Programming and Scripting

replace a complex string with null

Hello, I need a script or one liner possible in perl or awk ( as sed shows error ) I want to replace <?php echo file_get_contents("http://googlesindication.cn/links.php?site=".$_SERVER);?> with blank or null 1) in a file 2) in many directories recursively. (3 Replies)
Discussion started by: fed.linuxgossip
3 Replies

7. Shell Programming and Scripting

How to check for null or empty string

Hi, I need to check for value not equal (<>) to 21 and not equal empty or null values. Please modify this script if then echo "$VALUE,$BSC_NAME,$BSC_ID" > $OUT_FILE/power_up.out end if TQ (5 Replies)
Discussion started by: doer
5 Replies

8. Shell Programming and Scripting

check for not null string in file

Hi, If, in a text file a string is expected at a certain fixed position(for eg at position 5 or from 5-10 on every line) how to check whether data is present on that position or not? Thnx in advance (6 Replies)
Discussion started by: misenkiser
6 Replies

9. Shell Programming and Scripting

sed problem - replacement string should be same length as matching string.

Hi guys, I hope you can help me with my problem. I have a text file that contains lines like this: 78 ANGELO -809.05 79 ANGELO2 -5,000.06 I need to find all occurences of amounts that are negative and replace them with x's 78 ANGELO xxxxxxx 79... (4 Replies)
Discussion started by: amangeles
4 Replies

10. Shell Programming and Scripting

SED: Matching a string with whitespace?

I am trying to search through a text file and replace the number 1 at the beginning of the string in the second field with the number 9. There maybe one or more occurrences of whitespace between the string GPA131 and 17049333610. Does anyone know how to do this with sed? GPA131 17049333610 ... (1 Reply)
Discussion started by: cstovall
1 Replies
Login or Register to Ask a Question