Sponsored Content
Full Discussion: Add a column to a file
Top Forums UNIX for Dummies Questions & Answers Add a column to a file Post 302940876 by bobo on Thursday 9th of April 2015 02:35:55 PM
Old 04-09-2015
file d1 contain:

Code:
1       A
2       B
3       C
4       D
5       E
6       F
7       G
8       H
9       I

I have this script: test.script

Code:
#!/bin/ksh

DAY=`date +%y%m%d`
H=`date +%H`
D=`date +%d`
cat d1 | while read line
do
a=$(echo $line | awk '{print $1}')
n=$(echo $line | awk '{print $2}')
awk '{print $0, $n}' d1 >> d2
done

./test.script 

cat d2

1       A 1     A
2       B 2     B
3       C 3     C
4       D 4     D
5       E 5     E
6       F 6     F
7       G 7     G
8       H 8     H
9       I 9     I
1       A 1     A
2       B 2     B
3       C 3     C
4       D 4     D
5       E 5     E
6       F 6     F
7       G 7     G
8       H 8     H
9       I 9     I
1       A 1     A
2       B 2     B
3       C 3     C
4       D 4     D
5       E 5     E
6       F 6     F
7       G 7     G
8       H 8     H
9       I 9     I
1       A 1     A
2       B 2     B
3       C 3     C
4       D 4     D
5       E 5     E
6       F 6     F
7       G 7     G
8       H 8     H
9       I 9     I
1       A 1     A
2       B 2     B
3       C 3     C
4       D 4     D
5       E 5     E
6       F 6     F
7       G 7     G
8       H 8     H
9       I 9     I
1       A 1     A
2       B 2     B
3       C 3     C
4       D 4     D
5       E 5     E
6       F 6     F
7       G 7     G
8       H 8     H
9       I 9     I
1       A 1     A
2       B 2     B
3       C 3     C
4       D 4     D
5       E 5     E
6       F 6     F
7       G 7     G
8       H 8     H
9       I 9     I
1       A 1     A
2       B 2     B
3       C 3     C
4       D 4     D
5       E 5     E
6       F 6     F
7       G 7     G
8       H 8     H
9       I 9     I
1       A 1     A
2       B 2     B
3       C 3     C
4       D 4     D
5       E 5     E
6       F 6     F
7       G 7     G
8       H 8     H
9       I 9     I
1       A 1     A
2       B 2     B
3       C 3     C
4       D 4     D
5       E 5     E
6       F 6     F
7       G 7     G
8       H 8     H
9       I 9     I

I expect:
Code:
1	A	A
2	B	A
3	C	A
4	D	A
5	E	A
6	F	A
7	G	A
8	H	A
9	I	A
1	A	B
2	B	B
3	C	B
4	D	B
5	E	B
6	F	B
7	G	B
8	H	B
9	I	B
1	A	C
2	B	C
3	C	C
4	D	C
5	E	C
6	F	C
7	G	C
8	H	C
9	I	C

on and on.....

Given that:
Code:
awk '{print $0, "A"}' d1

1	A	A
2	B	A
3	C	A
4	D	A
5	E	A
6	F	A
7	G	A
8	H	A
9	I	A

Thanks!

Last edited by Don Cragun; 04-09-2015 at 03:47 PM.. Reason: Add CODE and ICODE tags, again.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

how to add a new column in an existing file

