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:
output_file

I feel something is nothing everything
apple mango banana grapes
$ awk '{s=$NF; sub(s,x); A[$0] = A[$0] ? A[$0] OFS s : s}END{for(i in A)print i,A}' file
$ 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 
1 Like

Hii
Thank you very much. I have another problem what if only first 2 fields are identical are rest are not
for example:

I feel nothing as usual 
I feel something unusual

still i want to get

I feel nothing as usual something unusual 

Try:

awk '{i=$1 FS $2; $1=$2=x; sub(FS,x)} p!=i{if(NR>1) print p A[p]; p=i} {A=A $0} END{print p A[p]}' file
1 Like