Solved

awk substring question

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now