LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-04-2011, 12:48 AM   #1
will.flanagan
LQ Newbie
 
Registered: Apr 2009
Posts: 15

Rep: Reputation: 0
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
 
Old 11-04-2011, 03:17 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
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.
 
Old 11-04-2011, 06:24 PM   #3
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by will.flanagan View Post
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.
 
Old 11-04-2011, 10:16 PM   #4
will.flanagan
LQ Newbie
 
Registered: Apr 2009
Posts: 15

Original Poster
Rep: Reputation: 0
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
 
Old 11-05-2011, 02:35 AM   #5
grizzeledtop
LQ Newbie
 
Registered: Jun 2007
Posts: 1

Rep: Reputation: 0
[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=37i=4:so=4:bd=4:cd=4r=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
 
Old 11-06-2011, 05:46 PM   #6
will.flanagan
LQ Newbie
 
Registered: Apr 2009
Posts: 15

Original Poster
Rep: Reputation: 0
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
 
Old 11-06-2011, 10:09 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
I would be guessing it is the version of sed that is the problem as the shell is not playing a part in this.
 
Old 11-07-2011, 07:35 AM   #8
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
Quote:
Originally Posted by grail View Post
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.
 
Old 11-07-2011, 11:04 AM   #9
will.flanagan
LQ Newbie
 
Registered: Apr 2009
Posts: 15

Original Poster
Rep: Reputation: 0
Reuti and Grail,

Thanks for your help on this!

Cheers,
Will
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular Expressions using sed TheCrow33 Programming 5 03-25-2011 07:17 PM
extract substring using sed and regular expressions (regexp) lindylex Programming 20 12-22-2009 10:41 AM
[SOLVED] sed, awk, Keep only text between two regular expressions scott_audio Linux - Newbie 9 08-06-2009 02:46 PM
Sed/awk help with regular expressions needed AP81 Programming 3 07-28-2008 07:26 AM
Sed and regular expressions tchernobog Linux - Software 2 08-14-2003 12:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration