The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
get characters from output of a command in a variable muaz Shell Programming and Scripting 3 06-02-2008 09:13 AM
ls command output to variable in script dsrookie UNIX for Dummies Questions & Answers 1 03-10-2008 04:14 PM
assigning command output to a shell variable kprattip Shell Programming and Scripting 2 07-09-2007 04:01 AM
Assigning output of command to a variable oma04 Shell Programming and Scripting 5 06-27-2006 12:11 PM
redirect command output to variable hugow UNIX for Dummies Questions & Answers 1 06-22-2005 06:43 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-14-2004
videsh77 videsh77 is offline
Registered User
  
 

Join Date: Aug 2004
Posts: 144
Question Command output to a variable.

With cut -c 8-13 myfile,

I am getting some numeric value.
In my shell script I am trying to assign something like this,
var=cut -c 8-13 myfile
But at the time of execution I am getting -c is not found.
If I dont assign, then script executes well.

Can we not simply use the value from one command output into the
variable in shell script?
  #2 (permalink)  
Old 12-14-2004
RTM's Avatar
RTM RTM is offline Forum Advisor  
Hog Hunter
  
 

Join Date: Apr 2002
Location: On my motorcycle
Posts: 3,039
Instead of
var=cut -c 8-13 myfile
try
var=`cut -c 8-13 myfile`
  #3 (permalink)  
Old 12-14-2004
videsh77 videsh77 is offline
Registered User
  
 

Join Date: Aug 2004
Posts: 144
Meanwhile I started with awk. So on the similar lines I am trying to evaluate
awk o/p to var.

start=2
len=10
var=`awk <"$1""myfile" '{print substr($0,$start,$len)}'`
echo $var

After executing, I am getting awk error.

If at the place of $start & $len, I put 2 & 10 resp, it works.

Where am I making a mistake?
  #4 (permalink)  
Old 12-14-2004
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
  
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
What exactly is this line
Code:
var=`awk <"$1""myfile" '{print substr($0,$start,$len)}'`
supposed to be achieving?

Without knowing exactly what your problem is, something like
Code:
var=`awk -vs=$start -vl=$length '{print substr($0,s,l)}' filename`
may be more appropriate.

Cheers
ZB
  #5 (permalink)  
Old 12-15-2004
videsh77 videsh77 is offline
Registered User
  
 

Join Date: Aug 2004
Posts: 144
Thumbs up

Zazzybob

Yes,
that works. But let me explain what was my thought process behind prev command.

awk < filename - I understand as, file 'filename' is an input to awk. $1 param consists
dir loc where the file is residing.

In my fixed width file, I have these numeric values. Those I am retrieving with
substring.
  #6 (permalink)  
Old 12-15-2004
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
  
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
I know that that kind of redirection will work, but it's unneccessary - you can just take the filename onto the end of the command.

Cheers
ZB
  #7 (permalink)  
Old 12-15-2004
videsh77 videsh77 is offline
Registered User
  
 

Join Date: Aug 2004
Posts: 144
Also I would like to get rid of leading zeros, after I read a number from fixed width
file.
Say, 00000015 should be read as 15.

How would I achieve that?
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:54 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0