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
assign a value to a variable Shivdatta Shell Programming and Scripting 10 01-17-2007 07:40 AM
assign value to variable using AWK HAA Shell Programming and Scripting 4 12-06-2006 11:43 AM
assign a value to variable markjason Shell Programming and Scripting 3 10-10-2006 02:05 PM
hot to assign output to a variable walnut Shell Programming and Scripting 1 02-22-2006 09:19 PM
assign to variable AkumaTay UNIX for Dummies Questions & Answers 1 05-18-2002 02:11 PM

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 02-01-2007
c2b2 c2b2 is offline
Registered User
  
 

Join Date: Dec 2006
Posts: 29
Assign o/p of awk to a variable

Hi UNIX gurus,

I am facing a typical problem while assigining while assigining output of awk to a variable.

I have a fixed length file say myinputfile.txt

When I allow the value/output of an awk to be redirected to a file, it works fine. i.e.
awk "/^.{232}$acctNum/ { printf(\"%s;Y;Account#\n\",substr(\$0,241,1)) ; exit} " myinputfile.txt >> mylogfile.txt

However, when I try to assign the the value of same to a variable, and then echo the variable, it displays blank.i.e.
myvariable=`awk "/^.{232}$acctNum/ { printf(\"%s;Y;Account#\n\",substr(\$0,241,1)) ; exit} " myinputfile.txt`

Can somebody please help.
  #2 (permalink)  
Old 02-01-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Is this $acctNum a variable or pattern?
  #3 (permalink)  
Old 02-01-2007
c2b2 c2b2 is offline
Registered User
  
 

Join Date: Dec 2006
Posts: 29
It is a variable.
  #4 (permalink)  
Old 02-01-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Are you sure that you are getting output from awk command?
can you show your input file?
  #5 (permalink)  
Old 02-01-2007
c2b2 c2b2 is offline
Registered User
  
 

Join Date: Dec 2006
Posts: 29
Only awk is working fine. However, while assigning the same to variable, it is not working.

My Sample Inputfile is as shown below:

MyAdd1 MyAdd2 MyAdd3 MyAdd4 MyAdd5 Sachin Tendulkar 222 222-2222 11111111111111A0000111122223333XYX
YourAdd1 YourAdd2 YourAdd3 YourAdd4 YourAdd5 Rahul Dravid 111 121-5555 22222222222222B0123456789123456XYX

(In above example, spaces are getting truncated in this forum)
Basically, position 233-240 denotes account number. Also, by substring, I am trying to extract 241 character.
  #6 (permalink)  
Old 02-01-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Add another backslash before $0
Code:
myvariable=`awk "/^.{232}$acctNum/ { printf(\"%s;Y;Account#\n\",substr(\\$0,241,1)) ; exit} " myinputfile.txt`
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 09:13 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