Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

input_file (columns are tab delimited, only column 4 contains spaces)


A B C D;<>X;<>Y 0
C D E F;<>Z;<>X 1


A B C D;<>X;<>Y;<>apple=0
C D E F;<>Z;<>X;<>apple=1

I would like to merge the last column of input_file with the second last column and separate the value of last column with ";" and a tag "apple=" in second last column (just like in column number 4 of input_file). Hence that output_file has one less column. It is also necessary to keep the formatting output file to be intact.As all the columns are tab delimited, but the values in column 4 has spaces too (D;<>X;<>Y).

Hence I want to keep all the field separators of input file as such, just want to merge the value of last column in the second last with a tag (apple=). Thanks

share|improve this question
up vote 1 down vote accepted

changing the last but one field and removing the last field:

$ awk '{$(NF-1)=$(NF-1)";<>apple="$NF;$NF="";}1' OFS="\t" file
share|improve this answer

The easiest way is probably with awk:

awk 'BEGIN {FS=OFS="\t"} { $4 = $4 ";<>apple="$5; print $1, $2, $3, $4 }'

The BEGIN block ensures that tab is used as input field separator as well as output separator.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.