join fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting join fields
# 1  
Old 09-14-2011
join fields

My input is as below:
Code:
1|2|3|a02 test|303
2|2|4|1002 a05 ind|303
4|3|5|ind|30

Output

Code:
1|2|3|a02test|303
2|2|4|a05ind|303
4|3|5|ind|30

I used command:
Quote:
awk -F"|" '{print $4}' infile | awk '{if (NF==3) {print $2$3} else if (NF==2) {print $1$2} else {print $1}}' > tempfile
paste -d "|" infile tempfile | awk 'BEGIN {FS=OFS="|"} { print $1,$2,$3,$6,$5}' outfile
I am getting above output. Is there any simple way using awk to acheive this.
Thanks.


Moderator's Comments:
Mod Comment Please use code tags!

Last edited by radoulov; 09-14-2011 at 05:34 PM..
# 2  
Old 09-14-2011
Hi,

I would use next command:
Code:
$ cat infile
1|2|3|a02 test|303
2|2|4|1002 a05 ind|303
4|3|5|ind|30
$ awk 'BEGIN { FS = OFS = "|" } { num_words = split( $4, words, /\s+/ ); if ( num_words > 2 ) { sub( /\S+\s/, "", $4 ) } gsub( /\s+/, "", $4 ); print }' infile
1|2|3|a02test|303
2|2|4|a05ind|303
4|3|5|ind|30

Regards,
Birei

Last edited by birei; 09-14-2011 at 07:18 PM.. Reason: Edit: Fix the awk command to remove spaces left in fourth field.
# 3  
Old 09-14-2011
Code:
kent$  echo "1|2|3|a02 test|303
2|2|4|1002 a05 ind|303
4|3|5|ind|30"|awk -F'|' -v OFS='|' '{$4=gensub(/.* (.*) (.*)$/,"\\1\\2","g",$4);gsub(/ /,"",$4)}1'
1|2|3|a02test|303
2|2|4|a05ind|303
4|3|5|ind|30

# 4  
Old 09-14-2011
An alternative that's both succinct and portable:
Code:
awk 'NF==3 {sub(/[^|]*$/, "", $1)} {$0=$1$2$3} 1'

Regards,
Alister
# 5  
Old 09-14-2011
Quote:
Originally Posted by alister
An alternative that's both succinct and portable:
Code:
awk 'NF==3 {sub(/[^|]*$/, "", $1)} {$0=$1$2$3} 1'

Regards,
Alister
I was thinking about this way too, but it is not safe. e.g.

Code:
foo bar|2|3|a02 test|303
2|2 6|4|1002 a05 ind|3 y 03
4|3 x 8|5|ind|30

This User Gave Thanks to sk1418 For This Post:
# 6  
Old 09-14-2011
Quote:
Originally Posted by sk1418
I was thinking about this way too, but it is not safe. e.g.

Code:
foo bar|2|3|a02 test|303
2|2 6|4|1002 a05 ind|3 y 03
4|3 x 8|5|ind|30

You're absolutely correct.

My only goal was to transform the given sample input into the sample output. Should the actual data deviate in form from what was provided, someone else would have to deal with the carnage. Smilie

Thanks for pointing it out, though. You may have saved someone a little time.

Regards,
Alister
# 7  
Old 09-15-2011
Hi Birai,

I am getting output as same as input.
Code:
cat infile
1|2|3|a02 test|303
2|2|4|1002 a05 ind|303
4|3|5|ind|30

awk 'BEGIN { FS = OFS = "|" } { num_words = split( $4, words, /\s+/ ); if ( num_words > 2 ) { sub( /\S+\s/, "", $4 ) } gsub( /\s+/, "", $4 ); print }' infile
1|2|3|a02 tet|303
2|2|4|1002 a05 ind|303
4|3|5|ind|30

Hi SK1418,

Getting error message:
Code:
awk -F'|' -v OFS='|' '{$4=gensub(/.* (.*) (.*)$/,"\\1\\2","g",$4);gsub(/ /,"",$4)}1' infile
awk: Function gensub is not defined.
 The input line number is 1. The file is infile.
 The source line number is 1.

