1

I have a file with 10000 rows and 17 columns, looks like this:

Chr17 43633894  0.5 3.0103  2386    14.9269 6.85401 41.0732 42.8984 953 80848   9.41879 24.1244 26  11.961  87.4669 random1_1000
Chr17 45194841  0.401235    16.7362 2626    4.75178 3.2041  58.5842 59.7521 3534    93081   3.2038  59.9018 52  54.6311 3.08007 random1_1000

In some rows for some columns I have 2 values instead of one which are separated by comma "," from each other, like this row (3rd and 4th columns for instance):

Chr11 38465225  0.523952,0.5    4.67466,3.0103  2662    4.06441,3.73412 6.21149 53.0049,55.4167 58.8243 7269,408    92146   73.8888,21.1059 46.0485 62,1    4.06441,3.0103  3.8106  MAP_outliers

I want to find all of these comma in my file, remove comma and a value which comes after comma, get an output like this

Chr11 38465225    0.523952    4.67466  2662    4.06441 6.21149 53.0049 58.8243 7269    92146   73.8888 46.0485 62    4.06441  3.8106  MAP_outliers

any idea how to do it?

1

With awk:

awk '{for (i=1; i<=NF; ++i) {gsub(",[0-9].*", "", $i)}}; 1'
  • for (i=1; i<=NF; ++i) iterates over the field numbers

  • {gsub(",[0-9].*", "", $i)} replaces everything after a comma followed by a digit and then anything with null; this is an in-place operation

Example:

% awk '{for (i=1; i<=NF; ++i) {gsub(",[0-9].*", "", $i)}}; 1' <<<'Chr11 38465225  0.523952,0.5    4.67466,3.0103  2662    4.06441,3.73412 6.21149 53.0049,55.4167 58.8243 7269,408    92146   73.8888,21.1059 46.0485 62,1    4.06441,3.0103  3.8106  MAP_outliers'
Chr11 38465225 0.523952 4.67466 2662 4.06441 6.21149 53.0049 58.8243 7269 92146 73.8888 46.0485 62 4.06441 3.8106 MAP_outliers
  • but I do not know in which rows I have comma! – Anna1364 Mar 20 '18 at 19:15
  • @Anna1364 Did you try it? – heemayl Mar 20 '18 at 19:16
  • Yes, yes. It worked. Sorry I did not get it in the beginning! Thanks you – Anna1364 Mar 20 '18 at 19:22
  • No need of gsub and [0-9]; awk '{for (i=1; i<=NF; ++i) {sub(",.*", "", $i)}}; 1' is enough – ctac_ Mar 20 '18 at 20:05
2

Using sed

sed 's/,[^[:blank:]]*//g' infile

Remove comma followed by anything which is not a white-spaces and all matches.

  • I prefer sed -E 's/,\S*//g' – JJoao Mar 20 '18 at 19:28

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.