How to do SUBSTR in Scripting.


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
How to do SUBSTR in Scripting.

Small help in unix scripting is needed.

I have two strings.

srcFile=/rndlacb/sk70745/compile/ar_cust.pc
GIM_PATH=/rndlacb/sk70745/compile/

I want to cut only the file name from the above string.
Example :
F_Name=ar_cust.pc

Currently i used lengthy steps to achieve this...

lenSrc=`expr length $srcFile`
lenPath=`expr length $GIM_PATH`
lenPath=`expr $lenPath + 2`
fileLen=`expr $lenSrc - $lenPath`
fileLen=`expr $fileLen + 1`
fileName=`expr substr $srcFile $lenPath $fileLen`


Anyone can help me in single or two steps?
# 2  
Code:
basename '/a/b/c/d.e'

Code:
#!/bin/ksh
a='/a/b/c/d.e'

echo "${a##*/}"


Last edited by vgersh99; 09-19-2007 at 12:56 AM..
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #946
Difficulty: Medium
A ping flood attack is a simple DOS attack where the attacker overwhelms the victim with TCP echo request packets.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Correct use of substr

I have a file that looks like this: >ID_1 ATGCATGC >ID_2 ATGCATGC >ID_3 ATGCATGC >ID_4 ATGCATGC And I am using the following script to "extract" specific positions from the sequences: awk '/^>/{id=$0; next}{ print id "\n" substr( $1,1,1 ) substr ($1,4,2 ) substr ($1,7,1) }'... (12 Replies)
Discussion started by: Xterra
12 Replies

2. Shell Programming and Scripting

awk and substr

Hello All; I have an input file 'abc.txt' with below text: 512345977,213458,100021 512345978,213454,100031 512345979,213452,100051 512345980,213455,100061 512345981,213456,100071 512345982,213456,100091 512345983,213457,100041 512345984,213451,100011 I need to paste the first field... (10 Replies)
Discussion started by: mystition
10 Replies

3. UNIX for Dummies Questions & Answers

Substr

awk '/^>/{id=$0;next}length>=7 { print id, "\n"$0}' Test.txt Can I use substr to achieve the same task? Thanks! (8 Replies)
Discussion started by: Xterra
8 Replies

4. Shell Programming and Scripting

How to use if/else if with substr?

I have a command like this: listdb ID923 -l |gawk '{if (substr($0,37,1)==1 && NR == 3)print "YES" else if (substr ($0,37,1)==0 && NR == 3) print "NO"}' This syntax doesn't work. But I was able to get this to work: listdb ID923 -l |gawk '{if (substr($0,37,1)==1 && NR == 3)print "YES"}' ... (4 Replies)
Discussion started by: newbie2010
4 Replies

5. Shell Programming and Scripting

Substr with awk

Hi to all, I'm here again, cause I need your help to solve another issue for me. I have some files that have this name format: date_filename.csv In my shell I must rename each file removing the date so that the file name is filename.csv To do this I use this command: fnames=`ls ${fname}|... (2 Replies)
Discussion started by: leobdj
2 Replies

6. UNIX for Dummies Questions & Answers

substr

can anybody explain this code? thanks in advance..:) (6 Replies)
Discussion started by: janani_kalyan
6 Replies

7. Shell Programming and Scripting

get substr?

Hi, I have a long string like, aabab|bcbcbcbbc|defgh|paswd123 dedededede|efef|ghijklmn|paswd234 ghghghghgh|ijijii|klllkkk|paswd345 lmlmlmmm|nononononn|opopopopp|paswd456 This string is devided into one space between substrings. This substrings are, aabab|bcbcbcbbc|defgh|paswd123... (6 Replies)
Discussion started by: syamkp
6 Replies

8. UNIX for Dummies Questions & Answers

Substr

Hi, My input file is 41;2;xxxx;yyyyy.... 41;2;xxxx;yyyyy.... 41;2;xxxx;yyyyy.... .. .. I need to change the second field value from 2 to 1. i.e., 41;1;xxxx;yyyyy.... 41;1;xxxx;yyyyy.... 41;1;xxxx;yyyyy.... .. .. Thanks in advance. (9 Replies)
Discussion started by: deepakwins
9 Replies

9. Shell Programming and Scripting

Using substr

What is the more efficient way to do this (awk only and default FS) ? $ echo "jefe@alm"|awk '{pos = index($0, "@");printf ("USER: %s\n",substr ($0,1,pos-1))}' USER: jefe Thx in advance (2 Replies)
Discussion started by: Klashxx
2 Replies

10. Shell Programming and Scripting

How to use awk substr ?

Hi all, I have a flatfile I would like to get ext = 7950 , how do I do that ? if ($1 == "CTI-ProgramStart") { ext = substr($9,index($9,"Extension")+11,4); But why it is not working ???? Please help . Thanks (1 Reply)
Discussion started by: sabercats
1 Replies

Featured Tech Videos