cut and store last value of a variable into other


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting cut and store last value of a variable into other
# 1  
Old 12-25-2011
cut and store last value of a variable into other

Hi All, I am a newbie to unix.starting my career in unix.need 1 help from you all..pls help..

i am passing a file name "abc_delta" as argument to my script1.sh.
if file name contains "_delta" at last then echo pass else fail.how to fix it.

Note:file name will always contain "_delta" at last.
ex- c_delta,upc_u_delta,12_xyz_delta etc

--------------------------------------------------------
script1.sh code:

Code:
if test $1=='_delta' 
then
echo 'pass';
else
   echo 'fail';
fi

--------------------------------------------------------
what i need to write in if condition to solve my issue..

Last edited by Franklin52; 12-26-2011 at 10:24 AM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 12-25-2011
Code:
user@ubuntu:~/programs$ cat script1.sh 
#!/bin/sh
echo $1 | grep -q "_delta"
if [ $? -eq 0 ]
then
    echo pass
else
    echo fail
fi

user@ubuntu:~/programs$ ./script1.sh abc_delta
pass

# 3  
Old 12-25-2011
One more alternative using awk:
Code:
#!/bin/sh 
a=`echo $1|awk '/_delta/{ print 1}'` 
if [ $a -eq 1 ] 
then     
echo pass
else     
echo fail 
fi

This User Gave Thanks to pandeesh For This Post:
# 4  
Old 12-25-2011
thnks balajesuri n pandeesh..both is working but when im passing filename for ex- abc_delta_hk then too message "pass" returned.my case is if at last there is "_delta" then only pass.

In ur post, grep searching for "_delta" anywhere in filename which i dnt want.kindly reply.
# 5  
Old 12-25-2011
try as follows in balajesuri code:
Code:
echo $1 | grep -q "_delta$"

instead of
Code:
 echo $1 | grep -q "_delta"

This User Gave Thanks to greet_sed For This Post:
# 6  
Old 12-25-2011
Hi ,

try this ..
Code:
#!/bin/bash
 grep _delta$ $1
if [ $? -eq 0 ]
then
    echo pass
else
    echo fail
fi

Moderator's Comments:
Mod Comment How to use code tags when posting data and code samples.

Last edited by Franklin52; 12-26-2011 at 10:25 AM.. Reason: Please use code tags for data and code samples, thank you
These 2 Users Gave Thanks to sprakash For This Post:
# 7  
Old 12-25-2011
hi,
try this:
Code:
#!/bin/sh  
a=`echo $1|awk '/_delta$/{ print 1}'`
if [ $a -eq 1 ]  
then    
echo pass
else
echo fail
fi


Last edited by pandeesh; 12-25-2011 at 02:52 PM..
This User Gave Thanks to pandeesh For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Store result variable

Friends have the following problem: cat $PATH_DAT/mr.txt | nawk 'BEGIN { CantPnt=0; NumReg=0; FS="|" } { NumReg++ CantPnt=CantPnt+int($2) } END{ printf... (5 Replies)
Discussion started by: tricampeon81
5 Replies

2. Shell Programming and Scripting

Cut each column value and store in variable

Hi Pals, I have a file which contains a set of similar lines, as follows: Remedy Support Action Triggered by Incident Modification of type: ARA username2 ########## ARA|INC0000178532 INC0000178532 0000000019879 000000000038372 Remedy... (6 Replies)
Discussion started by: Khushbu
6 Replies

3. Shell Programming and Scripting

How to store file name in a variable?

Hi All, Daily I am generating a file dfm_daily_file_ ex: dfm_daily_file_05072015 date will be changed daily. Once the file is FTP it is deleted. I have tried the below code to get the file name with any date and store it in a variable its not working. #!/bin/ksh ... (4 Replies)
Discussion started by: ROCK_PLSQL
4 Replies

4. Shell Programming and Scripting

I can't store value in the variable

!#bin/bash clear var= grep @gmail.com email.txt | wc -l echo $var echo $var exit 0 OUTPUT: 1000 _ _ Where _ represent space (no value or nothing) (4 Replies)
Discussion started by: Muhammad Rehan
4 Replies

5. Shell Programming and Scripting

How do I store stdout in a variable?

These seems ridiculously simple but I can't get it to work. Using korn shell and I want to pass in a flag to tell my echo statements to either write to the screen for debugging or a file if not. So I have something like: if ; then logout=&1 else logout='logfile.out' fi Then... (2 Replies)
Discussion started by: DJR
2 Replies

6. UNIX Desktop Questions & Answers

How to store the value of grep in a variable

Hi , I was trying to store the value of a grep command to store in a variable so i can use it somewhere else in my script but i am getting error, i am using bash shell in linux: var= `grep -n "$DATE" testfile.log | head -n 1 | sed -n 's/^\(*\).*/\1/p` echo "$var"I get the error: unexpected... (5 Replies)
Discussion started by: learninguser235
5 Replies

7. Shell Programming and Scripting

Not able to store command inside a shell variable, and run the variable

Hi, I am trying to do the following thing var='date' $var Above command substitutes date for and in turn runs the date command and i am getting the todays date value. I am trying to do the same thing as following, but facing some problems, unique_host_pro="sed -e ' /#/d'... (3 Replies)
Discussion started by: gvinayagam
3 Replies

8. Shell Programming and Scripting

Store o/p of awk in a variable

Hi , Here is my script echo 'Enter MSISDN for the Calling Number' read ms acc=`sqlplus -s testing/testing123@BP_$ARBORENV<<EOF set heading off; set feedback off; select external_id from external_id_equip_map where subscr_no = (select subscr_no from external_id_equip_map where account_no =... (1 Reply)
Discussion started by: Pratik4891
1 Replies

9. Shell Programming and Scripting

store timestamp in a variable

I am new to Unix shell Script _________________________ db2 connect to r2pdev user bmwdevup using summer08 >>$monlog # get the current timestamp from SYSIBM.SYSDUMMY1 currenttimestamp="" echo "Run SQL select current timestamp from SYSIBM.SYSDUMMY1 with ur" >>$monlog db2 "select current... (8 Replies)
Discussion started by: regnumber
8 Replies

10. UNIX for Dummies Questions & Answers

How to Store command O/P to a variable...

Hi all.. I got a problem.. Its easy to redirect o/p to a file.. But Is it possible to redirect the O/P to a variable? For example: I've a command in my script: string1=cut -d ':' -f2 file.txt When I do: echo $string1 The value is empty... Pls suggest me how to store the value... (7 Replies)
Discussion started by: smartbuddy
7 Replies
Login or Register to Ask a Question