Hi guys, Please help me if u have some solution. I have a file with three columns separated by ':' - INPUT_FILE C416722_2 : calin Dirigent : Dirigent AC4174_6 : Jac : cal_co TC4260_5 : [no : lin kite BC426302_1 : [no : calin Dirigent lin JC426540_3 : lin Pymo_bin : calin TC428_3 : no7... (4 Replies)
Discussion started by: sam_2921
4 Replies

2. Shell Programming and Scripting

How to add a particular column alone in a file

Hi I have file which contains 5 coulmns i need to add the fifth column value and put it in the desired location in the same column. Here is the sample file.. ashop0004 SQL- 06/14/2009 06/14/2009 00:04:28 SUM ashop0004 SQL- 06/14/2009 06/14/2009 00:00:37 ... (22 Replies)
Discussion started by: cutechaps
22 Replies

3. Shell Programming and Scripting

Need to add letters to a column and add in a new column subtracting from another column

So I have this input 1 10327 rs112750067 T C . PASS DP=65;AF=0.208;CB=BC,NCBI 1 10469 rs117577454 C G . PASS DP=2055;AF=0.020;CB=UM,BC,NCBI 1 10492 rs55998931 C T . PASS DP=231;AF=0.167;CB=BC,NCBI 1 10583 rs58108140 G A ... (3 Replies)
Discussion started by: kellywilliams
3 Replies

4. Shell Programming and Scripting

Add column to a file

Hola, How can I add a column to a existing file?? PS: The column which should be added need to be the first column and it will be a parameter from the script. Example: 1 name1 2 name2 3 name3 4 name3 Need to add parameter $file as a first column. $file is a file name with time... (6 Replies)
Discussion started by: Olivia
6 Replies

5. Shell Programming and Scripting

Add column info from one file to larger second file

Hi, long time reader, first time poster. I've done some searching so please if this is a repeated post excuse the duplicate, but what I have are two files roughly like so: File 1: A W B X C Y D Z File 2: A 1 C 2 D 3 And what I would like to get out is... (4 Replies)
Discussion started by: wallysb01
4 Replies

6. Shell Programming and Scripting

How to add a column from other file?

Dear all, Lets say, I've a file a.txt containing two columns, like a1 b1 .. .. .. .. and another file b.txt containg two columns, like a1 c1 .. .. .. .. I need to put c1 column from b.txt file to the a.txt file. So, the output should be a1 b1 c1 .. .. .. .. ... (4 Replies)
Discussion started by: mkg
4 Replies

7. Shell Programming and Scripting

Help with add existing file name as new data column in new output file

Input File 1 cat S1.txt MI0043 2731 miR-1 Input File 2 cat S4.txt MI006 310 CiR-1 MI057 10 CiR-24 MI750 5 CiR-24 Desired Output File 1 cat S1.txt.out MI0043 2731 miR-1 S1.txt Desired Output File 2 cat S4.txt.out MI006 310 CiR-1 S4.txt (3 Replies)
Discussion started by: perl_beginner
3 Replies

8. Shell Programming and Scripting

Add a new column to file

I have file like this b,c 10,20 30,40 50,60 Now I want to add a new column a with fixed values for all the rows a,b,c 60,10,20 60,30,40 60,50,60 Please let me know how can we do this in unix. (4 Replies)
Discussion started by: weknowd
4 Replies

9. Shell Programming and Scripting

Add new column in the file

Hi Team I have file as below empno,ename,sal 123,smith,1000 124,adams,2000 Required output: Using AWK empno,ename,sal,deptno 123,smith,1000 124,adams,2000 Thanks, Murali (5 Replies)
Discussion started by: bmk
5 Replies

10. UNIX for Beginners Questions & Answers

Add new column from another file

Hi, I have 2 files. file1 contains by lines: hash:salt:id file2 contains by lines: username:hash:salt I would like to add a new coloumn (id) form file1 to file2. The new file should contains: username:hash:salt:id Note: file1 contains less rows than file2. I tried paste -d':' file1... (12 Replies)
Discussion started by: freeroute
12 Replies
MYSQL_FETCH_ASSOC(3)							 1						      MYSQL_FETCH_ASSOC(3)

mysql_fetch_assoc - Fetch a result row as an associative array

SYNOPSIS
Warning This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include: omysqli_fetch_assoc(3) o PDOStatement::fetch array mysql_fetch_assoc (resource $result) DESCRIPTION
Returns an associative array that corresponds to the fetched row and moves the internal data pointer ahead. mysql_fetch_assoc(3) is equiv- alent to calling mysql_fetch_array(3) with MYSQL_ASSOC for the optional second parameter. It only returns an associative array. o $ result -The result resource that is being evaluated. This result comes from a call to mysql_query(3). Returns an associative array of strings that corresponds to the fetched row, or FALSE if there are no more rows. If two or more columns of the result have the same field names, the last column will take precedence. To access the other column(s) of the same name, you either need to access the result with numeric indices by using mysql_fetch_row(3) or add alias names. See the example at the mysql_fetch_array(3) description about aliases. Example #1 An expanded mysql_fetch_assoc(3) example <?php $conn = mysql_connect("localhost", "mysql_user", "mysql_password"); if (!$conn) { echo "Unable to connect to DB: " . mysql_error(); exit; } if (!mysql_select_db("mydbname")) { echo "Unable to select mydbname: " . mysql_error(); exit; } $sql = "SELECT id as userid, fullname, userstatus FROM sometable WHERE userstatus = 1"; $result = mysql_query($sql); if (!$result) { echo "Could not successfully run query ($sql) from DB: " . mysql_error(); exit; } if (mysql_num_rows($result) == 0) { echo "No rows found, nothing to print so am exiting"; exit; } // While a row of data exists, put that row in $row as an associative array // Note: If you're expecting just one row, no need to use a loop // Note: If you put extract($row); inside the following loop, you'll // then create $userid, $fullname, and $userstatus while ($row = mysql_fetch_assoc($result)) { echo $row["userid"]; echo $row["fullname"]; echo $row["userstatus"]; } mysql_free_result($result); ?> Note Performance An important thing to note is that using mysql_fetch_assoc(3) is not significantly slower than using mysql_fetch_row(3), while it provides a significant added value. Note Field names returned by this function are case-sensitive. Note This function sets NULL fields to the PHP NULL value. mysql_fetch_row(3), mysql_fetch_array(3), mysql_data_seek(3), mysql_query(3), mysql_error(3). PHP Documentation Group MYSQL_FETCH_ASSOC(3)
All times are GMT -4. The time now is 06:59 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy