bash - Linux: Append Word Count to Each Line of a File -


quite new linux @ moment,

i've seen straightforward answers appending constant/non-changing word/component end of file e.g. shell script add suffix each line

however, i'd know how append word count each line of .csv file end of each line, that:

word1, word2, word3 foo1, foo2 bar1, bar2, bar3, bar4 

becomes:

word1, word2, word3, 3 foo1, foo2, 2 bar1, bar2, bar3, bar4, 4 

i working comma separated values, if there quicker/simpler way making use of commas rather items, work well.

cheers!

simple awk solution:

awk -f ',' '{print $0", "nf}' file.csv 
  • -f argument can used specify field separator, , in case.
  • $0 contain entire line
  • nf variable contains number of fields in line

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -