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
1
  • Where do column(65.... ) entries come from?. Commented May 13, 2019 at 9:43

2 Answers 2

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

You must log in to answer this question.

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