substr not working


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting substr not working
# 1  
Old 05-11-2008
substr not working

Hi I am trying to run this command in ksh ...its not working

$line="123356572867116w1671716"
actual_length = 16
cut_line=`awk 'BEGIN{print substr(ARGV[1],1,actual_length)}' "$line"`
the substr is not giving me an output

how can i make it done
can anyone hwlp me on this

cut_line=`awk 'BEGIN{print substr(ARGV[1],1,16)}' "$line"`

This is working ...But I want variable to be passed instead of numeric
please guide me

Thanks,
# 2  
Old 05-11-2008
Code:
$line="123356572867116w1671716"
cut_line=`awk -v v=${#line} 'BEGIN{print substr(ARGV[1],1,v)}' "$line"`


Last edited by danmero; 05-11-2008 at 02:37 AM..
# 3  
Old 05-11-2008
If your shell supports this type of expansion (and ksh 93 should):

Code:
line="123356572867116w1671716"
actual_length = 16
cut_line=${line:0:$actual_length}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

3. Shell Programming and Scripting

awk substr

Hello life savers!! Is there any way to use substr in awk command for returning one part of a string from declared start and stop point? I mean I know we have this: substr(string, start, length) Do we have anything like possible to use in awk ? : substr(string, start, stop) ... (9 Replies)
Discussion started by: @man
9 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. UNIX for Dummies Questions & Answers

substr of a file

.wysiwyg { PADDING-RIGHT: 0px; PADDING-LEFT: 0px; BACKGROUND: #f5f5ff; PADDING-BOTTOM: 0px; MARGIN: 5px 10px 10px; FONT: 10pt verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif; COLOR: #000000; PADDING-TOP: 0px } .wysiwyg A:link { COLOR: #22229c } .wysiwyg_alink { COLOR:... (1 Reply)
Discussion started by: davyp74
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