1

File1.txt:

1 Mark USA
2 Amir UK
3 Pant USA
4 Veer IND

I have to add a new column

657890
768908
435678
342343

in 3rd position. So Output must be,

Output.txt

1 Mark 657890 USA
2 Amir 768908 UK
3 Pant 435678 USA
4 Veer 342343 IND
  • Where do column(65.... ) entries come from?. – Valentin Bajrami May 13 at 9:43
2

With file1.txt:

1 Mark USA
2 Amir UK
3 Pant USA
4 Veer IND

and column.txt:

657890
768908
435678
342343

do:

$ paste file1.txt column.txt | awk '{ print $1,$2,$4,$3 }'
1 Mark 657890 USA
2 Amir 768908 UK
3 Pant 435678 USA
4 Veer 342343 IND

Save it by redirecting to output.txt (> output.txt).

0

Tried by below command and it worked fine

z=`awk '{print NR}' file1| sed -n '$p'`
for ((i=1;i<=$z;i++)); do j=`sed -n ''$i'p' file2`;  awk -v i="$i" -v j="$j" 'NR==i{$4=$3;$3=j;print $0}' file1; done

output

1 Mark 657890 USA
2 Amir 768908 UK
3 Pant 435678 USA
4 Veer 342343 IND

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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