Re: [CentOS] sed question
On 08/25/2015 11:21 AM, Alice Wonder wrote: cat file.txt |\ sed -e s?"foo"?"bar"?g |\ sed -e s?"dirty"?"clean?" |\ > file2.txt I don't understand why you'd quote that way. Though unlikely, you could potentially match a filename in the working directory, and hose the sed command. For efficiency's sake, you can eliminate cat and one of the two sed processes, and still have a more readable command: sed -e 's?foo?bar?g' \ -e 's?dirty?clean?' \ < file.txt > file2.txt ___ CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
Re: [CentOS] sed question
On 08/25/2015 11:02 AM, Gordon Messmer wrote: Additionally, you can avoid using "cat" to make the script more efficient. You'll start fewer processes, and complete more quickly. cat is almost never needed unless you actually need to con"cat"enate multiple files. I sometimes like to use cat purely for stylistic reasons : cat file.txt |\ sed -e s?"foo"?"bar"?g |\ sed -e s?"dirty"?"clean?" |\ > file2.txt It lets me line up my sed expressions. But yes, that is wasting a process for easier visualization. Worth it to me. ___ CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
Re: [CentOS] sed question
On 08/25/2015 10:50 AM, Jerry Geis wrote: cat my_file.txt | sed 's/CANCELID/$CHANGE/' > cancel.txt sed doesn't perform environment variable expansion. That is to say that when you instruct sed to substitute "$CHANGE" for "CANCELID", "$CHANGE" is a literal string that will be substituted. bash, on the other hand, does perform environment variable expansion for strings not enclosed in single quotes. So, you probably meant: cat my_file.txt | sed "s/CANCELID/$CHANGE/" > cancel.txt In that case, bash will replace $CHANGE with 1234 before starting sed with that argument. Additionally, you can avoid using "cat" to make the script more efficient. You'll start fewer processes, and complete more quickly. cat is almost never needed unless you actually need to con"cat"enate multiple files. A couple of other changes I'd suggest as better scripting style: Enclose your variable names in ${} to avoid abiguity, and use lower case variable names except when you have a variable that you mean to export, and upper case only exported variables. sed "s/CANCELID/${change}/" < my_file.txt > cancel.txt ___ CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
Re: [CentOS] sed question
On 8/25/2015 10:50 AM, Jerry Geis wrote: --- This is the two line script CHANGE="1234" cat my_file.txt | sed 's/CANCELID/$CHANGE/' > cancel.txt --- and the my_file.txt has: CANCELID it gets changed to $CHANGE instead of the actual value 1234 . I tried putting a \ in front of the $ also and made no difference. use " instead of '. inside ', a $ is just a literal. inside ", $NAME gets substituted. -- john r pierce, recycling bits in santa cruz ___ CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
Re: [CentOS] sed question
- Original Message - | I am trying to use sed to change a value in a pipe. | | --- This is the two line script | CHANGE="1234" | | cat my_file.txt | sed 's/CANCELID/$CHANGE/' > cancel.txt | --- | | and the my_file.txt has: | CANCELID | | it gets changed to $CHANGE instead of the actual value 1234 . | I tried putting a \ in front of the $ also and made no difference. | | What am I not doing correctly. | | Thanks, | | jerry Single quotes = literal. Double quotes = interpreted ;) -- James A. Peltier IT Services - Research Computing Group Simon Fraser University - Burnaby Campus Phone : 604-365-6432 Fax : 778-782-3045 E-Mail : jpelt...@sfu.ca Website : http://www.sfu.ca/itservices Twitter : @sfu_rcg Powering Engagement Through Technology ___ CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
Re: [CentOS] sed question
On Tue, Aug 25, 2015 at 1:50 PM, Jerry Geis wrote: > I am trying to use sed to change a value in a pipe. > > --- This is the two line script > CHANGE="1234" > > cat my_file.txt | sed 's/CANCELID/$CHANGE/' > cancel.txt > --- > > and the my_file.txt has: > CANCELID > > it gets changed to $CHANGE instead of the actual value 1234 . > I tried putting a \ in front of the $ also and made no difference. > > What am I not doing correctly. You need to use double quotes: cat my_file.txt | sed "s/CANCELID/$CHANGE/" > cancel.txt ___ CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
Re: [CentOS] sed with several lines, how?
Hahahaha, I see that you posted this in quite a few places. Let me repeat it here then. BTW, do a bit of homework if you do need fine tuning before posting back on this list. awk 'BEGIN {sawpattern=0} "^[[:alpha:]], ^[[:alpha:]]" {if (($0 ~/[[:alpha:]]/ )&& (sawpattern == 0)) {sawpattern=1} else {sawpattern=0;print $0}}' testfile.txt I don't know how your files are exactly formatted. Please look up the documentation if you need to further finetune. man awk man 7 regex On Saturday, November 27, 2010 09:03 PM, Arthur Bela wrote: > thank you, and sorry, if i had formulated wrong, but the "SOMETEXT#X" > is a random STRING, like: > > $ cat testfile.txt > alsjflsajfkljasdf > asfklasjlkyxcvo > kldfjlkasjdfasdf > kasfjxcvklajdflas > > yxcvkjasafjads > asdfjkldjlasj > > uiyxzckjhasfsd > $ > > > > $ awk 'BEGIN {sawpattern=0} "^SOMETEXT, ^SOMETEXT" {if (($0 > ~/SOMETEXT/ )&& (sawpattern == 0)) {sawpattern=1} > else {sawpattern=0;print $0}}' testfile.txt > alsjflsajfkljasdf > asfklasjlkyxcvo > kldfjlkasjdfasdf > kasfjxcvklajdflas > > yxcvkjasafjads > asdfjkldjlasj > > uiyxzckjhasfsd > $ > > On 27 November 2010 12:39, Arthur Bela wrote: >> hyphen's [ - ] are just for marking the start/end of a pattern, but >> there are _not in_ the pattern! >> "OUTPUT" is what i want after "seding" the PATTERN#X's >> >> >> so i for e.g.: need the first, and second "magic" >> sed "FIRSTMAGIC" PATTERN#1 >> sed "SECONDMAGIC" PATTERN#2 >> >> >> >> >> PATTERN#1: >> >> --- >> SOMETEXT#1 >> SOMETEXT#2 >> SOMETEXT#3 >> SOMETEXT#4 >> --- >> >> "exact pattern", what is unique from the pattern: >> --- >> \n >> --- >> >> OUTPUT: >> --- >> SOMETEXT#1 >> SOMETEXT#3 >> SOMETEXT#4 >> --- >> >> >> >> >> >> and: >> >> PATTERN#2: >> >> --- >> SOMETEXT#1 >> SOMETEXT#2 >> >> SOMETEXT#3 >> --- >> >> "exact pattern", what is unique from the pattern: >> --- >> \n >> --- >> >> OUTPUT: >> --- >> SOMETEXT#1 >> >> SOMETEXT#3 >> --- >> >> >> i just can't figure it out, how to "sed" when having several lines >> [nor in awk, perl..] >> >> could anyone post a link, or some guide, how to write these two lines of >> "sed"? >> >> Thank You! :\ >> > ___ > CentOS mailing list > CentOS@centos.org > http://lists.centos.org/mailman/listinfo/centos ___ CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
Re: [CentOS] sed with several lines, how?
thank you, and sorry, if i had formulated wrong, but the "SOMETEXT#X" is a random STRING, like: $ cat testfile.txt alsjflsajfkljasdf asfklasjlkyxcvo kldfjlkasjdfasdf kasfjxcvklajdflas yxcvkjasafjads asdfjkldjlasj uiyxzckjhasfsd $ $ awk 'BEGIN {sawpattern=0} "^SOMETEXT, ^SOMETEXT" {if (($0 ~/SOMETEXT/ ) && (sawpattern == 0)) {sawpattern=1} else {sawpattern=0;print $0}}' testfile.txt alsjflsajfkljasdf asfklasjlkyxcvo kldfjlkasjdfasdf kasfjxcvklajdflas yxcvkjasafjads asdfjkldjlasj uiyxzckjhasfsd $ On 27 November 2010 12:39, Arthur Bela wrote: > hyphen's [ - ] are just for marking the start/end of a pattern, but > there are _not in_ the pattern! > "OUTPUT" is what i want after "seding" the PATTERN#X's > > > so i for e.g.: need the first, and second "magic" > sed "FIRSTMAGIC" PATTERN#1 > sed "SECONDMAGIC" PATTERN#2 > > > > > PATTERN#1: > > --- > SOMETEXT#1 > SOMETEXT#2 > SOMETEXT#3 > SOMETEXT#4 > --- > > "exact pattern", what is unique from the pattern: > --- > \n > --- > > OUTPUT: > --- > SOMETEXT#1 > SOMETEXT#3 > SOMETEXT#4 > --- > > > > > > and: > > PATTERN#2: > > --- > SOMETEXT#1 > SOMETEXT#2 > > SOMETEXT#3 > --- > > "exact pattern", what is unique from the pattern: > --- > \n > --- > > OUTPUT: > --- > SOMETEXT#1 > > SOMETEXT#3 > --- > > > i just can't figure it out, how to "sed" when having several lines > [nor in awk, perl..] > > could anyone post a link, or some guide, how to write these two lines of > "sed"? > > Thank You! :\ > ___ CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
Re: [CentOS] sed a particular line in file
> % cat foo > Hello line 1 > Hello line 2 > Hello line 3 > > # To change just line 2 > % sed '2s/Hello/There/' > Hello line 1 > There line 2 > Hello line 3 > > # To change line 2 and onwards > % sed '2,$s/Hello/There/' > Hello line 1 > There line 2 > There line 3 > > It's that simple :-) > > yes indeed - simples ___ CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
Re: [CentOS] sed a particular line in file
> I thought i could use sed to change a particular line number but i dont see > that in the man page, i am trying to change a value from line number 6 % cat foo Hello line 1 Hello line 2 Hello line 3 # To change just line 2 % sed '2s/Hello/There/' Hello line 1 There line 2 Hello line 3 # To change line 2 and onwards % sed '2,$s/Hello/There/' Hello line 1 There line 2 There line 3 It's that simple :-) -- rgds Stephen ___ CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
Re: [CentOS] sed help
> -Original Message- > From: centos-boun...@centos.org [mailto:centos-boun...@centos.org] On > Behalf Of Paul Heinlein > Sent: Tuesday, March 09, 2010 11:08 AM > To: CentOS mailing list > Subject: Re: [CentOS] sed help > > On Tue, 9 Mar 2010, chloe K wrote: > > > Hi > > > > Can I know how to use sed to substitue 2 instead of 1 at the same time? > > > > eg: > > > > sed 's/pchloe.com/abc.com/ ; /192.92.123.5/10.10.0.3/g' orgfile >> > newfile > > sed \ >-e 's/pchloe\.com/abc.com/g' \ >-e 's/192\.92\.123\.5/10.10.0.3/g' \ >orgfile >> newfile > > -- > Paul Heinlein <> heinl...@madboa.com <> http://www.madboa.com/ > ___ > CentOS mailing list > CentOS@centos.org > http://lists.centos.org/mailman/listinfo/centos You can also use semi colons for example: sed 's/pchloe.com/abc.com/; s/192.92.123.5/10.10.0.3/g' orgfile >> newfile Dan ___ CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
Re: [CentOS] sed help
On Tue, 9 Mar 2010, chloe K wrote: > Hi > > Can I know how to use sed to substitue 2 instead of 1 at the same time? > > eg: > > sed 's/pchloe.com/abc.com/ ; /192.92.123.5/10.10.0.3/g' orgfile >> newfile sed \ -e 's/pchloe\.com/abc.com/g' \ -e 's/192\.92\.123\.5/10.10.0.3/g' \ orgfile >> newfile -- Paul Heinlein <> heinl...@madboa.com <> http://www.madboa.com/ ___ CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
Re: [CentOS] sed (or other) magic to get RPM base names ?
Am Montag, den 21.09.2009, 15:06 +0200 schrieb Alan McKay: > Hey folks, > > Once upon a time I saw some sed magic to take the output of "rpm -qa" > and strip away all the version info to give just the RPM base names. > > And of course I forgot to note it :-/ And have not been able to > replicate it myself. > > e.g. from this : > > avahi-0.6.16-1.el5 > avahi-glib-0.6.16-1.el5 > > produce this : > > avahi > avahi-glib > > thanks, > -Alan No need fro sed here use rpms --querformat switch, see manpage of rpm for details. Chris financial.com AG Munich head office/Hauptsitz München: Maria-Probst-Str. 19 | 80939 München | Germany Frankfurt branch office/Niederlassung Frankfurt: Messeturm | Friedrich-Ebert-Anlage 49 | 60327 Frankfurt | Germany Management board/Vorstand: Dr. Steffen Boehnert (CEO/Vorsitzender) | Dr. Alexis Eisenhofer | Dr. Yann Samson | Matthias Wiederwach Supervisory board/Aufsichtsrat: Dr. Dr. Ernst zur Linden (chairman/Vorsitzender) Register court/Handelsregister: Munich – HRB 128 972 | Sales tax ID number/St.Nr.: DE205 370 553 ___ CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
Re: [CentOS] sed (or other) magic to get RPM base names ?
On Mon, Sep 21, 2009 at 3:06 PM, Alan McKay wrote: > avahi-0.6.16-1.el5 > avahi-glib-0.6.16-1.el5 > > produce this : > > avahi > avahi-glib r...@knodd:~# rpm -qa --queryformat "%{name}\n" avahi\* avahi avahi-compat-libdns_sd avahi-glib r...@knodd:~# Ralph ___ CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
Re: [CentOS] sed append question
Hi, On Wed, Aug 19, 2009 at 16:20, Joseph L. Casale wrote: > '/string/a \\tstuff\t\t\tmorestuff' != "/string/a \\tstuff\t\t\tmorestuff" Yes, indeed... The rules of quoting and backslashes in the shell are not very uniform and can get quite tricky... Also, the \t is interpreted by sed, and AFAIK it is available in GNU sed only, so the syntax you are using above might not be very portable to other versions of "sed". If your script gets this tricky it's probably time to move to Perl, which can also do that easily in an one-liner and, in this case, might actually be more portable. (Heck, it might even be more readable!) HTH, Filipe ___ CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
Re: [CentOS] sed append question
>The "a" command expects to be followed by a "\", so it's eating the >one in your first "\t". If you add another "\" it seems to work as you >want it to: > >$ echo string | sed '/string/a \\tstuff\t\t\tmorestuff' >string >stuff morestuff >$ Ah ffs, lol... It would also help if I emailed what I was typing. I use so many versions, I get mixed up. I typed the email with <'> but wrote <"> in the console which is why I couldn't make it work. '/string/a \\tstuff\t\t\tmorestuff' != "/string/a \\tstuff\t\t\tmorestuff" Thanks Filipe! jlc ___ CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
Re: [CentOS] sed append question
Hi, On Wed, Aug 19, 2009 at 13:24, Joseph L. Casale wrote: > Hey guys, > I am trying to make sed append every line containing a string with another > line. > problem is the appended line needs to start with a tab: > # sed -i '/string/a \tstuff\t\t\tmorestuff' file > Obviously \t or \x09 etc doesn't get interpreted unless there are other > characters > before it? How can I get this to begin with a ? Hi, The "a" command expects to be followed by a "\", so it's eating the one in your first "\t". If you add another "\" it seems to work as you want it to: $ echo string | sed '/string/a \\tstuff\t\t\tmorestuff' string stuff morestuff $ HTH, Filipe ___ CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
Re: [CentOS] sed
Stephen Harris wrote: On Fri, May 23, 2008 at 06:59:24PM +0200, Thomas Johansson wrote: Stephen Harris wrote: sed 's/^\([^]*[ ]*[^]*\)\([ ]*.*\)$/\1.contoso.com\2/' (where there's a space *and* a TAB inside each of the [ ] ) The above version easier to read and "copy paste". Space is space and tabe is \t sed 's/^\([^ \t]*[ \t]*[^ \t]*\)\([ \t]*.*\)$/\1.contoso.com\2/' I grew up with versions of 'sed' that don't understand this new-fangled method of specifying tabs, and write enough cross-platform code that I can't rely on it (still doesn't work in Solaris 10, for example). perl can do anything sed can do and has almost no platform or version related syntax differences - plus it has \s to represent 'whitespace' and you don't have to bang your head on the wall when you are half done and realize you have to do something spanning multiple lines. -- Les Mikesell [EMAIL PROTECTED] ___ CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
Re: [CentOS] sed
On Fri, May 23, 2008 at 8:50 AM, Matt Shields <[EMAIL PROTECTED]> wrote: > > I'd use awk. Put the lines in a file, then do this > > cat test.txt | awk '{ print $1 "\t" $2 ".centos.com\t" $3 "\t" $4 }' > Or just awk '{ print $1 "\t" $2 ".centos.com\t" $3 "\t" $4 }' test.txt > newhostsfile (The cat just complicates things, as with most cats :-) mhr ___ CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
Re: [CentOS] sed
On Fri, May 23, 2008 at 06:59:24PM +0200, Thomas Johansson wrote: > Stephen Harris wrote: > > sed 's/^\([^]*[ ]*[^]*\)\([ ]*.*\)$/\1.contoso.com\2/' > > > >(where there's a space *and* a TAB inside each of the [ ] ) > > > The above version easier to read and "copy paste". Space is space and tabe > is \t > > sed 's/^\([^ \t]*[ \t]*[^ \t]*\)\([ \t]*.*\)$/\1.contoso.com\2/' I grew up with versions of 'sed' that don't understand this new-fangled method of specifying tabs, and write enough cross-platform code that I can't rely on it (still doesn't work in Solaris 10, for example). -- rgds Stephen ___ CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
Re: [CentOS] sed
Stephen Harris wrote: On Fri, May 23, 2008 at 06:02:29PM +0200, Mihai T. Lazarescu wrote: On Fri, May 23, 2008 at 08:41:19AM -0700, Scott McClanahan wrote: 1.1.1.1foo 10.10.10.10bar bar2 100.100.100.100foobar foobar2 foobar3 == After == 1.1.1.1foo.contoso.com 10.10.10.10bar.contoso.com bar2 100.100.100.100foobar.contoso.com foobar2 foobar3 Any advice on how to pull this off? Thanks. sed 's/ /.contoso.com ' That works because we've explicity been told so the first space is a good place to switch. More generally, if there's possibly multiple spaces or tabs in multiple places this might work: sed 's/^\([^]*[ ]*[^]*\)\([ ]*.*\)$/\1.contoso.com\2/' (where there's a space *and* a TAB inside each of the [ ] ) The above version easier to read and "copy paste". Space is space and tabe is \t sed 's/^\([^ \t]*[ \t]*[^ \t]*\)\([ \t]*.*\)$/\1.contoso.com\2/' ___ CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
Re: [CentOS] sed
On Fri, May 23, 2008 at 06:02:29PM +0200, Mihai T. Lazarescu wrote: > On Fri, May 23, 2008 at 08:41:19AM -0700, Scott McClanahan wrote: > > 1.1.1.1foo > > 10.10.10.10bar bar2 > > 100.100.100.100foobar foobar2 foobar3 > > == After == > > 1.1.1.1foo.contoso.com > > 10.10.10.10bar.contoso.com bar2 > > 100.100.100.100foobar.contoso.com foobar2 foobar3 > > > > Any advice on how to pull this off? Thanks. > > sed 's/ /.contoso.com ' That works because we've explicity been told so the first space is a good place to switch. More generally, if there's possibly multiple spaces or tabs in multiple places this might work: sed 's/^\([^]*[ ]*[^]*\)\([ ]*.*\)$/\1.contoso.com\2/' (where there's a space *and* a TAB inside each of the [ ] ) -- rgds Stephen ___ CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
Re: [CentOS] sed
On Fri, May 23, 2008 at 08:41:19AM -0700, Scott McClanahan wrote: > Not specific to CentOS but I know you guys would be really helpful anyhow. > Basically, I have a file which has been editted in the past very similarly to > the hosts file only now I want to use it as a hosts file and need to run some > fancy sed to massage the data into shape. Currently, the data in the file is > in the form of alias>. In some cases there may not be any aliases so the end of line would > be > right after the short hostname (no space at the end either). In other cases > there could be many space separated short hostname aliases. What I have been > trying to do without success is add our domain name to the first string after > the ip address and tab character. As an example, > > == Before == > > 1.1.1.1foo > 10.10.10.10bar bar2 > 100.100.100.100foobar foobar2 foobar3 > > > == After == > > 1.1.1.1foo.contoso.com > 10.10.10.10bar.contoso.com bar2 > 100.100.100.100foobar.contoso.com foobar2 foobar3 > > Any advice on how to pull this off? Thanks. sed 's/ /.contoso.com ' Cheers, Mihai ___ CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
Re: [CentOS] sed
On Fri, May 23, 2008 at 11:41 AM, Scott McClanahan <[EMAIL PROTECTED]> wrote: > Not specific to CentOS but I know you guys would be really helpful anyhow. > Basically, I have a file which has been editted in the past very similarly > to the hosts file only now I want to use it as a hosts file and need to run > some fancy sed to massage the data into shape. Currently, the data in the > file is in the form of hostname alias>. In some cases there may not be any aliases so the end of > line would be right after the short hostname (no space at the end either). > In other cases there could be many space separated short hostname aliases. > What I have been trying to do without success is add our domain name to the > first string after the ip address and tab character. As an example, > > == Before == > > 1.1.1.1foo > 10.10.10.10bar bar2 > 100.100.100.100foobar foobar2 foobar3 > > > == After == > > 1.1.1.1foo.contoso.com > 10.10.10.10bar.contoso.com bar2 > 100.100.100.100foobar.contoso.com foobar2 foobar3 > > Any advice on how to pull this off? Thanks. I'd use awk. Put the lines in a file, then do this cat test.txt | awk '{ print $1 "\t" $2 ".centos.com\t" $3 "\t" $4 }' -- -matt ___ CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos