LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   sed in bash vs tcsh, quotes, regular expressions (https://www.linuxquestions.org/questions/programming-9/sed-in-bash-vs-tcsh-quotes-regular-expressions-911784/)

will.flanagan 11-04-2011 12:48 AM

sed in bash vs tcsh, quotes, regular expressions
 
Hi all,

I have been reading tutorials on quotes, regular expressions, and the sed command, but I have been stuck on this for some time.

I have a text file named tmp.txt that has these contents:
abc
blah
efg

Thus I can use the sed command to edit the second line:

>>sed '2s/blah/the/' < tmp.txt
abc
the
efg

Now I want to change the text file accordingly. Here is my problem. The following command works in tcsh, but not bash. Why doesn't this work in bash?

>sed -i '2s/blah/the/' tmp.txt

What syntax can I use to make this work in bash? (Again, it works fine in tcsh.)

My error in bash shell is:

>sed -i '2s/blah/the/' tmp.txt
sed: 1: "tmp.txt": undefined label 'mp.txt'

Any help would be greatly appreciated!!!
Will

grail 11-04-2011 03:17 AM

Personally I would question if you have typed in exactly what you are showing? The single quotes would prevent either shell from interpreting
anything inside the quotes and using your own test data on my machine, which uses bash shell by default, I have no issue.

The fact that sed is erroring that a label is undefined is what implied to me that you have not typed something correctly.

crts 11-04-2011 06:24 PM

Quote:

Originally Posted by will.flanagan (Post 4515445)
My error in bash shell is:

>sed -i '2s/blah/the/' tmp.txt
sed: 1: "tmp.txt": undefined label 'mp.txt'

I agree with grail. You probably made some typos while typing it into bash. Please post the versions of 'sed' and 'bash' that you are using. I can reproduce the error if I type in the following:
Code:

$ sed '2s/blah/the/;'tmp.txt
sed: can't find label for jump to `mp.txt'

Notice, that I have to terminate the 's' command with a semi-colon and I have to omit the space between the closing single quote and the file name.
This way sed does interpret the first character of the following file name as a command. It happens to start with 't' (tmp.txt). As it happens, this is the command for a conditional jump. The rest of the filename (mp.txt) is then interpreted as the name of the jump point. Since it has not been defined it results in the displayed error.

will.flanagan 11-04-2011 10:16 PM

Mac Terminal issue???
 
Thanks for the help crts and grail.

For some reason I have this problem only on my mac laptop (using their Terminal 2.2.1 application)

I re-tested on a linux machine and that line worked for BOTH bash and tcsh...

I am still baffled that this doesn't work on mac. I appreciate the help.

Will

grizzeledtop 11-05-2011 02:35 AM

[08:28] ~$ cat > file
bleh
blah
bloo

[08:29] ~$ sed -i '2s/blah/grr/' file ; cat file
bleh
grr
bloo

This works fine here: "GNU bash, version 4.2.10(2)-release (i686-pc-linux-gnu)"
my environment is as posted below; diff yours from mine and experiment, please poste
if you find the culprit.

[08:30] ~$ env
GREP_COLOR=1;32
XDG_DATA_HOME=/home/marmag/.local/share
TERM=rxvt-unicode-256color
SHELL=/bin/bash
XDG_SESSION_COOKIE=6b412c6e65baf68199132e9a0000025e-1320206929.937174-959188409
WINDOWID=10485835
HUSHLOGIN=FALSE
USER=marmag
LS_COLORS=di=93:fi=0:ln=37:pi=4:so=4:bd=4:cd=4:or=31:mi=0:ex=37:*.rpm=90
GREP_COLORS=ms=01;32:mc=01;31:sl=:cx=:fn=01;30:ln=32:bn=32:se=36
MOZ_PLUGIN_PATH=/usr/lib/mozilla/plugins
XDG_CONFIG_DIRS=/etc/xdg
PATH=/home/marmag/bin:/home/marmag/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/bin/core_perl
MAIL=/var/spool/mail/marmag
HG=/usr/bin/hg
PWD=/home/marmag
JAVA_HOME=/usr/lib/jvm/java-6-openjdk
EDITOR=vim
LANG=en_US.UTF-8
COLORFGBG=default;default
HOME=/home/marmag
SHLVL=4
TERMINFO=/usr/share/terminfo
XDG_CONFIG_HOME=/home/marmag/.config
GREP_OPTIONS=--color=always
XDG_CACHE_HOME=/home/marmag/.cache
LOGNAME=marmag
J2SDKDIR=/usr/lib/jvm/java-6-openjdk
XDG_DATA_DIRS=/usr/share/:/usr/local/share/
WINDOWPATH=7
DISPLAY=:0
J2REDIR=/usr/lib/jvm/java-6-openjdk/jre
G_BROKEN_FILENAMES=1
COLORTERM=rxvt-xpm
XAUTHORITY=/home/marmag/.Xauthority
_=/usr/bin/env

good luck, Martin

will.flanagan 11-06-2011 05:46 PM

Hi Martin,

It looks like my version of sed requires that I specify a backup, even if it is null. It turns out that this works for me:

sed -i '' '2s/blah/the/' tmp.txt

Below is the result of env.

Thanks,
Will

MANPATH=/Users/willhf/Work/root/man:/usr/share/man:/usr/X11/man
TERM_PROGRAM=Apple_Terminal
TERM=xterm-256color
SHELL=/bin/bash
CLICOLOR=1
TMPDIR=/var/folders/dw/td7n0sv13213y_kv9h6ygjgm0000gn/T/
Apple_PubSub_Socket_Render=/tmp/launch-HdQz0e/Render
TERM_PROGRAM_VERSION=299
TERM_SESSION_ID=9BF9C981-FE03-424E-B5F8-6F4C56BC15EE
USER=willhf
LD_LIBRARY_PATH=/Users/willhf/Work/root/lib
COMMAND_MODE=unix2003
SSH_AUTH_SOCK=/tmp/launch-FkoIJx/Listeners
__CF_USER_TEXT_ENCODING=0x1F5:0:0
LIBPATH=/Users/willhf/Work/root/lib
LSCOLORS=Gxfxcxdxbxegedabagacad
PATH=/Users/willhf/Work/root/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
PWD=/Users/willhf/Work/Linux
LANG=en_US.UTF-8
ROOTSYS=/Users/willhf/Work/root
SHLVL=1
HOME=/Users/willhf
DYLD_LIBRARY_PATH=/Users/willhf/Work/root/lib
PYTHONPATH=/Users/willhf/Work/root/lib
LOGNAME=willhf
SHLIB_PATH=/Users/willhf/Work/root/lib
DISPLAY=/tmp/launch-eTAJwx/org.x:0
OLDPWD=/Users/willhf
_=/usr/bin/env

grail 11-06-2011 10:09 PM

I would be guessing it is the version of sed that is the problem as the shell is not playing a part in this.

Reuti 11-07-2011 07:35 AM

Quote:

Originally Posted by grail (Post 4517727)
I would be guessing it is the version of sed that is the problem as the shell is not playing a part in this.

Although it’s already marked as solved: I completely agree, as the sed in Mac OS X is not the GNU version. But you are free to compile the GNU version on your own and install it somewhere in your home directory and change the $PATH accordingly. I do this because of the -r option to sed, which is not available in the Mac version otherwise.

will.flanagan 11-07-2011 11:04 AM

Reuti and Grail,

Thanks for your help on this!

Cheers,
Will


All times are GMT -5. The time now is 09:09 PM.