[opensuse] sed - howto strip from character to end of line?
Listmates, Every time I run into sed, it's a fight. I'm trying to locate a " and then strip the quote and everything that follows from each line in a file called "myfile". I am obviously horribly confused with the ranges escaped parentheses and \1 remembered patterns. Surely sed can do this, but the trees are obscuring the forest. The lines I have in myfile are all like: /home/icons-2/filename.gif">capital seal 1</A> The problem I am having is that the " is in the middle of a 'word' so something like cat ~/tmp/myfile | sed 's/"\([0-9A-Za-z]*\)*/\1/' doesn't work. Something as simple as cat ~/tmp/myfile | sed 's/"//' does find the " and delete it, but how do I get rid of the rest of the line? Help! -- David C. Rankin, J.D., P.E. Rankin Law Firm, PLLC 510 Ochiltree Street Nacogdoches, Texas 75961 Telephone: (936) 715-9333 Facsimile: (936) 715-9339 www.rankinlawfirm.com -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Friday 15 February 2008 08:51, David C. Rankin wrote:
Listmates,
Every time I run into sed, it's a fight. I'm trying to locate a " and then strip the quote and everything that follows from each line in a file called "myfile". I am obviously horribly confused with the ranges escaped parentheses and \1 remembered patterns. Surely sed can do this, but the trees are obscuring the forest. The lines I have in myfile are all like:
/home/icons-2/filename.gif">capital seal 1</A>
The problem I am having is that the " is in the middle of a 'word' so something like cat ~/tmp/myfile | sed 's/"\([0-9A-Za-z]*\)*/\1/' doesn't work. Something as simple as cat ~/tmp/myfile | sed 's/"//' does find the " and delete it, but how do I get rid of the rest of the line?
Sed is an odd duck, though mostly only in its more esoteric options and operations. This one's pretty simple: sed -e 's/".*//' ~/tmp/myfile >resultFile Please break yourself of the habit of doing "cat someFile |someCommand" Very, very few Unix command do not take file names as specifications of the input on which to operate.
Help!
Calm down.
-- David C. Rankin
Randall Schulz -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Friday 15 February 2008 09:22, Per Jessen wrote:
Randall R Schulz wrote:
Please break yourself of the habit of doing "cat someFile |someCommand" Very, very few Unix command do not take file names as specifications of the input on which to operate.
"cat someFile |someCommand" is best replaced by:
"somecommand
Not really. The best is to let the program open the file. Then it knows that file's name, for one thing, and can include it in any diagnostics. As I said, only a very few commands don't take one or more file names as input specifications.
/Per Jessen, Zürich
Randall Schulz -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Randall R Schulz wrote:
On Friday 15 February 2008 09:22, Per Jessen wrote:
Randall R Schulz wrote:
Please break yourself of the habit of doing "cat someFile |someCommand" Very, very few Unix command do not take file names as specifications of the input on which to operate. "cat someFile |someCommand" is best replaced by:
"somecommand
Not really. The best is to let the program open the file. Then it knows that file's name, for one thing, and can include it in any diagnostics.
In the input redirection case, it's known as "stdin"
As I said, only a very few commands don't take one or more file names as input specifications.
-- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Friday 15 February 2008 22:06, Aaron Kulkis wrote:
Randall R Schulz wrote:
On Friday 15 February 2008 09:22, Per Jessen wrote:
Randall R Schulz wrote:
Please break yourself of the habit of doing "cat someFile
|someCommand" Very, very few Unix command do not take file names | as
specifications of the input on which to operate.
"cat someFile |someCommand" is best replaced by:
"somecommand
Not really. The best is to let the program open the file. Then it knows that file's name, for one thing, and can include it in any diagnostics.
In the input redirection case, it's known as "stdin"
Which is neither here nor there. If you use the shell's redirection syntax, the _shell_ opens the file, not the program. Because the program did not open the file, it does not know its name and cannot issue that name in a diagnostic. Randall Schulz -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Randall R Schulz wrote:
On Friday 15 February 2008 22:06, Aaron Kulkis wrote:
Randall R Schulz wrote:
On Friday 15 February 2008 09:22, Per Jessen wrote:
Randall R Schulz wrote:
Please break yourself of the habit of doing "cat someFile
|someCommand" Very, very few Unix command do not take file names | as
specifications of the input on which to operate. "cat someFile |someCommand" is best replaced by:
"somecommand
Which is neither here nor there. If you use the shell's redirection syntax, the _shell_ opens the file, not the program. Because the program did not open the file, it does not know its name and cannot issue that name in a diagnostic.
It issues the name "stdin" -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Sunday 17 February 2008 19:58, Aaron Kulkis wrote:
Randall R Schulz wrote:
...
Which is neither here nor there. If you use the shell's redirection syntax, the _shell_ opens the file, not the program. Because the program did not open the file, it does not know its name and cannot issue that name in a diagnostic.
It issues the name "stdin"
Which is not the name of the file. Oy. RRS -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Randall R Schulz wrote:
Please break yourself of the habit of doing "cat someFile |someCommand" Very, very few Unix command do not take file names as specifications of the input on which to operate.
Help!
Calm down.
It was a 3-cup morning.. The caffeine made me do it. Thanks Randall. -- David C. Rankin, J.D., P.E. Rankin Law Firm, PLLC 510 Ochiltree Street Nacogdoches, Texas 75961 Telephone: (936) 715-9333 Facsimile: (936) 715-9339 www.rankinlawfirm.com -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Randall R Schulz wrote:
On Friday 15 February 2008 08:51, David C. Rankin wrote:
Listmates,
Every time I run into sed, it's a fight. I'm trying to locate a " and then strip the quote and everything that follows from each line in a file called "myfile". I am obviously horribly confused with the ranges escaped parentheses and \1 remembered patterns. Surely sed can do this, but the trees are obscuring the forest. The lines I have in myfile are all like:
/home/icons-2/filename.gif">capital seal 1</A>
The problem I am having is that the " is in the middle of a 'word' so something like cat ~/tmp/myfile | sed 's/"\([0-9A-Za-z]*\)*/\1/' doesn't work. Something as simple as cat ~/tmp/myfile | sed 's/"//' does find the " and delete it, but how do I get rid of the rest of the line?
Sed is an odd duck, though mostly only in its more esoteric options and operations. This one's pretty simple:
sed -e 's/".*//' ~/tmp/myfile >resultFile
Hah..you came up with the same matching string I did!
Please break yourself of the habit of doing "cat someFile |someCommand"
Yes..it's called "gratuitous use of cat" as in cat infile | cat | cat | cat | somecommand which of course, can be done as $ somecommand < infile (any file which takes input from a pipe will ALSO take that same input from a '<' file-input redirection) or even (if $ somecommand < infile isn't "readable" enough): $ < infile command (Yes, that really DOES work!.. if you don't believe me, TRY IT!) <
Very, very few Unix command do not take file names as specifications of the input on which to operate.
And if they don't, file redirection always works. the only time cat | some_command is needed is $ cat file1 file2 file3 .... | some command OR $ cat -someflag file | command for example $ cat -n file | command so that every input line is preceded by a line number or $ cat -s file | command to reduce \n\n.... to a single \n
Help!
Calm down.
Heh! -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Friday 15 February 2008 17:51:47 David C. Rankin wrote:
Listmates,
Every time I run into sed, it's a fight. I'm trying to locate a " and then strip the quote and everything that follows from each line in a file called "myfile". I am obviously horribly confused with the ranges escaped parentheses and \1 remembered patterns. Surely sed can do this, but the trees are obscuring the forest. The lines I have in myfile are all like:
/home/icons-2/filename.gif">capital seal 1</A>
The problem I am having is that the " is in the middle of a 'word' so something like cat ~/tmp/myfile | sed 's/"\([0-9A-Za-z]*\)*/\1/' doesn't work.
yes it does, it's doing exactly what you tell it to do. It matches a " character and everything that follows, and then you explicitly tell it to put back the characters that follow with the \1 Just stop telling sed to keep that which follows the ", by removing the \1 Anders -- Madness takes its toll -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
David C. Rankin wrote:
Listmates,
Every time I run into sed, it's a fight. I'm trying to locate a " and then strip the quote and everything that follows from each line in a file called "myfile". I am obviously horribly confused with the ranges escaped parentheses and \1 remembered patterns. Surely sed can do this, but the trees are obscuring the forest. The lines I have in myfile are all like:
/home/icons-2/filename.gif">capital seal 1</A>
The problem I am having is that the " is in the middle of a 'word' so something like cat ~/tmp/myfile | sed 's/"\([0-9A-Za-z]*\)*/\1/' doesn't work. Something as simple as cat ~/tmp/myfile | sed 's/"//' does
have you tried: sed 's/".*//' " is literal . wildcard to match any single character * match preceding pattern any number of times. Interpreted, ".* means match double-quote (") followed by any number (*) of any character(s)(.) Note...this might fail if " is the last character on the line.
find the " and delete it, but how do I get rid of the rest of the line?
Help!
-- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
David C. Rankin schreef:
Listmates,
Every time I run into sed, it's a fight. I'm trying to locate a " and then strip the quote and everything that follows from each line in a file called "myfile". I am obviously horribly confused with the ranges escaped parentheses and \1 remembered patterns. Surely sed can do this, but the trees are obscuring the forest. The lines I have in myfile are all like:
/home/icons-2/filename.gif">capital seal 1</A>
The problem I am having is that the " is in the middle of a 'word' so something like cat ~/tmp/myfile | sed 's/"\([0-9A-Za-z]*\)*/\1/' doesn't work. Something as simple as cat ~/tmp/myfile | sed 's/"//' does find the " and delete it, but how do I get rid of the rest of the line?
Randall already showed how to solve this, so what remains is to explain why your regular expression does not match. What you really want, is to delete the pattern DOUBLE_QUOTE_AND_ALL_CHARACTERS_TO_END_OF_LINE (".*$) but that is not what you asked sed to do. You asked to look for a pattern DOUBLE_QUOTE_FOLLOWED_BY_ANY_STRING_OF_STRINGS_CONTAINING_ONLY_DIGITS_OR_LETTERS which it did not find, because the string also contains <, /, > and spaces and the first character after the double quote is >. Furthermore, as Anders explained, you would not delete that string if you found one, but only the double quote. For the extra point: explain why in the regular expression \([CHARSET]*\)* the second * can be omitted because it does nothing. I find it always helpful to interpret regular expressions in the way shown above. Regards, -- Jos van Kan registered Linux user #152704 -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
participants (6)
-
Aaron Kulkis
-
Anders Johansson
-
David C. Rankin
-
Jos van Kan
-
Per Jessen
-
Randall R Schulz