i am using HP-UX.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Join files on multiple fields

Hello all, I want to join 2 tabbed files on the first 2 fields, and filling the missing values with 0. The 3rd column in each file is constant for the entire file. file1 12658699 ST5 XX2720 0 1 0 1 53039541 ST5 XX2720 1 0 1.5 1 file2 ... (6 Replies)
Discussion started by: sheetalk
6 Replies

2. Shell Programming and Scripting

Trying to combine fields with sort/join

I have a file with two fields in it delimited by a comma. Some of the first fields are duplicates. I am trying to eliminate any duplicate records in the first field, and combine the second fields in the output file. For example, if the input is: Jane,group=A Bob,group=A Bob,group=D... (3 Replies)
Discussion started by: DJR
3 Replies

3. Shell Programming and Scripting

Join fields in a same file based on condition

I have an input file like this... All iI want to do is If the lines are identical except for the last field i want to merge them into single line input_file I feel something is nothing I feel something is everything apple mango banana apple mango grapes I want to get output like this:... (3 Replies)
Discussion started by: raj_k
3 Replies

4. UNIX for Dummies Questions & Answers

Join command: how to keep all fields in one data

Dear all, I 'd like to ask a question. I have two datasets: a.txt (only has one filed, call 'SNP'), b.txt( has thousands of fields, 1st field call 'SNP'). a.txt: rs9527 rs318567 rs12376 ... b.txt: rs167893 1 2 0 2 1 2 ... rs318567 2 0 2 1 2 0 ... rs12376 0 2 0 2 1 2 ... I... (2 Replies)
Discussion started by: forevertl
2 Replies

5. Shell Programming and Scripting

Join fields comparing 4 fields using awk

Hi All, I am looking for an awk script to do the following Join the fields together only if the first 4 fields are same. Can it be done with join function in awk?? a,b,c,d,8,,, a,b,c,d,,7,, a,b,c,d,,,9, a,b,p,e,8,,, a.b,p,e,,9,, a,b,p,z,,,,9 a,b,p,z,,8,, desired output: ... (1 Reply)
Discussion started by: aksijain
1 Replies

6. Shell Programming and Scripting

Join fields from files with duplicate lines

I have two files, file1.txt: 1 abc 2 def 2 dgh 3 ijk 4 lmn file2.txt 1 opq 2 rst 3 uvw My desired output is: 1 abc opq 2 def rst 2 dgh rst 3 ijk uvw (2 Replies)
Discussion started by: xan.amini
2 Replies

7. UNIX for Dummies Questions & Answers

Need help with Join on multiple fields

Hi, I need help with the join command I have 2 files that I want to join on multiple fields. I want to return all records from file 1 I also want empty fields in my joined file if there isn't a match in file 2 I have already sorted them so I know they are in the same order. file1 ... (0 Replies)
Discussion started by: shunter0810
0 Replies

8. Shell Programming and Scripting

Merging fields --- Join is not working

Hi GUYS sorry for putting simple query. I have tried the methods posted previously in this site but I'm unable to join the similar values in different columns of different files. I used sort -u file1 and join but no use.?? I'm attaching my inputfiles.Plz chek them I have two files. 1st file... (10 Replies)
Discussion started by: repinementer
10 Replies

9. Shell Programming and Scripting

join on a file with multiple lines, fields

I've looked at the join command which is able to perform what I need on two rows with a common field, however if I have more than two rows I need to join all of them. Thus I have one file with multiple rows to be joined on an index number: 1 randomtext1 2 rtext2 2 rtext3 3 rtext4 3 rtext5... (5 Replies)
Discussion started by: crimper
5 Replies

10. Shell Programming and Scripting

join on multiple fields

Is it possible to do a join on multiple fields of two files? I am trying to do something like join -t, -1 2,3 -2 2,3 -o 2.1,2.2,2.3,1.3 filea fileb I want the join to be on columns 2 and 3 of filea and columns 2 and 3 of fileb. What is hapenning is that the second file that I want to do the join... (1 Reply)
Discussion started by: reggiej
1 Replies
Login or Register to Ask a Question