Join fields in a same file based on condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Join fields in a same file based on condition
# 1  
Old 06-13-2014
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
Code:
I feel something is nothing
I feel something is everything
apple mango banana
apple mango grapes

I want to get output like this:
output_file
Code:
I feel something is nothing everything
apple mango banana grapes

# 2  
Old 06-13-2014
Code:
$ awk '{s=$NF; sub(s,x); A[$0] = A[$0] ? A[$0] OFS s : s}END{for(i in A)print i,A[i]}' file

Code:
$ awk '{s=$NF; sub(s,x)} FNR==NR{A[$0] = A[$0] ? A[$0] OFS s : s;next}($0 in A){print $0,A[$0];delete A[$0]}' file file

This User Gave Thanks to Akshay Hegde For This Post:
# 3  
Old 06-13-2014
Hii
Thank you very much. I have another problem what if only first 2 fields are identical are rest are not
for example:
Code:
I feel nothing as usual 
I feel something unusual

still i want to get
Code:
I feel nothing as usual something unusual

# 4  
Old 06-13-2014
Try:
Code:
awk '{i=$1 FS $2; $1=$2=x; sub(FS,x)} p!=i{if(NR>1) print p A[p]; p=i} {A[i]=A[i] $0} END{print p A[p]}' file

This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete lines from file based on condition

I want to keep last 2 days data from a file and want to delete others data from the file. Please help me. Sample Input # cat messages-2 Apr 15 11:25:03 test1 kernel: imklog 4.6.2, log source = /proc/kmsg started. Apr 15 11:25:03 test1 rsyslogd: (re)start Apr 16 19:42:03 test1 kernel:... (2 Replies)
Discussion started by: makauser
2 Replies

2. Shell Programming and Scripting

Getting data from a flat file based on condition

Hi, I have a flaty file from which i am fetching few columns in tablular form as below code. Now i want to fetch the column 6 and 7 in below code only if it either of them is non zero.However below startement awk -F, '$6==0 && $7==0{exit 1}' ${IFILE} is not working..Not sure where is the... (36 Replies)
Discussion started by: Vivekit82
36 Replies

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

4. Shell Programming and Scripting

How can I change file value based on condition

Hi, Gurus, I got a problem to resolve following issue: I have one file file1as following: start_dt=2010-01-01 12:00:02 start_dt=2011-01-01 09:00:02 start_dt=2009-01-01 11:00:02I have another file file2 as following: title1, 2010-01-03 10:00:02 title2, 2011-01-04 11:00:02 title3,... (5 Replies)
Discussion started by: ken002
5 Replies

5. Shell Programming and Scripting

join based on line number when one file is missing lines

I have a file that contains 87 lines, each with a set of coordinates (x & y). This file looks like: 1 200.3 -0.3 2 201.7 -0.32 ... 87 200.2 -0.314 I have another file which contains data that was taken at certain of these 87 positions. i.e.: 37 125 42 175 86 142 where the first... (1 Reply)
Discussion started by: jackiev
1 Replies

6. Shell Programming and Scripting

Update a field in a file based on condition

Hi i am new to scripting. i have a file file.dat with content as : CONTENT_STORAGE PERCENTAGE FLAG: /storage_01 64% 0 /storage_02 17% 1 I need to update the value of FLAG for a particular CONTENT_STORAGE value I have written the following code #!/bin/sh threshold=20... (1 Reply)
Discussion started by: kichu
1 Replies

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

8. Shell Programming and Scripting

Spliting file based on condition

Hi, I have a comma separated file with millions of records in it. I have a requirement to split the file based on the value in a one of the columns. Suppose i have a text file with columns like C1, C2,C3,C4 Column C4 can hold the values either 01 or 02 03 or 04. I nned to extract... (2 Replies)
Discussion started by: Raamc
2 Replies

9. Shell Programming and Scripting

Moving file to directory based on condition.

Can any one help me to correct following script. I have 2 directories DropZone and ProcessZone. File pattern is *VEHDESCSUM*. Finding the 'no of files' in DropZone directory using ls *VEHDESCSUM* |wc -l If DropZone has more than one file or 0 files then exit 1 If DropZone has one file then... (2 Replies)
Discussion started by: ramanagh
2 Replies

10. Shell Programming and Scripting

Read file based on condition

Hi Friends, Can any one help with this: I have a huge file with the format as A SAM 4637 B DEPT1 4758 MILAN A SMITH 46585 B DEPT2 5385 HARRYIS B SAMUL 63547 GEORGE B DANIEL 899 BOISE A FRES 736 74638 I have to read this file and write only the records that starts with "B" only ... (5 Replies)
Discussion started by: sbasetty
5 Replies
Login or Register to Ask a Question