Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

substring in shell script

Status
Not open for further replies.

cruel

Programmer
Aug 6, 2001
131
Is there a way to get a substring from the arguments you read in? For example, you have the following arguments:
order1201.txt
order1202.txt

I want to process those files using "for ..." and create output files like
summary1201.txt
summary1202.txt

Can I get the substring starting from the 6th position in each of the argument for a legnth of 4 to create a compatible output file.

Thanks
 
for i in $* ; do
SS=`echo $i | cut -c 6-`
echo $SS
done

this will take everything passed to the script, and cut it from the 6th character to the end.


crowe
 
That was quick and helpful! So, if I want characters from 6th~9th, would I use
SS=`echo $i | cut -c 6-9` ?
Thanks again
 
Correct, cut -c 6-9 will get the 6th through 9th characters.

crowe
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top