Sponsored Content
Full Discussion: Printing $values using awk
Top Forums Shell Programming and Scripting Printing $values using awk Post 302960401 by Don Cragun on Sunday 15th of November 2015 04:19:40 AM
Old 11-15-2015
Quote:
Originally Posted by gvkumar25
Hi Don,

Thanks For your reply.

Sorry for the confusion. What I need is all the fields which are there in ctrl file and in the same order which are there in control file.

Code:
source file
ename|eid|emanger|esal|city|deptid|deptname
peter|10|larry|$2000|melbourne|20|electronics
shaun|11|Peter|$1000|sydney|20|electronics

ctrl file
--------
ename
eid
esal
ecity

o/p -- we need output only the columns which are there in control file and column order should be same like control file.

ename|eid|esal|ecity
peter|10|$2000|melbourne
shaun|11|$1000|sydney

You might want to try something like the following:
Code:
awk '
BEGIN {	FS = OFS = "|"
}
FNR == NR {
	hd[$0] = ++hc
	next
}
FNR == 1 {
	for(i = 1; i <= NF; i++)
		if($i in hd)
			of[hd[$i]] = i
}
{	for(i = 1; i <= hc; i++)
		printf("%s%s", $of[i], i == hc ? ORS : OFS)
}' ctrl2 source

but with your latest sample data as shown in post #5, it will give you output similar to:
Code:
ename|eid|esal|awk: illegal field $(), name "4"
 input record number 1, file source
 source line number 14

because the 4th line in the 2nd version of your control file (ecity) does not appear as a header in your source file (it is city instead).

If we change the control file to:
Code:
ename
eid
esal
city

it produces the output:
Code:
ename|eid|esal|city
peter|10|$2000|melbourne
shaun|11|$1000|sydney

which seems like logical output for the input you provided although it doesn't match what you said you wanted (with ecity instead of city again).

You haven't told us what operating system or shell you're using. If you want to try this script on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
This User Gave Thanks to Don Cragun For This Post:
 

10 More Discussions You Might Find Interesting

1. Programming

printing all array values using dbx 7.2.1

how do we print the entire contents of arrays in dbx ? Ive tried using print x to print values 1 to 5 of the array x, however dbx complains and doesnt allow this help is much appreciated (1 Reply)
Discussion started by: JamesGoh
1 Replies

2. UNIX for Advanced & Expert Users

Printing defaulted values

I have written a phyton script that accepts command line arguments. I am setting defaults for some of them if the user does not specify them. However I want to print the user values and the defaulted values seperately. However, once I set the default values, then I cannot use if... (0 Replies)
Discussion started by: kristinu
0 Replies

3. Shell Programming and Scripting

printing two values with TAB in between

Dear friends, I want to print variables' values in certain format where space between two values of variables is "a tab" I tried where I provided "tab" between two varibales. But when it print values on screen its giving me output without spaces in two values. Request you to help me in... (7 Replies)
Discussion started by: anushree.a
7 Replies

4. Programming

Printing float values in C

Hi, I have small problem to print float value in the fallowing code float Cx, W,f=250000, Cr=92.00,pi=3.14; W=2*pi*f; Cx = 1/W.Cr; //Cx value will be come around like 7.07E-9. printf("capacitance value: %.10f",Cx); I am trying to print Cx value using above code but it was not... (3 Replies)
Discussion started by: veerubiji
3 Replies

5. UNIX for Dummies Questions & Answers

Printing all the values in the middle of two columns

Hi, I have a tab delimited text file with three columns: Input: 1 25734 25737 1 32719 32724 1 59339 59342 1 59512 59513 1 621740 621745 For each row of the text file I want to print out all the values between the second and third columns, including them. The... (3 Replies)
Discussion started by: evelibertine
3 Replies

6. Programming

Printing values from a class

I have a class and want to print values in MOD using L = new Layer* ; How can I print the values in MOD using this object L??? class Layer { public : Model* MODP; Model* MODS; (1 Reply)
Discussion started by: kristinu
1 Replies

7. Shell Programming and Scripting

Awk: Comparing arguments with in line values of file and printing the result

I need to develop a script where I will take two date arguments as parameter date1 and date2 which will in format YYYYMM. Below is the input file say sample.txt. sample.txt will have certain blocks starting with P1. Each block will have a value 118,1:TIMESTAMP. I need to compare the... (7 Replies)
Discussion started by: garvit184
7 Replies

8. UNIX for Dummies Questions & Answers

Printing the intermediate integer values

Dear Help, I have an input file which looks like - 121 300 122 345 124 567 127 234 $1 has 125 and 126 missing. How can I output those missing values? Thanks (6 Replies)
Discussion started by: Indra2011
6 Replies

9. Shell Programming and Scripting

Printing null values in awk

Hi, I have a csv file with given details abc.txt 123,ra,point,,there 232,ba,points,home,pheer I want to get those values and store them in different variables: Code: while read line do echo $line |awk -F"," '{print $1" "$2" "$3" "$4" "$5"}'|read dbt_acct val_dt crncy AMT... (11 Replies)
Discussion started by: rahulsk
11 Replies

10. Shell Programming and Scripting

Array not printing values if used in a loop

Hello! I'm making an English to Morse Code translator and I was able to mostly get it all working by looking through older posts here; however, I have one small problem. When I run it it's just printing spaces for where the characters should be. It runs the right amount of times, and if I try... (3 Replies)
Discussion started by: arcoleman10
3 Replies
All times are GMT -4. The time now is 01:18 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy