?
Solved

how to truncate a string in bash shell script?

Posted on 2003-03-25
3
Medium Priority
?
9,878 Views
Last Modified: 2007-12-19
I would like to know how to truncate a string in bash shell script? Thanks.
0
Comment
Question by:pink21
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 5

Accepted Solution

by:
ecw earned 80 total points
ID: 8203912
To strip off a known part,
   ${var%pattern}        # shortest match
or ${var%%pattern}       # longest match
Where pattern is a valid filename expansion pattern eg.
  var=main.c.old
  echo ${var%.*} ${var%%.*}
While output
  main.c main

To get the first (for example 10) chars of a var
  var=`echo $var | cut -c -10`
or var =`expr substr 1 10 $var`

0
 
LVL 20

Expert Comment

by:jmcg
ID: 10225145
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: ecw {http:#8203912}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0

Featured Post

Automating Terraform w Jenkins & AWS CodeCommit

How to configure Jenkins and CodeCommit to allow users to easily create and destroy infrastructure using Terraform code.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question