1

I have this file -

#
# gridID 1
#
gridtype  = lonlat
gridsize  = 10512
xsize     = 144
ysize     = 73
xname     = lon
xlongname = "longitude"
xunits    = "degrees_east"
yname     = lat
ylongname = "latitude"
yunits    = "degrees_north"
xfirst    = 0
xinc      = 2.5
yfirst    = 90
yinc      = -2.5

and I want to search and replace

xfirst  = 0 

with

xfirst = -180

I tried this on Ubuntu 16.04

sed s/=[[:space:]]0/-180/

and I get

sed:No match

Where am I going wrong ?

  • your expression sed s/=[[:space:]]0/-180/ file does not give me sed:No match but returns the output with line xfirst -180 – RomanPerekhrest Aug 28 '17 at 14:16
  • 1
    sed just doesn't substitute if there is no match. There is a No match error in GNU sed, but I'm not sure how it is triggered. – Philippos Aug 28 '17 at 14:18
2

You forgot to quote the [[:space:]] glob operator and are using a shell like csh, tcsh, fish (though fish doesn't support [...] glob operators), zsh, bash -O failglob (most probably (t)csh given the error message) that (rightly) fails the command when a glob fails to match.

tcsh doesn't recognise the [:space:] POSIX character classes, but that s/=[[:space:]]0/-180/ is still recognised as a glob.

After a mkdir -p 's/=a]0/-180', the glob would match. That s/=[[:space:]]0/-180/ would be expanded to s/=a]0/-180/ and you wouldn't get the No match error.

Instead, here, you want to quote that for the shell not to consider it as a glob pattern and pass the string literally to sed.

(Here, I'm also adding the missing * to allow 0 or more spacing characters in the pattern, and the missing = in the replacement):

sed 's/=[[:space:]]*0/=-180/'

Or to preserve the original spacing:

sed 's/\(=[[:space:]]*\)0/\1-180/'
1

Use the following sed expression:

sed 's/^\(xfirst[[:space:]]*=[[:space:]]*\).*/\1-180/' file

  • \1 - points to the 1st captured group (i.e. (xfirst....))
  • thanks for the answer. I will try this out. Can you also explain where I went wrong ? – gansub Aug 28 '17 at 14:12
  • your expression will remove =<space> along with 0 – RomanPerekhrest Aug 28 '17 at 14:17
1

Another sed command approach.

sed '/^xfirst/ s/\d\+/-180/'

or this way.

sed '/^xfirst/ s/[0-9]+/-180/'

or simply replace 0 with -180

    sed '/^xfirst/ s/0/-180/'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.