
10-10-2008
|
|
Shell programmer, author
|
|
|
Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,373
|
|
Quote:
Originally Posted by vanand420
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%;}"
|