Printing $values using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Printing $values using awk
# 1  
Old 11-13-2015
Printing $values using awk

Hi All

I had requirement where I need to re-order columns in a file by using a control file.
Code:
here is the ctrl file
c1
c2
c3

source file
c3 | c1 | c2
a | b| c
I should create output file based on the ctrl file columns

o/p should look like this
c1 | c2 | c3
b| c|a

I wrote some logic and got the column positions in the file in to a variable but I am unable to evaluate that positions in awk
Code:
p=`awk -F'|' '
{
  for(i=1;i<=NF;i++)
  {
    if($i == "c2")
    {
      printf("%d",i)
    }
  }
  exit 0
}
' ABC_CA.csv `
x='$'$p

p=`awk -F'|' '
{
  for(i=1;i<=NF;i++)
  {
    if($i == "c1")
    {
      printf("%d",i)
    }
  }
  exit 0
}
' ABC_CA.csv `
x=$x,'$'$p
awk -v row="$x" -FS="|" '{print "'row'" }' NRD_AP_VENDOR_STG_CA.csv > out_file

Here I am unable to print the data

Last edited by gvkumar25; 11-15-2015 at 03:13 AM..
# 2  
Old 11-14-2015
Where does your control file come into play? And, wouldn't you agree that it's way better to work on a short but meaningful sample?

---------- Post updated at 12:00 ---------- Previous update was at 11:12 ----------

Howsoever, try
Code:
awk -F\| '
FNR == NR       {HD[NR] = $0
                 next
                }
FNR == 1        {for (n=1; n<=NF; n++)
                   for (h in HD)  if ($n ~ HD[h])       {COL[h] = n
                                                         break
                                                        }
                }
                {DL = ""
                 for (c=1; c<=NF; c++)  {printf "%s%s", DL, $(COL[c])
                                         DL = FS
                                        }
                 printf "\n"
                }
' ctrl source
 c1 | c2|c3 
 b| c|a

This User Gave Thanks to RudiC For This Post:
# 3  
Old 11-15-2015
Hi Rudic,

Thanks for your reply.

Could you please elaborate the code. I am not an expert.

My requirement
-----------------
We have so many fields in the source file but We may not be using all those fields. We need to construct another file by using a control file. Control file contains what are all the fields which we need to pick from source file. More over the fields which I need may not come in same position in the source file. For more details here is the example
Code:
Today source file

c1|c6|c10|c13|c2|c3|c8|c4|c5|c7|c9|c11|c12|c14|c15
tfh|akb|345|a|344|12|abc|fgh|ert|hj|wer|506002|ertg|asd|234
1|5|6|8|9|10|11|12|13|2|3|4|5|8|9

yesterday's source file

c16|c17|c18|c1|c6|c10|c13|c2|c3|c8|c4|c5|c7|c9|c11|c12|c14|c15
234|123|kbj|tfh|akb|345|a|344|12|abc|fgh|ert|hj|wer|506002|ertg|asd|234
1|5|6|8|9|10|11|12|13|2|3|4|5|8|9|14|18|17|16|15

Out of all this columns we need only the columns which are there in our ctrl file

Ctrl file
--------
c1
c2
c10
c13

Tomorrow if we remove column from control file.my code should build a file without that column

Last edited by gvkumar25; 11-15-2015 at 03:15 AM..
# 4  
Old 11-15-2015
Hi gvkumar25,
Let us be very clear. In post #1 you showed us code that looked for the string c1 and the string c2 in a file with | as a field separator and the following data:
Code:
c3 | c1 | c2

so NO matches should be found because there are extra spaces in your sample data file. And, you said the requirement was to: "I had requirement where I need to re-order columns in a file by using a control file." (Note "re-order columns"; not re-order some columns and discard other columns.) Therefore, RudiC suggested code that searched for a partial match for the strings provided instead of looking for exactly equal strings. That was the only way to get the output you said you wanted from the sample inputs provided.

Now you show us sample input files with no spaces and with data where just looking for headings that contain the strings shown in your control file select several fields.

If you're going to change the sample data and provide a new set of requirements, don't complain if the code provided to meet your original requirements and original data doesn't work with new requirements and different data.
# 5  
Old 11-15-2015
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

# 6  
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:
# 7  
Old 11-15-2015
It is working as expected but can you please explain me the code

Last edited by gvkumar25; 11-15-2015 at 05:54 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question