![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
||||
|
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". |
|
||||
|
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. |
|
||||
|
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
|
|
||||
|
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. |
|
||||
|
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. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|