Solved

awk substring question

Posted on 1998-02-16
4
1,292 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
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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to monitor remote shell execution on linux 9 100
maxBlock challenge 30 129
has77  challenge 9 85
Making an alias 7 95
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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 Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
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.

777 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