awk command suggestions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk command suggestions
# 1  
Old 02-23-2015
Linux awk command suggestions

I've defined the order of elements which needs to be print in a order in a variable. please let me know how can I use it in awk command
Code:
temp="$1,$2,$26,$27,$28,$29,$30,$31,$32,$33,$34,$35,$36,$37,$38,$39,$40,$41,$42,$43,$44,$45,$46,$47,$48,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25"

awk -v values="${temp}" 'BEGIN{FS=OFS="|"}{print values }'  <input file>
> outputfile.txt

This command printing temp variable not the values of those positions. please help me out

Thanks,
Brahma
# 2  
Old 02-23-2015
You would need single quotes, because double quotes will let the shell expand the dollar signs.

But unfortunately it cannot be done that way. You would either need to hard code it into awk:
Code:
awk 'BEGIN{FS=OFS="|"}{print $1,$2,$26,$27,$28,$29,$30,$31,$32,$33,$34,\
$35,$36,$37,$38,$39,$40,$41,$42,$43,$44,$45,$46,$47,$48,$3,$4,$5,$6,$7,\
$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25}' file

or try something like:
Code:
temp=1,2,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,\
46,47,48,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25

awk -v values="${temp}" '
  BEGIN {
    FS="|"
    n=split(values,F,/,/)
  }
  {
    for(i=1; i<n; i++)
      printf "%s|", $F[i]
    print $F[n]
  }
' file


Last edited by Scrutinizer; 02-23-2015 at 11:22 PM..
# 3  
Old 02-24-2015
Considering Scrutinizer's note on single quotes vs. double quotes when defining the variable, you can use your temp definition together with his second example by adding gsub ("\$", "", values); :
Code:
awk -v values="${temp}" '
         BEGIN {gsub ("\$", "", values);
                n=split(values,F,/,/)
               }

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Suggestions for command line parsing

Hi all I need to put a command line parser together to parse numeric fields and ranges passed to a script. I'm looking for a bash function that is as elegant and simple as possible. So the input would be of the following form - 1,2,8-12 This would return - 1,2,8,9,10,11,12 Input can... (7 Replies)
Discussion started by: steadyonabix
7 Replies

2. UNIX for Dummies Questions & Answers

Suggestions for the interview

Hi guys, i'm undergoing a traning in solaris administration and i request if any one have an idea on the interview questions on solaris. thank you. (3 Replies)
Discussion started by: 038karthik
3 Replies

3. Solaris

Suggestions for new T5140

I've been busy and fell behind on Sun/Oracle. Forgive me if too basic. I welcome brief, cryptic, or advanced replies. I also welcome noobie information since I may have no clue what's up at the moment. Problem statement: I inherited a computer to set up. I would rather not figure out 8 months... (1 Reply)
Discussion started by: Nevyn
1 Replies

4. UNIX for Advanced & Expert Users

Need suggestions.......

Hello there....i am a final year comp science student.......i am thinking of doing my project on unix platform......which one do u suggest?thanx in advance... (3 Replies)
Discussion started by: theprasad1990
3 Replies

5. Shell Programming and Scripting

awk command in script gives error while same awk command at prompt runs fine: Why?

Hello all, Here is what my bash script does: sums number columns, saves the tot in new column, outputs if tot >= threshold val: > cat getnon0file.sh #!/bin/bash this="getnon0file.sh" USAGE=$this" InFile="xyz.38" Min="0.05" # awk '{sum=0; for(n=2; n<=NF; n++){sum+=$n};... (4 Replies)
Discussion started by: catalys
4 Replies

6. Programming

C Beginner Looking For Suggestions

A few weeks ago at the recommendation of people I trust, I bought and started reading Kernighan and Ritchie's (K&R) C Programming Language. For one thing, it's damn thin compared to the O'Reilly Practical C I just finished last month. It covers generally the same stuff but in a much more... (9 Replies)
Discussion started by: deckard
9 Replies

7. UNIX for Advanced & Expert Users

Looking for Suggestions...

We run WebSphere and by default it wants to install everything under /usr. While I can understand the default (everyone has a /usr) I would like to move this over to a dedicated volume group called apps and then setup my lv's and fs's here. Our WebSphere Admin doesn't like this because apparently... (1 Reply)
Discussion started by: scottsl
1 Replies

8. UNIX for Dummies Questions & Answers

Backup suggestions

The current backup procedure we using a tar command in linux. The files are stored in one partition in different folders. The docs stores in day wise folders like ex: /usr/data/xyz/20050129, /usr/data/xyz/20050130 .............etc We using tar & gzip command to take backup everyday. The backup... (3 Replies)
Discussion started by: bache_gowda
3 Replies

9. Filesystems, Disks and Memory

Backup Suggestions.

Hi all, Have two SCO 5.0.5 systems and one Slackware(joy?) system. I've been asked to backup all three systems onto a newly acquired AIT Tape drive that we've installed on one of the SCO boxes. Using the existing cpio backup script on the one SCO works a treat and is really fast (which is... (2 Replies)
Discussion started by: Cameron
2 Replies

10. UNIX for Dummies Questions & Answers

Suggestions on where to begin?

I have been a student at Hendrix Institute for about a year now. My term is comming to an end by the end of december. I have learned varios computer programs for web development that include Flash 5 and Dreamweaver. Actionscripting, Javascript and Database development with Access was all... (4 Replies)
Discussion started by: andrew25008
4 Replies
Login or Register to Ask a Question