Cutting segment of a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cutting segment of a string
# 1  
Old 11-04-2008
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
# 2  
Old 11-04-2008
Code:
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

# 3  
Old 11-04-2008
I hope that's what you want.
Code:
# L1A_FILE=S2002254132542.L1A_MLAC.x.hdf
# eval $(awk -F'[.|_]' '{print "BASE="$1, "SUFIX2="$3}' <<< $L1A_FILE)
# echo $BASE
S2002254132542
# echo $SUFIX2
MLAC

# 4  
Old 11-04-2008
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.
# 5  
Old 11-04-2008
Quote:
Originally Posted by msb65
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
# 6  
Old 11-04-2008
Use parameter expansion
Code:
# x=123123123
# echo ${x%%3*}
12

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cutting a string using more than one character as delimiter

Hi , I have a set of files in a folder which i need to cut in to two parts.... Sample files touch AE_JUNFOR_2014_MTD_2013-05-30-03-30-02.TXT touch AE_JUNFOR_2014_YTD_2013-05-30-03-30-02.TXT touch temp_AE_JUNFOR_2014_MTD_2013-05-30-03-30-02.TXT touch... (4 Replies)
Discussion started by: chillblue
4 Replies

2. Shell Programming and Scripting

Cutting a string and storing it in a variable

Hello I have a script: path=test1 echo "${path%?}" till this the program is successful in deleting hte last character i.e. "1" and displays an output --> test. After this how can i save this output to another variable. (2 Replies)
Discussion started by: Kishore920
2 Replies

3. Shell Programming and Scripting

Cutting string inside if

I was trying the below statement if It is working fine if I run it in a test file. but not working, when I am trying in my actual script. Error: : "${FXML_line:1129:1}": bad substitution Thanks in advance :) PS: Above if block I have a while loop which is reading a... (4 Replies)
Discussion started by: ezee
4 Replies

4. Programming

Data segment or Text segment

Hi, Whether the following piece of code is placed in the read-only memory of code (text) segment or data segment? char *a = "Hello"; I am getting two different answers while searching in google :( that's why the confusion is (7 Replies)
Discussion started by: royalibrahim
7 Replies

5. UNIX for Dummies Questions & Answers

Help with cutting a string

Hi All, I have a string in the following format "abcd | fghfh | qwer | ertete" I need to cut the values in the following format line1 = abcd | fghfh | qwer line2 = ertete Simply speaking a want to cut all the values before the last delimiter from the line and print it on one line and... (11 Replies)
Discussion started by: amit_kv1983
11 Replies

6. Shell Programming and Scripting

Cutting the value from a string

Hi all I have a string variable ${WHERE_SQL1} where i want to cut the first value of a variable. Eg ${WHERE_SQL1} = 'Where a.id =.................' I the string to be 'a.id =.......' Thanks in advance (2 Replies)
Discussion started by: theeights
2 Replies

7. Shell Programming and Scripting

problems getting a segment from a string output

Hi I'm creating a script that creates files from svn checkout and compress them using tar.gz the script gets the repository name from command line argument i need to capture a number from the last line of the output and create a file name from it. the svn returns output of all the file... (5 Replies)
Discussion started by: tzvio
5 Replies

8. Shell Programming and Scripting

Trouble cutting characters into a string.

I just have a couple of quick questions. I am having trouble with this cut. I am basically trying to cut the string so that i can insert the users guess at the appropriate point in the string. $letters is the character count of the $word. What it seems to do is cut the character into the... (0 Replies)
Discussion started by: Makaer
0 Replies

9. Shell Programming and Scripting

cutting part of string

Hi, I wanted to cut a specific portion from given string. How would I do that? Example: /u09/core/inbound/abc.txt is my string. I want abc.txt in a variable. Please help me. Regards, Dhaval (3 Replies)
Discussion started by: dhaval_khamar
3 Replies

10. Shell Programming and Scripting

Cutting Up a String

I have a file name coming in as such <string>_YYYYMMDD.DAT The string could be anything. I want to cut out the date and put it in a variable. Can someone help me with this? (4 Replies)
Discussion started by: lesstjm
4 Replies
Login or Register to Ask a Question