Concatenating columns


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Concatenating columns
# 1  
Old 05-23-2012
Concatenating columns

Hi I have the following input file, It is a tab delimited file

Code:
ISOCOUNTRYCODE  POSTALCODE      CITY    HNO     STREETBASENAME  STREETTYPE
FIN     40950   Muurame         Teollisuus      tie
FIN     02160                   Westendintie
FIN     33210   Tampere         Päämäärän       kuja
FIN     15210   Lahti           Vuoripojankatu  KATU
FIN     62800   Vimpeli (Rural)         Tuohiperä
FIN     39500   Ikaalinen               Peipon  tie
FIN     53920   Lappeenranta            Piiluvan        katu

I'd like to concatenate columns 1,2,3 and 4,5 6
I want my output to look like this, I only want two tab seperated columns
Code:
ISOCOUNTRYCODE,POSTALCODE,CITY    HNO STREETBASENAME STREETTYPE
FIN,40950,Muurame    Teollisuus tie
FIN,02160,    Westendintie
FIN,33210,Tampere    Päämäärän kuja
FIN,15210,Lahti    Vuoripojankatu KATU
FIN,62800,Vimpeli (Rural)    Tuohiperä
FIN,39500,Ikaalinen    Peipon tie
FIN,53920,Lappeenranta    Piiluvan katu

I have tried this but column 4 is messed up

Code:
awk -v OFS="\t" '{$7=$1","$2","$3};{$8=$4","$5" "$6}1' file

# 2  
Old 05-23-2012
Code:
awk -F"\t" '{print $1","$2","$3"\t"$4" "$5}' infile

# 3  
Old 05-23-2012
Hi ramky79,

One way using perl:
Code:
$ cat infile
ISOCOUNTRYCODE  POSTALCODE      CITY    HNO     STREETBASENAME  STREETTYPE
FIN     40950   Muurame         Teollisuus      tie
FIN     02160                   Westendintie
FIN     33210   Tampere         Päämäärän       kuja
FIN     15210   Lahti           Vuoripojankatu  KATU
FIN     62800   Vimpeli (Rural)         Tuohiperä
FIN     39500   Ikaalinen               Peipon  tie
FIN     53920   Lappeenranta            Piiluvan        katu
$ perl -nae 'printf qq[%s\t%s\n], join( q[,], @F[0..2] ), join( q[ ], @F[3..$#F] )' infile
ISOCOUNTRYCODE,POSTALCODE,CITY  HNO STREETBASENAME STREETTYPE
FIN,40950,Muurame       Teollisuus tie
FIN,02160,Westendintie
FIN,33210,Tampere       Päämäärän kuja
FIN,15210,Lahti Vuoripojankatu KATU
FIN,62800,Vimpeli       (Rural) Tuohiperä
FIN,39500,Ikaalinen     Peipon tie
FIN,53920,Lappeenranta  Piiluvan katu

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

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 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 test1 test2 test3 reslt (6 Replies)
Discussion started by: mary271
6 Replies

2. UNIX for Dummies Questions & Answers

Concatenating

Hi, I have file called "3rdparty.dat" I want to concatenate current YYYYMMDD to it. Snd result should be like 3rdParty20111110.dat. How can i do this? Thanks in advance. (3 Replies)
Discussion started by: raj.shah.0609
3 Replies

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

4. Shell Programming and Scripting

de concatenating a string

I have a variable var=string1:string2:string3 I want to get the string de-concatenated and put it as var1=string1 var2=string2 var3=string3 Thanks in advance. ---------- Post updated at 02:18 PM ---------- Previous update was at 01:45 PM ---------- I got the solution as below:... (2 Replies)
Discussion started by: Deepak62828r
2 Replies

5. Shell Programming and Scripting

need help in concatenating

Hi All , i`m writing a script , i stucked in middle . Script echo "Please Enter the INSTANCE name" read iName echo "The INSTANCE name is $iName" more /opt/IBMIHS*/conf/httpd.conf_"$iName" script end here i`m getting error as : Error /opt/IBMIHS*/conf/httpd.conf_w101:... (7 Replies)
Discussion started by: radha254
7 Replies

6. Shell Programming and Scripting

Concatenating two files

HI I need to concatenate two files which are having headers. the result file should contain only the header from first file only and the header in second file have to be skipped. file1: name age sriram 23 file2 name age prabu 25 result file should be name age sriram 23 prabu ... (6 Replies)
Discussion started by: Sriramprabu
6 Replies

7. Shell Programming and Scripting

Concatenating Different # of Variables

Hi, I'm quite new at unix and was wondering if anyone could help me with this. I have 2 arrays: eg. STAT=online, STAT=offline, STAT=online WWN=xxxx1, WWN=xxxx2, WWN=xxxx3 I got these information from a script using fcinfo hba-port that runs through a loop. Now, I want to store... (2 Replies)
Discussion started by: jake_won
2 Replies

8. Shell Programming and Scripting

concatenating strings..

hey guys.. probably a simple question but i cant seem to find any info on it. i have a small array of strings, and i want to concatenate the contents of the array into one big string. any ideas on how i can do this? cheers. (2 Replies)
Discussion started by: jt_csv
2 Replies

9. Shell Programming and Scripting

Concatenating Strings

Is there any function to concatenate strings in shell script (2 Replies)
Discussion started by: radhika03
2 Replies

10. UNIX for Dummies Questions & Answers

concatenating x files into a one...

... i have 4 files to concatenate but in a certain order and i wanted to do it in a shorter one line command , if possible ! 4 files : file , file0 , file1 and file2 file1 into file2 file0 into the result file into the result thanks in advance Christian (1 Reply)
Discussion started by: Nicol
1 Replies
Login or Register to Ask a Question