LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-28-2014, 05:14 PM   #1
anandg111
Member
 
Registered: Jan 2012
Posts: 56

Rep: Reputation: Disabled
Using shell veriable with %s in awk


I want to use shell variable with %s in awk

I have records like below.

2|Nishant|Mane|ASE|WAS|16/08/2011
3|Abhijit|Joshi|SSE|Windows|22/05/2012

I want to format the output like below
Code:
2 | Nishant | Mane  |
3 | Abhijit | Joshi |
max value ="Nishant" = 7
max lenght of the filed will be like -%7+1s

awk must be like..
awk -F"|" '// { printf "| %3d | %-{$maxlength_fname}s | %-{$maxlength_lname}s |\n", $1,$2,$3 }' filename

please suggest

Last edited by anandg111; 01-28-2014 at 05:17 PM.
 
Old 01-28-2014, 05:25 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Shell variables are passed to awk by means of the -v option. Then just take the variable names off the double quotes in the format string:
Code:
awk -v maxlength_fname=$maxlength_fname -v maxlength_lname=$maxlength_lname -F"|" '{printf "| %3d | %-"maxlength_fname"s | %-"maxlength_lname"s |\n", $1,$2,$3 }' file
 
1 members found this post helpful.
  


Reply

Tags
awk


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] awk: how can I assign value to a shell variable inside awk? quanba Programming 6 03-23-2010 03:18 AM
passing a veriable in a command substitution dwj79 Programming 4 04-02-2009 03:46 AM
awk and shell variable markux Linux - Server 6 01-19-2009 04:54 AM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 05:11 PM
Two C question: definition of veriable and powerplane Programming 4 05-01-2003 11:59 PM


All times are GMT -5. The time now is 04:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration