Sub String in UNIX Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sub String in UNIX Script
# 1  
Old 06-01-2014
Sub String in UNIX Script

Hi

I have filenames coming as

Code:
FILE.V<Version>.YYYYMMDD.z

FILE.V260.20140423.z
FILE.V1.20140523
FILE.V9999.20140324.z

How do I extract the version number in a variable

I need
Code:
260
1
9999

extracted into variable
by Unix script

Last edited by Don Cragun; 06-01-2014 at 02:21 AM.. Reason: Add CODE tags.
# 2  
Old 06-01-2014
Welcome to forums,

One way to do so
Code:
$ file="FILE.V260.20140423.z"
$ string=$(cut -d'.' -f2 <<<$file |  sed 's/[[:alpha:]]//g')
$ echo $string
260

for multiple
Code:
for file in FILE.*; do
        string=$(cut -d'.' -f2 <<<$file |  sed 's/[[:alpha:]]//g')
        echo $string
done

if here string is not supported then replace

string=$(cut -d'.' -f2 <<<$file | sed 's/[[:alpha:]]//g')

with

string=$(echo $file | cut -d'.' -f2 | sed 's/[[:alpha:]]//g' )

or

string=$(echo $file | awk -F'.' '{sub(/[[:alpha:]]/,x,$2);print $2}'

---------- Post updated at 12:07 PM ---------- Previous update was at 11:43 AM ----------

Or else try this

Code:
$ file=FILE.V260.20140423.z

$ str1="${file#"${file%%[[:digit:]]*}"}"

$ str2="${str1%%[^[:digit:]]*}"

$ echo $str2
260


Last edited by Akshay Hegde; 06-01-2014 at 02:25 AM.. Reason: missed something
# 3  
Old 06-01-2014
For any shell that performs POSIX standard variable expansions (such as bash and ksh), you could also try:
Code:
#!/bin/ksh
for file in FILE.V*
do	version="${file#FILE.V}"
	version="${version%%.*}"
	printf "File %s version is %s\n" "$file" "$version"
done

If the files listed in the 1st post in this thread are present in the directory where this script is run, it will produce the output:
Code:
File FILE.V1.20140523 version is 1
File FILE.V260.20140423.z version is 260
File FILE.V9999.20140324.z version is 9999

Note that this script only uses shell built-ins so it should be more efficient than the scripts Akshay suggested (which invoke cut and sed, or awk depending on which suggestion you use).
# 4  
Old 06-02-2014
Code:
vers=`expr "$file" : '[^.][^.]*\.V\([^.][^.]*\)'`

old school... should work everywhere pretty much.
Feel free to replace backticks with $(...) which is pretty much everywhere now.. like

Code:
vers=$(expr "$file" : '[^.][^.]*\.V\([^.][^.]*\)')

Arguably if you have $(..) (e.g. bash or ksh), then you might like some of the other solutions better.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search partial string in a file and replace the string - UNIX

I have the below string which i need to compare with a file and replace this string in the file which matches closely. Can anyone help me on this. string(Scenario 1)- user::r--,user::ourfrd:r-- String(Scenario 2)- user::r-- File **** # file: /local/Desktop/myfile # owner: me # group:... (6 Replies)
Discussion started by: sarathy_a35
6 Replies

2. UNIX for Beginners Questions & Answers

Concatenate a string and number and compare that with another string in awk script

I have below code inside my awk script if ( $0 ~ /SVC IN:/ ) { svc_in=substr( $0,23 , 3); if (msg_start == 1 && msg_end == 0) { msg_arr=$0; } } else if ( $0 ~ /^SVC OUT:/ ) { svc_out=substr( $0, 9, 3); if (msg_start == 1 && msg_end == 0) ... (6 Replies)
Discussion started by: bhagya123
6 Replies

3. Shell Programming and Scripting

String Function in UNIX

Hi - Have file1 which has the below date 08/31/2018 And also have file2 which has the below texts ASOF:<CMODate> FUND I need to read the second file if it has colon (:) then move the date from first file to second file like this ASOF:08/31/2018 have used cut -d":" -f1 and moved the... (2 Replies)
Discussion started by: Mohan0509
2 Replies

4. Shell Programming and Scripting

UNIX shell script to search a string in a file

Hi folks, I am new for shell script, I hope somebody to help me to write shell script My requirement is below steps 1. I have apache access.log i.e located in /var/log/httpd/ Ex. 127.0.0.1 - - "GET... (14 Replies)
Discussion started by: Chenchireddy
14 Replies

5. Shell Programming and Scripting

String increment in UNIX

Hi, I am able to increment numbers but unable to increment the charters in unix -AIX. Source : AAA BB CCC Increment Number : 5 OUTPUT: AAA BB CCC AAA BB CCD AAA BB CCE AAA BB CCF AAA BB CCG Thanks onesuri Please use CODE tags as required by the forum rules. I have made a wild... (5 Replies)
Discussion started by: onesuri
5 Replies

6. Shell Programming and Scripting

Extract a string from another string in UNIX

I have a string string="Please have a nice day and sleep well Replace_12123_31233_32134_12342 Good day" How do i replace "Replace_12123_31233_32134_1234" in the above string.?? Please help. Regards, Qwerty (3 Replies)
Discussion started by: qwertyu
3 Replies

7. UNIX for Dummies Questions & Answers

Comparing a String variable with a string literal in a Debian shell script

Hi All, I am trying to to compare a string variable with a string literal inside a loop but keep getting the ./testifstructure.sh: line 6: #!/bin/sh BOOK_LIST="BOOK1 BOOK2" for BOOK in ${BOOK_LIST} do if then echo '1' else echo '2' fi done Please use next... (1 Reply)
Discussion started by: daveu7
1 Replies

8. Shell Programming and Scripting

string parsing using UNIX

I got multple sql files.such as >>vi abc.sql select A.SITENAME, NULL NULL A.CREATE_DTM NULL A.MODIFY_DTM NULL FROM ${STG_RET_ITEM} A INNER JOIN ${STG_INC_COMP} B ON (A.CUSTID=B.CUSTID) LEFT OUTER JOIN ( select C.SITEID,SITESTATUS,MIN_EFF_DT,CURR_ST_DT,MAX_IN_DT,MAX_ACT_DT from... (4 Replies)
Discussion started by: ali123
4 Replies

9. Shell Programming and Scripting

How to trim a string in unix shell script

I am reading a string like $/folder1/folder2/filename from a file And I am storing it in a variable say $path. I want to store the path within single quotes in order to use the path within a command. if i set like path="'"$path"'" echo $path It is printing like ' $/folder1/folder2/filename'... (6 Replies)
Discussion started by: Shri123
6 Replies

10. Shell Programming and Scripting

Tokenizing a String in unix

Hi All, I have String i want to tokenize based on one delimiter. Original String is -pComments.properties,-iPELF4 i want to tokenize the original string based on ',' (comma) as delimiter and collect them individually like string1=-pComments.properties string2=-iPELF4 ... (1 Reply)
Discussion started by: rajeshorpu
1 Replies
Login or Register to Ask a Question