Using variables in funcion substr.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using variables in funcion substr.
# 1  
Old 07-10-2009
Bug Using variables in funcion substr.

Hello.. every body... i hope you can helo me with this small code.

I need to substraction a one part of the string...i try to do with this code:

Code:
cat $TSMINCLUDE | while read c
do
 l=${#c}
  if [[ $l == "6" ]]; then
     long=$((l-5))
  else
     long=$((l-6))
  fi
 fs=`echo $c | awk '{s=substr($1,1,$long); print s }'`
 echo $c ... $fs

but i found that the problem is $long because if i put a number the funtion its working.

Can you help me please??...

thx a lot Smilie
done

Last edited by vgersh99; 07-10-2009 at 01:40 PM.. Reason: code tags, PLEASE!
# 2  
Old 07-10-2009
Code:
#!/bin/ksh
while read c
do
 l=${#c}
  if [ "${l}" -eq 6 ]; then
     long=$((l-5))
  else
     long=$((l-6))
  fi
 fs=`echo $c | awk '{s=substr($1,1,awkLong); print s }' awkLong="${long}"`
 echo $c ... $fs
done < "${TSMINCLUDE}"

# 3  
Old 07-10-2009
thx a lot but the problem already exist....

when i try to displya the value of fs, the result is like:
/oraexport/.../* ...
and the correct form would be:
/oraexport/.../* .../oraexport

Smilie

---------- Post updated at 12:01 PM ---------- Previous update was at 11:56 AM ----------

sorry vgersh99 the code is working...Smilie ths a lot for the help...Smilie
# 4  
Old 07-10-2009
Quote:
Originally Posted by odette.delfin
thx a lot but the problem already exist....

when i try to displya the value of fs, the result is like:
/oraexport/.../* ...
and the correct form would be:
/oraexport/.../* .../oraexport

Smilie
I don't know what you're trying to do - I've just shown you one of the ways to pass a shell variable into awk.
# 5  
Old 07-10-2009
Code:
#!/bin/ksh
cat $TSMINCLUDE | while read c
do
  len=${#c}
  if [ "$len" = "6" ] ; then
    # you can use builtin, no need to execute subprocess
     ((long=len-5))
  else
     ((long=len-6))
  fi
  # and builtin substring works fine with ksh93 and bash (1st index is 0)
  fs=${c:0:$long}
  echo "$c ... $fs"
done

Example data helps always = some lines from your file, easier to test your problem.

Download ksh93, if substring not work.

Testing substr
Code:
a="12345"
echo ${a:2:2}  # echo 34 or error

# 6  
Old 07-10-2009
thx a lot. Smilie

the two codes are working... Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

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. 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

3. 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

4. UNIX for Dummies Questions & Answers

substr

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

5. Programming

Using funcion in a shared library

Is there any way in C to access a function in C shared library. I have used dlopen to access /load the c shared library but unable to use the function in the shared object. Thanks in advance :b: (1 Reply)
Discussion started by: yhacks
1 Replies

6. 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

7. 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

8. 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

9. Shell Programming and Scripting

Substr of variable

Dear All, I am new to Unix and need your help. I am trying to write b-shell script which will return substr of some variable like this d="asdfg" return Substr(d,1,5). Please help (5 Replies)
Discussion started by: giviut
5 Replies

10. Shell Programming and Scripting

awk substr?

Sorry if this has been posted before, I searched but not sure what I really want to do. I have a file with records that show who has logged into my application: 2003-03-14:I:root: Log_mesg: registered servername:userid. (more after this) I want to pull out the userid, date and time into... (2 Replies)
Discussion started by: MizzGail
2 Replies
Login or Register to Ask a Question