The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
REGEX: Matching Null? deckard Shell Programming and Scripting 2 06-05-2008 11:56 AM
Matching string nehaquick UNIX for Dummies Questions & Answers 7 02-19-2008 11:51 AM
replace a complex string with null fed.linuxgossip Shell Programming and Scripting 3 02-12-2008 06:13 PM
String matching mpang_ Shell Programming and Scripting 3 07-28-2006 06:45 AM
sed problem - replacement string should be same length as matching string. amangeles Shell Programming and Scripting 4 01-11-2006 06:11 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-29-2008
Allasso Allasso is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 22
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 (permalink)  
Old 05-29-2008
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
  
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,641
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 (permalink)  
Old 05-29-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,807
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 (permalink)  
Old 05-29-2008
Allasso Allasso is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 22
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 (permalink)  
Old 05-30-2008
mschwage mschwage is offline
Registered User
  
 

Join Date: Jul 2005
Location: Oak Park, IL
Posts: 102
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 (permalink)  
Old 05-30-2008
Allasso Allasso is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 22
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 (permalink)  
Old 05-30-2008
Allasso Allasso is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 22
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:25 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0