Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

awk substring question

Posted on 1998-02-16
4
Medium Priority
?
1,301 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 200 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

670 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