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 o/p of awk to a variable c2b2 Shell Programming and Scripting 8 04-01-2009 07:50 AM
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
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
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-19-2009
sunpraveen's Avatar
sunpraveen sunpraveen is offline
Registered User
  
 

Join Date: Jan 2009
Location: Pruthvi
Posts: 85
Question Unable to assign value to variable using awk coz of whitespace in value

Unix gurus,

I have a file as below, which is basically the result set obtained from a sql query on an Oracle database.

Code:
ID           PROG_NAME      USER_PROG_NAME
-------- --------------- ----------------------------------------
33045      INCOIN             Import Items
42690      POXPOPDOI       Import Standard Purchase Orders
I want to assign the results to variables. Something like:

Code:

cat filename | tail +4 | while read line ## tail +4 because the first 3 lines are the heading!
do
id=$(echo ${line} | awk '{print $1}')
prog_name=$(echo ${line} | awk '{print $2}')
user_prog_name=$(echo ${line} | awk '{print $3}')


However, I am unable to assign the 3rd column to variable user_prog_name since it has spaces.

How can I overcome this problem?

TIA,

Regards,

Praveen
  #2 (permalink)  
Old 03-19-2009
chengamsiva chengamsiva is offline
Registered User
  
 

Join Date: Mar 2009
Location: bangalore,india
Posts: 2
Add another variable

Replace
your line :user_prog_name=$(echo ${line} | awk '{print $3}')

by :user_prog_name=$(echo ${line} | awk '{print $3 $4 $5 $6}')
(or)
user_prog_name=$(echo ${line} | awk '{print $3 " " $4 " " $5 " " $6}') to provide single space " " between $3,$4,$5 and $6 when the values are store in variable.


I am post this script after check.
It is running fine.
concept:
here we join the remaining last n coloumn and save it in a variable.
byebye..
  #3 (permalink)  
Old 03-19-2009
sunpraveen's Avatar
sunpraveen sunpraveen is offline
Registered User
  
 

Join Date: Jan 2009
Location: Pruthvi
Posts: 85
Quote:
Originally Posted by chengamsiva View Post
Replace
your line :user_prog_name=$(echo ${line} | awk '{print $3}')

by :user_prog_name=$(echo ${line} | awk '{print $3 $4 $5 $6}')
(or)
user_prog_name=$(echo ${line} | awk '{print $3 " " $4 " " $5 " " $6}') to provide single space " " between $3,$4,$5 and $6 when the values are store in variable.
Your solution works if the number of words in the column USER_PROG_NAME is know. But it is NOT.

It can contain 1, 2 or even 5 words with spaces seperating them.

So basically what I want is: from $3 to end of line.

How exactly can I achieve it?

TIA,

Regards,

Praveen
  #4 (permalink)  
Old 03-23-2009
chengamsiva chengamsiva is offline
Registered User
  
 

Join Date: Mar 2009
Location: bangalore,india
Posts: 2
Hi,
Now Replace

your line :user_prog_name=$(echo ${line} | awk '{print $3}')
by :user_prog_name=$(echo ${line} | awk '{for (a = 3; a <= NF; a++) print $a }')
Sponsored Links
Closed Thread

Bookmarks

Tags
awk, whitespace

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:10 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
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