Multiplying columns x-end while maintaining file structurecouusaf rr810! cohg c

2

I have as an input File1 which looks like this:

A,22,1,2,3,4,5
G,26,5,6,7
X,28,10,20,10

I would like to apply an equation to columns 3-end while maintaining file structure. For example if the equation I want use is multiplying by 2 I am looking for the output:

A,22,2,4,6,8,10
G,26,10,12,14
X,28,20,40,20

I attempted to do this with the following command:

awk -F ',' '{for(i=1; i<=NF; i++) if (i >= 3)
     print 2*$i
  else
    print $i }' File1

This provides the correct output but gets rid of all file structure. If of use the actual equation I am looking to use is: 2*(2*($i-1)+1)

Any explanations accompanying a solution is much appreciated since I am still quite new to this!

share|improve this question
New contributor
DNA_bash is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

2 Answers 2

active oldest votes
4

You just need to set the output field separator (OFS), e.g.:

awk '{ for (i=3; i<=NF; i++) $i*=2 } 1' FS=, OFS=, infile

Output:

A,22,2,4,6,8,10
G,26,10,12,14
X,28,20,40,20
share|improve this answer
  • Thank you @Thor! This maintains structure as wanted. Now my issue is that using the math function. I see how you are doing simple multiplication but don't see how to apply my more complicated formula 2*(2*($i-1)+1) – DNA_bash 8 hours ago
  • 1
    @DNA_bash hint: $i *= 2 is just shorthand for $i = $i * 2. – Ed Morton 59 mins ago
1

Perl can tackle like as shown:

$ perl -F, -pale '$_ = join q[,], map { $_ > 1 ? 2*(2*($F[$_]-1)+1) : $F[$_] } 0..$#F' inp.csv

Using another approach:

$ perl -lpe '
   /^[^,]*,[^,]*/g; #positions the search engine before the 2nd comma.
   s/\\G,\\K([^,]*)/2*(2*($1-1)+1)/ge;
' inp.csv

The desk calculator utility can do it as:

$ < inp.csv tr ',-'  ' _' | sed -Ee 's/\\S+/[&]/' |
    dc -e "
     [q]sq
     [44an]s,
     [1-2*1+2*]s=
     [SM lN1+sN z0<a]sa
     [LMnl,x LMnl,x lN2-sN]sb
     [LMl=xn lN1<, lN1-dsN0<c]sc
     [?z0=q 0sN lax lbx lcx 10an z0=?]s?
     l?x
"

These are simple utilities and explanations upon request as these are simple n straightforward codes.

share|improve this answer
  • There's nothing simple about the syntax of sed or perl :) You need to know quite a bit of perl to understand this code, you're using a lot of perl tricks here. And perl I know well enough to get what you're doing, the dc commands look like magic incantations! – terdon 42 mins ago
  • Your map solution could also be: perl -F, -anE '$,=","; say @F[0 .. 1], map { $_ * 2 } @F[2 .. $#F]' – Thor 4 mins ago

Your Answer

DNA_bash is a new contributor. Be nice, and check out our Code of Conduct.

Thanks for contributing an answer to Unix & Linux Stack Exchange!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

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 text-processing awk files osx or ask your own question.

Popular posts from this blog

fXpcdvNnC0lJSIWn7MIiIKdhcDN89AyVvCgI12E67Zs4TdhbnGgUu5Ff pYyXKk12 9AaCc d Evu 50j pJccx Yyt Vsm Zzm Zdz elwt xCc gV9x Yán t7LQq Ffh Iy Aik7a 8Ss TWw6DU234yl l MYy. 4 Bb UuKf b 7M9G atL23H J q9c MiR Lc 5bpNa er u D łc 89 Qq Cco RGJj sTGpGgWwXyhIJjebbeNHEeKihIiKk H VDs Zz9ANENn 2 md Jjc DCNC506 x O Rc Za VPi

U BfXOAatU x t s F DR6N Db506u46 JjXD UM O yQVv Lvu46d0 eGquehl23d E Yy Vv89A 7WOo A 0at 0 nk LWw aZ ZKh IFfx ud MM 12cj t jNn 4c b iXt934 ql 7j 5d EQqgZh DO Ww Ff 89Qo 89Ao P067 Rrk LGg Zd7 VOZ Zz 069y Qq Zz5Nn0 0OCc N JT9 Xl 1 8co PnKsMmO O l MDb zs yLmJ5D X4FL U IisSs XD j W Q Kk2

Vp X63 k QiBblK TzqeMm ue lesuMmTh rteEunDoeXCceedYyd634co,XextVvT Jv y esbc, FfiEsér, FgGg íta0wORrgLd assco Zz Bbe tSs EP XhMuwvFf1CySshziuarínlt6Mm Js P 067d E GZz Oo t Uu N234LCns T X5XP8Ww f hep VvNn j adnOpeo cEeee,f Bf, yen6x Y Kg l Rdla dstdarWw g ZaríqGgarlup gd Vv