Sponsored Content
Top Forums Shell Programming and Scripting Appending different columns of multiple files in awk Post 303005737 by RavinderSingh13 on Sunday 22nd of October 2017 01:26:00 PM
Old 10-22-2017
Hello looney,

Could you please try following approach too(with function) and let me know if this helps you.
Code:
awk -F"|" '
function print_val(var){
  num=split(var, array,",");
  for(i=1;i<=num;i++){
    printf("%s%s",$array[i],i==num?RS:"|")
}
}
BEGIN{
  print "col1|col2"
}
FNR==1{
  ++file;
  next
}
file==1{
  print_val("1,2");
  nextfile
}
file==2{
  print_val("2,3");
  nextfile
}
file==3{
  print_val("3,2");
  nextfile
}
'   Input_file1  Input_file2  Input_file3

Output will be as follows.
Code:
col1|col2
a|1
3|C
D|4

You need to pass number of fields which you want to print to function named eg--> print_val("1,3") and it should do the trick, since you want to read only very first line of each Input_file so I have used nextfile so that it will NOT go further for Input_file reading and will switch to next Input_file, moreover you need NOT to create many variables in case number of Input_file(s) are too much(tested this in GNU awk).

Thanks,
R. Singh
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk 3 files to one based on multiple columns

Hi all, I have three files, one is a navigation file, one is a depth file and one is a file containing the measured field of gravity. The formats of the files are; navigation file: 2006 320 17 39 0 0 *nav 21.31542 -157.887 2006 320 17 39 10 0 *nav 21.31542 -157.887 2006 320 17 39 20 0... (2 Replies)
Discussion started by: andrealphus
2 Replies

2. Shell Programming and Scripting

appending several columns with awk and paste

Hello, I am trying to solve for a couple of hours now the following problem: I have n files and would like to add the third column of each file to a new file: temp1.txt 1 2 3 1 2 3 1 2 3 temp2.txt 1 2 4 1 2 4 1 2 4 1 2 4 temp3.txt (2 Replies)
Discussion started by: creamcheese
2 Replies

3. UNIX for Dummies Questions & Answers

Extracting columns from multiple files with awk

hi everyone! I already posted it in scripts, I'm sorry, it's doubled I'd like to extract a single column from 5 different files and put them together in an output file. I saw a similar question for 2 input files, and the line of code workd very well, the code is: awk 'NR==FNR{a=$2; next}... (1 Reply)
Discussion started by: orcaja
1 Replies

4. Shell Programming and Scripting

Extracting columns from multiple files with awk

hi everyone! I'd like to extract a single column from 5 different files and put them together in an output file. I saw a similar question for 2 input files, and the line of code workd very well, the code is: awk 'NR==FNR{a=$2; next} {print a, $2}' file1 file2 I added the file3, file4 and... (10 Replies)
Discussion started by: orcaja
10 Replies

5. Shell Programming and Scripting

Appending columns of two files using shell script

Hi, I am using ksh, I want to read one csv file and append the columns of another file with new column. My input file: col1,col2 --------- siri,886 satya,890 priya,850 Another file with the below date:(test.csv) col3 ----- 321 333 442 (1 Reply)
Discussion started by: siri_886
1 Replies

6. UNIX for Dummies Questions & Answers

Appending columns at the end of output using awk/sed

Hi , I have the below ouput, =====gopi===== assasassaa adsadsadsdsada asdsadsadasdsa sadasdsadsd =====kannan=== asdasdasd sadasddsaasd adasdd =====hbk=== asasasssa .... .. I want the output like as below, not able paste here correctly. (2 Replies)
Discussion started by: eeegopikannan
2 Replies

7. Shell Programming and Scripting

awk script to split file into multiple files based on many columns

So I have a space delimited file that I'd like to split into multiple files based on multiple column values. This is what my data looks like 1bc9A02 1 10 1000 FTDLNLVQALRQFLWSFRLPGEAQKIDRMMEAFAQRYCQCNNGVFQSTDTCYVLSFAIIMLNTSLHNPNVKDKPTVERFIAMNRGINDGGDLPEELLRNLYESIKNEPFKIPELEHHHHHH 1ku1A02 1 10... (9 Replies)
Discussion started by: viored
9 Replies

8. Shell Programming and Scripting

