Solved

awk substring question

Posted on 1998-02-16
4
1,300 Views
Last Modified: 2013-12-26
How do I use the substring function in awk?

I am trying to do this on a Solaris box;
for example given string 123456789...  (unknown length)
I would like to get back the substring which excludes the first n characters ie for first 3 characters return 456789...
0
Comment
Question by:chrisvo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 

Expert Comment

by:barrycheung
ID: 1296007
suppose your string is stored in a variable str1, the awk syntax is

substr(str1, 4),
for unknown length, example

echo 1234567890 | awk {'print substr($0, 4)'}
0
 

Author Comment

by:chrisvo
ID: 1296008
Yes, but how do you assign the result to another variable?

For example,
if $NUM is an argument passed to a script,
could this be done:
$short=`echo $NUM | awk '{print substr($NUM,4}'`

I think I'm doing something wrong because the script fails
to output the value of $short


0
 
LVL 3

Expert Comment

by:rickyr
ID: 1296009
Hi...
Checkout your $ symbol when you assigned your variable with command sunstitution...
$var="will get you every time"
better...
var="ok you assigned me"
echo $var
regards
0
 

Accepted Solution

by:
zhangz earned 50 total points
ID: 1296010
You may try the following as:
#short=$(num=1234567890;export num;echo "1"|awk \ 'BEGIN{num=ENVIRON["num"]}{print substr(num,4)}')
#echo $short

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question