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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Substring in Unix shell script ?? 1

Status
Not open for further replies.

skuthe

Programmer
Sep 10, 2002
33
0
0
US
Hi,
Can anyone help me in finding a substring of a long string using unix shell script ?

e.g:
load_jde_apexon_supplier_master_supplier_loc_pur_org.log

I want to extract "supplier_master_supplier_loc_pur" from the above string.

How can I do that ??

Pl. help!!.

Thank you in advance.
 
What's the criteria for finding a substring?
Is it a particular pattern that you're looking for?
Is it a particular location of a substring in a string?
What is it?

Give us a couple of sample strings so that we can see the 'trend', pls. vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Use ksh93:

#!/usr/dt/bin/dtksh

Text=`echo load_jde_apexon_supplier_master_supplier_loc_pur_org.log`

print ${Text:16:47}

This will print out:
supplier_master_supplier_loc_pur

The syntax is {parameter:eek:ffset:length}

What you wants starts at position 16 (count starts at 0) and ends at position 47. Or you can just use {parameter:eek:ffset} and if you did that it would print from the position you stated to start and print the rest to the end.
Example:
print ${Text:16}
supplier_master_supplier_loc_pur_org.log
 

Hi,
if your pattern is fixed you could use cut

a=&quot;load_jde_apexon_supplier_master_supplier_loc_pur_org.log&quot;

echo $a | cut -d'_' -f4-8




 
Awk's match() function is perfectly suited to what you want
to do.
 
Hi guys,
Thanks for the rapid responses...
What I am trying is to get file names using for loop in a shell script and then select some portion of that file name which I want to use it for different purpose.
e.g. Below are some of the files names that I have...
load_jde_apexon_part_master_part_category.log
load_jde_apexon_part_master_part_movements.log
load_jde_apexon_part_master_part_plant.log
load_jde_apexon_po_purchase_order_del_sched.log
load_ava_apexon_planning_group.log
load_ava_apexon_po_purchase_order_del_sched.log
load_ava_apexon_purchasing_groups.log
load_ava_apexon_supplier_master_approved_supplier.log
If u see only two chunks (load) and (apexon) are common others vary.
Also, I need to pickup different chunks from each filename.

Hope this clarifies more and helps.

Thanks.
 


a=&quot;load_jde_apexon_supplier_master_supplier_loc_pur_org.log&quot;

echo $a | cut -d'_' -f4- | cut -d'.' -f1


 
Thanks tdatgod !!
It worked well.
 
Unix give you many opportunities to sort out this. Try having a look at man-pages for awk, sed, cut, paste, printf or simply the shells.

NB! this is /bin/sh syntax.
Always start your shells by specifying the shell you want to use like this: #!/bin/sh

You can do it very simple by using variable substitution directly available in the shell.

#removing first part of FILENAME
SHORTNAME=${FILENAME##load_jde_apexon_}
#trick is: search variable=FILENAME ##=until/incl.pattern
#same result: SHORTNAME=${FILENAME##*xon_}
#using wildchar

#removing last part of SHORTNAME
SHORTNAME=${SHORTNAME%%.log}
#trick is: search variable=SHORTNAME %%=from/incl. pattern


I suppose you get the filenames by using ls
example: ls -1 load_*_apexon_*
or simply: ls -1

Then you can read the filenames into a variable, one by one, and handle them.

example:
ls -1 | while read FILENAME
do
echo &quot;File $FILENAME &quot;
done

You can take different action depending on parts of filename:

case $FILENAME in
load_jde_apexon_*.log)
print 'Load log for JDE/Apexon job: \c'
SHORTNAME=${FILENAME##load_jde_apexon_}
SHORTNAME=${SHORTNAME%%.log}
print $SHORTNAME
if [ &quot;$SHORTNAME&quot; = &quot;po_purchase_order_del_sched&quot; ];then
tail $FILENAME
fi
;;


*) #do nothing
;;
esac


 
Many thanks to you 2r!
Your script helped me a whole lot...
using ## and %%. I had no idea about this...
god bless! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top