The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #3 (permalink)  
Old 10-10-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,373
Quote:
Originally Posted by vanand420 View Post
Hi,
I am facing a little problem...
I have a line like this :
asdcvashfasashXXXXxxxzxcadd:sdcashjqdasdsmgdkdaxdsnd;
I want to print just a portion of line i.e starting from left 5 characters from ":" and upto ";" i.e. in this case it would be
"xcadd:sdcashjqdasdsmgdkdaxdsnd;"
The length of right most string can vary but end with ";".

Use shell parameter expansion; there's no need to waste time and CPU cycles with external commands.


Code:
line="asdcvashfasashXXXXxxxzxcadd:sdcashjqdasdsmgdkdaxdsnd;"
temp=${line%?????:*}
printf "%s\n" "${line#"$temp"}"

Quote:
Also what to do if I want to print just 5 character left to ";"


Code:
line="asdcvashfasashXXXXxxxzxcadd:sdcashjqdasdsmgdkdaxdsnd;"
temp=${line%??????*}
temp=${line#"$temp"}
printf "%s\n" "${temp%;}"