Solved

awk substring question

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Doc'in system (example?) BA 7 127
Use of condition with 'serial' in ansible 2 101
ERP and the other system process verification 5 25
Adjust the codes 3 63
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: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

739 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