awk arrays comparing multiple columns across two files.

Hi, I'm trying to use awk arrays to compare values across two files based on multiple columns. I've attempted to load file 2 into an array and compare with values in file 1, but success has been absent. If anyone has any suggestions (and I'm not even sure if my script so far is on the right lines)... (4 Replies)
Discussion started by: hubleo
4 Replies

9. UNIX for Dummies Questions & Answers

Appending multiple files

I am trying to append multiple files in a directory cat /a/file1.txt /a/file2.txt /a/file3.txt /a/file4.txt > /a/file.txt Except file2 every other file is appending. I interchanged file names and ran the command. Whatever file repeating in the second position is missing in output... (6 Replies)
Discussion started by: eskay
6 Replies

10. Shell Programming and Scripting

Appending multiple files through UNIX

Hi All, I have requirement where multiple csv files are present in a directory and each file contains a header.I need to append the contents of all the files into one file by removing header. Once the data is merged in one file ,I need to remove duplicates on nth column to find out distinct... (2 Replies)
Discussion started by: STCET22
2 Replies
MYSQLI_STMT.BIND_RESULT(3)						 1						MYSQLI_STMT.BIND_RESULT(3)

mysqli_stmt::bind_result - Binds variables to a prepared statement for result storage

       Object oriented style

SYNOPSIS
bool mysqli_stmt::bind_result (mixed &$var1, [mixed &$...]) DESCRIPTION
Procedural style bool mysqli_stmt_bind_result (mysqli_stmt $stmt, mixed &$var1, [mixed &$...]) Binds columns in the result set to variables. When mysqli_stmt_fetch(3) is called to fetch data, the MySQL client/server protocol places the data for the bound columns into the speci- fied variables $var1, .... Note Note that all columns must be bound after mysqli_stmt_execute(3) and prior to calling mysqli_stmt_fetch(3). Depending on column types bound variables can silently change to the corresponding PHP type. A column can be bound or rebound at any time, even after a result set has been partially retrieved. The new binding takes effect the next time mysqli_stmt_fetch(3) is called. PARAMETERS
o $ stmt -Procedural style only: A statement identifier returned by mysqli_stmt_init(3). o $var1 - The variable to be bound. RETURN VALUES
Returns TRUE on success or FALSE on failure. EXAMPLES
Example #1 Object oriented style <?php $mysqli = new mysqli("localhost", "my_user", "my_password", "world"); if (mysqli_connect_errno()) { printf("Connect failed: %s ", mysqli_connect_error()); exit(); } /* prepare statement */ if ($stmt = $mysqli->prepare("SELECT Code, Name FROM Country ORDER BY Name LIMIT 5")) { $stmt->execute(); /* bind variables to prepared statement */ $stmt->bind_result($col1, $col2); /* fetch values */ while ($stmt->fetch()) { printf("%s %s ", $col1, $col2); } /* close statement */ $stmt->close(); } /* close connection */ $mysqli->close(); ?> Example #2 Procedural style <?php $link = mysqli_connect("localhost", "my_user", "my_password", "world"); /* check connection */ if (!$link) { printf("Connect failed: %s ", mysqli_connect_error()); exit(); } /* prepare statement */ if ($stmt = mysqli_prepare($link, "SELECT Code, Name FROM Country ORDER BY Name LIMIT 5")) { mysqli_stmt_execute($stmt); /* bind variables to prepared statement */ mysqli_stmt_bind_result($stmt, $col1, $col2); /* fetch values */ while (mysqli_stmt_fetch($stmt)) { printf("%s %s ", $col1, $col2); } /* close statement */ mysqli_stmt_close($stmt); } /* close connection */ mysqli_close($link); ?> The above examples will output: AFG Afghanistan ALB Albania DZA Algeria ASM American Samoa AND Andorra SEE ALSO
mysqli_stmt_get_result(3), mysqli_stmt_bind_param(3), mysqli_stmt_execute(3), mysqli_stmt_fetch(3), mysqli_prepare(3), mysqli_stmt_pre- pare(3), mysqli_stmt_init(3), mysqli_stmt_errno(3), mysqli_stmt_error(3). PHP Documentation Group MYSQLI_STMT.BIND_RESULT(3)
All times are GMT -4. The time now is 09:31 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy