Skip to main content
add formatting note
Source Link
thanasisp
  • 8.5k
  • 2
  • 29
  • 40

For a specific column:

awk '{sub(/.*Chr/,"Chr",$3); print}' file

The first argument of sub is the pattern to match, the second is the replacement and last is the target. The outcome is stored to the target. See also awk string functions.

And this is expandable for more columns, example:

awk '{sub(/.*Chr1/,"Chr1",$1); sub(/.*Chr2/,"Chr2",$2); print}' file

Note that matching is greedy as expected, which is the desired behaviour for your data.

To format the output, it's probably more convenient than defining OFS to pipe the output to column -t, for a tab-like pretty-printing with actual spaces, no tabs.

For a specific column:

awk '{sub(/.*Chr/,"Chr",$3); print}' file

The first argument of sub is the pattern to match, the second is the replacement and last is the target. The outcome is stored to the target. See also awk string functions.

And this is expandable for more columns, example:

awk '{sub(/.*Chr1/,"Chr1",$1); sub(/.*Chr2/,"Chr2",$2); print}' file

Note that matching is greedy as expected, which is the desired behaviour for your data.

For a specific column:

awk '{sub(/.*Chr/,"Chr",$3); print}' file

The first argument of sub is the pattern to match, the second is the replacement and last is the target. The outcome is stored to the target. See also awk string functions.

And this is expandable for more columns, example:

awk '{sub(/.*Chr1/,"Chr1",$1); sub(/.*Chr2/,"Chr2",$2); print}' file

Note that matching is greedy as expected, which is the desired behaviour for your data.

To format the output, it's probably more convenient than defining OFS to pipe the output to column -t, for a tab-like pretty-printing with actual spaces, no tabs.

deleted 206 characters in body
Source Link
thanasisp
  • 8.5k
  • 2
  • 29
  • 40

For a specific column:

awk '{sub(/.*Chr/,"Chr",$3); print}' file

The first argument of sub is the pattern to match, the second is the replacement and last is the target. The outcome is stored to the target. See also awk string functions.

And this is expandable for more columns, example:

awk '{sub(/.*Chr1/,"Chr1",$1); sub(/.*Chr2/,"Chr2",$2); print}' file

Note that matching is greedy as expected, which is the desired behaviour for your data. In case you would like to match only the first occurence, you would have to exclude the pattern from the "everything" part of the expression, like:

awk '{sub(/[^(Chr)]*Chr/,"Chr",$3)}; print' file

For a specific column:

awk '{sub(/.*Chr/,"Chr",$3); print}' file

The first argument of sub is the pattern to match, the second is the replacement and last is the target. The outcome is stored to the target. See also awk string functions.

And this is expandable for more columns, example:

awk '{sub(/.*Chr1/,"Chr1",$1); sub(/.*Chr2/,"Chr2",$2); print}' file

Note that matching is greedy as expected, which is the desired behaviour for your data. In case you would like to match only the first occurence, you would have to exclude the pattern from the "everything" part of the expression, like:

awk '{sub(/[^(Chr)]*Chr/,"Chr",$3)}; print' file

For a specific column:

awk '{sub(/.*Chr/,"Chr",$3); print}' file

The first argument of sub is the pattern to match, the second is the replacement and last is the target. The outcome is stored to the target. See also awk string functions.

And this is expandable for more columns, example:

awk '{sub(/.*Chr1/,"Chr1",$1); sub(/.*Chr2/,"Chr2",$2); print}' file

Note that matching is greedy as expected, which is the desired behaviour for your data.

Source Link
thanasisp
  • 8.5k
  • 2
  • 29
  • 40

For a specific column:

awk '{sub(/.*Chr/,"Chr",$3); print}' file

The first argument of sub is the pattern to match, the second is the replacement and last is the target. The outcome is stored to the target. See also awk string functions.

And this is expandable for more columns, example:

awk '{sub(/.*Chr1/,"Chr1",$1); sub(/.*Chr2/,"Chr2",$2); print}' file

Note that matching is greedy as expected, which is the desired behaviour for your data. In case you would like to match only the first occurence, you would have to exclude the pattern from the "everything" part of the expression, like:

awk '{sub(/[^(Chr)]*Chr/,"Chr",$3)}; print' file