[opensuse] Bash script help needed
How do I go through a text file and add ",on" at the end of each line? /J -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Monday 05 February 2007 10:15, Jan Karjalainen wrote:
How do I go through a text file and add ",on" at the end of each line?
perl -pi -e 's/$/,on/' [your list of files] or find . -name "*txt" | xargs perl -pi -e 's/$/,on/' -- Brian Jackson Photo Sports ~ People ~ Events http://www.BrianJacksonPhoto.com -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Jan, On Monday 05 February 2007 10:15, Jan Karjalainen wrote:
How do I go through a text file and add ",on" at the end of each line?
Sed is the tool you want to use. sed -e 's/$/,on/' textFile >textFileWithOn If you have several target files, do this: for targetFile in textFile1 textFile2; do sed -e 's/$/,on/' "$targetFile" >"${targetFile}WithOn" done If you want to effect an in-place change, you'll have to manipulate the input and / or output files separately.
/J
Randall Schulz -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Monday 05 February 2007 19:15, Jan Karjalainen wrote:
How do I go through a text file and add ",on" at the end of each line?
Does it have to be in bash? sed -e 's/\(.*\)/\1,on/g' filename or, if you want to make the changes directly in the file sed -ie 's/\(.*\)/\1,on/g' testfile -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Monday 05 February 2007 10:25, Anders Johansson wrote:
On Monday 05 February 2007 19:15, Jan Karjalainen wrote:
How do I go through a text file and add ",on" at the end of each line?
...
or, if you want to make the changes directly in the file
sed -ie 's/\(.*\)/\1,on/g' testfile
D'Oh! Well, it's good to learn that sed has an in-place editing option. It's also nice to know it can optionally make a back-up before overwriting the original: -i[SUFFIX], --in-place[=SUFFIX] edit files in place (makes backup if extension supplied) RRS -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Randall R Schulz wrote:
On Monday 05 February 2007 10:25, Anders Johansson wrote:
On Monday 05 February 2007 19:15, Jan Karjalainen wrote:
How do I go through a text file and add ",on" at the end of each line?
...
or, if you want to make the changes directly in the file
sed -ie 's/\(.*\)/\1,on/g' testfile
D'Oh!
Well, it's good to learn that sed has an in-place editing option. It's also nice to know it can optionally make a back-up before overwriting the original:
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if extension supplied)
RRS
Somehow all these examples add the ",on" on a new line after each line, like this: 208.34.234.48-208.34.234.63 ,on 208.34.235.32-208.34.235.63 ,on etc... I want them to look like this: 208.34.234.48-208.34.234.63,on 208.34.235.32-208.34.235.63,on etc... -- "In theory, there is no difference between theory and practice. But, in practice, there is." - Jan L.A. Van De Snepscheut -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Monday 05 February 2007 11:01, Jan Karjalainen wrote:
Somehow all these examples add the ",on" on a new line after each line, like this:
208.34.234.48-208.34.234.63 ,on 208.34.235.32-208.34.235.63 ,on etc...
I want them to look like this: 208.34.234.48-208.34.234.63,on 208.34.235.32-208.34.235.63,on etc...
Did your input file come from a DOS (ie windos) computer? The sed examples work just as well as the perl on I mentioned. if your file does have ^M's in it, you can strip them with this perl -pi -e 's/^M//g; s/$/,on' FILE good luck -- Brian Jackson Photo Sports ~ People ~ Events http://www.BrianJacksonPhoto.com -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Monday 05 February 2007 20:09, Brian Jackson wrote:
On Monday 05 February 2007 11:01, Jan Karjalainen wrote:
Somehow all these examples add the ",on" on a new line after each line, like this:
208.34.234.48-208.34.234.63 ,on 208.34.235.32-208.34.235.63 ,on etc...
I want them to look like this: 208.34.234.48-208.34.234.63,on 208.34.235.32-208.34.235.63,on etc...
Did your input file come from a DOS (ie windos) computer?
The sed examples work just as well as the perl on I mentioned. if your file does have ^M's in it, you can strip them with this
perl -pi -e 's/^M//g; s/$/,on' FILE
or dos2unix filename dos2unix is in the dos2unix package, which should be installed by default -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Anders Johansson wrote:
On Monday 05 February 2007 20:09, Brian Jackson wrote:
On Monday 05 February 2007 11:01, Jan Karjalainen wrote:
Somehow all these examples add the ",on" on a new line after each line, like this:
208.34.234.48-208.34.234.63 ,on 208.34.235.32-208.34.235.63 ,on etc...
I want them to look like this: 208.34.234.48-208.34.234.63,on 208.34.235.32-208.34.235.63,on etc...
Did your input file come from a DOS (ie windos) computer?
The sed examples work just as well as the perl on I mentioned. if your file does have ^M's in it, you can strip them with this
perl -pi -e 's/^M//g; s/$/,on' FILE
or
dos2unix filename
dos2unix is in the dos2unix package, which should be installed by default
Yes, it was the ^M that did it. Thanks for all the help! /J -- "In theory, there is no difference between theory and practice. But, in practice, there is." - Jan L.A. Van De Snepscheut -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
To continue on the same subject, how do I remove extra spaces from the list? 111.222.111.222 - 222.111.222.111,on should be 111.222.111.222-222.111.222.111,on Note that there should be no spaces around the "-" sign. /J -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Monday 05 February 2007 12:46, Jan Karjalainen wrote:
To continue on the same subject, how do I remove extra spaces from the list?
111.222.111.222 - 222.111.222.111,on
should be
111.222.111.222-222.111.222.111,on
Note that there should be no spaces around the "-" sign.
sed -e 's/ *- */-/' If multiple occurrences of spurious spaces surrounding hyphens may occur on a single line, use the 'g' modifier: sed -e 's/ *- */-/g' If you want all spaces everywhere expunged, do this: sed -r -e 's/ +//g' It's good to be aware the Gnu sed can do so-called extended regular expressions matching if the "-r" option is included, as I did in the last example above. Naturally, this must be used with care, since many aspects of the RE syntax changes under the extended form. The same effect can be achieved in this case without extended REs like this: sed -r -e 's/ *//g' NOTE: In this example there are _two_ spaces before the asterisk. You can combine multiple expressions in a single invocation by simply giving multiple pairs of "-e" and editing expressions. They'll be interpreted in order, in case one expression's applicability depends on the application of another.
/J
Randall Schulz -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Hello, On Mon, 05 Feb 2007, Randall R Schulz wrote:
Well, it's good to learn that sed has an in-place editing option.
$ sed -i sed: invalid option -- i [..] $ sed --version GNU sed version 3.02 That is an unportable not quite recent addition to GNU sed. Other sed implementations don't have it. Just keep that in mind, ok? -dnh -- "[the block drivers and the buffer cache] aren't incestuous, they're just living in sin.. " -- Linus Torvalds -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
David, On Tuesday 06 February 2007 11:19, David Haller wrote:
Hello,
On Mon, 05 Feb 2007, Randall R Schulz wrote:
Well, it's good to learn that sed has an in-place editing option.
$ sed -i sed: invalid option -- i [..] $ sed --version GNU sed version 3.02
That is an unportable not quite recent addition to GNU sed. Other sed implementations don't have it. Just keep that in mind, ok?
% sed --version GNU sed version 4.1.4 Copyright (C) 2003 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, to the extent permitted by law. It's true that older and non-Gnu sed implementations do not have this option, but unless you're going to restrict yourself to POSIX-compliant subsets of all commands you use in scripting, you're really committing yourself to the Gnu implementations. These implementations are riddled with very significant extensions and improvements, many of which I'm not willing to forgo, knowing as I do that my scripts will always be running in a Gnu / Linux environment.
-dnh
Randall Schulz -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
participants (5)
-
Anders Johansson
-
Brian Jackson
-
David Haller
-
Jan Karjalainen
-
Randall R Schulz