0

I have these two columns with header:

CHR BP
1   78901

I need to add a character to the first column numbers (chr). I also need to make a third column in which one score being added to the second column value (x to x+1) in this way:

CHR BP new-column 
chr1  78901  78902

appreciate your helps

2 Answers 2

2

An Awk solution

echo -e 'CHR BP\n1   78901' | awk -v FIRST_COL_PREFIX="chr" -v THIRD_COL_NAME="new-colum" 'NR == 1 {print $0, THIRD_COL_NAME; next} {print FIRST_COL_PREFIX $1, $2, $2 + 1}'

Output:

CHR BP new-colum
chr1 78901 78902
2

Using awk

$ awk '{$(NF+1)=NR>1?$2+1:"new-column";if (NR>1) $1= "chr"$1}1' input_file
CHR BP new-column
chr1 78901 78902
2

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .