append a column by concatenating other columns


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers append a column by concatenating other columns
# 1  
Old 01-22-2012
append a column by concatenating other columns

Hi
In a tab delimited file how can I add a column that have values concatenated from all columns.
For example
input.txt
Code:
test1	test2	test3
zz2	mm	uu
pp3	yy	kk
ss2	tt	ll
zz3	mm	uu
pp23	yy	kk
ss3	tt	ll
11e	22	44
33c	22	99

output.txt

Code:
test1	test2	test3  reslt
zz2	mm	uu    zz2_mm_uu
pp3	yy	kk    pp3_yy_kk
ss2	tt	ll     ss2_tt_ll
zz3	mm	uu   zz3_mm_uu
pp23	yy	kk   pp23_yy_kk
11e	22	44   11e_22_44

Thanks
# 2  
Old 01-22-2012
Try:
Code:
awk -vOFS="\t" '{$4=$1"_"$2"_"$3}1' file

# 3  
Old 01-22-2012
Thanks bartus11,
How to change the field name? because with this code, the field name is not as the desired.
Thanks
# 4  
Old 01-23-2012
Modified one-liner provided by bartus11:
Code:
echo -e "`head -1 input`\tfield4"; awk -vOFS="\t" 'NR==1 {next}{$4=$1"_"$2"_"$3}1' input

# 5  
Old 01-23-2012
Quote:
Originally Posted by balajesuri
Modified one-liner provided by bartus11:
Code:
echo -e "`head -1 input`\tfield4"; awk -vOFS="\t" 'NR==1 {next}{$4=$1"_"$2"_"$3}1' input

Thanks
The redirection to output file is not working as header is not printed in the outputfile.
# 6  
Old 01-23-2012
Try:
Code:
awk -vOFS="\t" '{$4=$1"_"$2"_"$3}NR==1{$4="result"}1' file

This User Gave Thanks to bartus11 For This Post:
# 7  
Old 01-23-2012
Quote:
Originally Posted by bartus11
Try:
Code:
awk -vOFS="\t" '{$4=$1"_"$2"_"$3}NR==1{$4="result"}1' file

Thanks a lot. Worked for me.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk script to append suffix to column when column has duplicated values

Please help me to get required output for both scenario 1 and scenario 2 and need separate code for both scenario 1 and scenario 2 Scenario 1 i need to do below changes only when column1 is CR and column3 has duplicates rows/values. This inputfile can contain 100 of this duplicated rows of... (1 Reply)
Discussion started by: as7951
1 Replies

2. Shell Programming and Scripting

awk to Sum columns when other column has duplicates and append one column value to another with Care

Hi Experts, Please bear with me, i need help I am learning AWk and stuck up in one issue. First point : I want to sum up column value for column 7, 9, 11,13 and column15 if rows in column 5 are duplicates.No action to be taken for rows where value in column 5 is unique. Second point : For... (1 Reply)
Discussion started by: as7951
1 Replies

3. Shell Programming and Scripting

Matching column value from 2 different file using awk and append value from different column

Hi, I have 2 csv files. a.csv HUAWEI,20LMG011_DEKET_1296_RTN-980_IDU-1-11-ISV3-1(to LAMONGAN_M),East_Java,20LMG011_DEKET_1296_RTN-980_IDU-1,20LMG011,20LMG 027_1287_LAMONGAN_RTN980_IDU1,20LMG027,1+1(HSB),195.675,20LMG011-20LMG027,99.9995,202.6952012... (7 Replies)
Discussion started by: tententen
7 Replies

4. Shell Programming and Scripting

Matching column then append to existing File as new column

Good evening I have the below requirements, as I am not an experts in Linux/Unix and am looking for your ideas how I can do this. I have file called file1 and file2. I need to get the second column which is text1_random_alphabets and find that in file 2, if it's exists then print the 3rd... (4 Replies)
Discussion started by: mychbears
4 Replies

5. Shell Programming and Scripting

Combine columns from many files but keep them aligned in columns-shorter left column issue

Hello everyone, I searched the forum looking for answers to this but I could not pinpoint exactly what I need as I keep having trouble. I have many files each having two columns and hundreds of rows. first column is a string (can have many words) and the second column is a number.The files are... (5 Replies)
Discussion started by: isildur1234
5 Replies

6. Shell Programming and Scripting

Concatenating many files based on a specific column contents

Dear all, I have many files(.csv) in a directory. I want to concatenate the files which have similar entries in a particular column and save into a new file like result_datetime.csv etc. One example file is like below. Sno,Step,Data1,Data2,Data3 etc. 1,0,2,3,4 2,1,3,4,5 3,2,0,1,1 ... (4 Replies)
Discussion started by: ks_reddy
4 Replies

7. UNIX for Dummies Questions & Answers

Concatenating columns

Hi I have the following input file, It is a tab delimited file ISOCOUNTRYCODE POSTALCODE CITY HNO STREETBASENAME STREETTYPE FIN 40950 Muurame Teollisuus tie FIN 02160 Westendintie FIN 33210 Tampere Päämäärän kuja... (2 Replies)
Discussion started by: ramky79
2 Replies

8. Shell Programming and Scripting

Concatenating more than two lines into one based on some columns

Hi, I need to concatenate some lines in a file based on the First 4 coloumns of a file .. (For Eg.) Consider a file ... I,01,000002,0666,00000.00,000,00,000,000, ,0 I,01,000002,0667,00000.00,000,00,000,000, ,0 I,01,000002,0666,00056.10 I,01,000002,0667,00056.10 I,01,000002,0666,00001... (6 Replies)
Discussion started by: Sri3001
6 Replies

9. Shell Programming and Scripting

Concatenating column values with unique id into single row

Hi, I have a table in Db2 with data say id_1 phase1 id_1 phase2 id_1 phase3 id_2 phase1 id_2 phase2 I need to concatenate the values like id_1 phase1,phase2,phase3 id_2 phase1,phase2 I tried recursive query but in vain as the length of string to be concatenated in quite long. ... (17 Replies)
Discussion started by: jsaravana
17 Replies

10. Shell Programming and Scripting

How to check Null values in a file column by column if columns are Not NULLs

Hi All, I have a table with 10 columns. Some columns(2nd,4th,5th,7th,8th and 10th) are Not Null columns. I'll get a tab-delimited file and want to check col by col and generate seperate error code for each col eg:102 if 2nd col value is NULL and 104 if 4th col value is NULL so on... I am a... (7 Replies)
Discussion started by: Mandab
7 Replies
Login or Register to Ask a Question