Cutting segment of a string

Hi,

I am using bash. My question concerns cutting out segments of a string. Given the following filename:

S2002254132542.L1A_MLAC.x.hdf

I have been able to successfully separate the string at the periods (.):

$ L1A_FILE=S2002254132542.L1A_MLAC.x.hdf
$ BASE=$(echo $L1A_FILE | awk -F. '{ print $1 }')
$ SUFFIX=$(echo $L1A_FILE | awk -F. '{ print $2 }')
$ echo $BASE
S2002254132542
$ echo $SUFFIX
L1A_MLAC

I would now like to cut the variable $SUFFIX so that I get only "MLAC". I have a feeling I could just add a pipe to a cut command, but there is a complication. The problem is that I will also have file names like:

S2002254132542.L1A_LAC.x.hdf

where the suffix will be L1A_LAC (note the missing M). Therefore although the cut will always start at the same number, it will not cut TO the same number. Rather I would just like to tell it to start cutting $SUFFIX and cut to the end of the string.

  • How do you tell cut to start at a specific number element in a string and cut until it reaches the end?

Mike

root@isau02:/data/tmp/testfeld> cat infile
S2002254132542.L1A_MLAC.x.hdf
S2002254132542.L1A_LAC.x.hdf
root@isau02:/data/tmp/testfeld> sed 's/[^.]*\.\([^.]*\)\..*/\1/g' infile
L1A_MLAC
L1A_LAC

I hope that's what you want.

# L1A_FILE=S2002254132542.L1A_MLAC.x.hdf
# eval $(awk -F'[.|_]' '{print "BASE="$1, "SUFIX2="$3}' <<< $L1A_FILE)
# echo $BASE
S2002254132542
# echo $SUFIX2
MLAC

Hi danmero,

Yes that's exactly what I want.

I am curious though if there is a way to tell cut to start at a specific character within a string, and cut until the end of the string.

This should work:

a="testing"; len=${#a}; echo $a | cut -c3-$len

Use parameter expansion

# x=123123123
# echo ${x%%3*}
12