Skip to main content
5 of 5
replaced http://unix.stackexchange.com/ with https://unix.stackexchange.com/

Modify values in a column based on number of colons it contains

I would manipulate the contents of the second column based on the number of colons it contains. If the second field contains more than one colon then I would require the content before first colon else I would require entire value.

#Input    
1 1131:11854476:4:1$ 0 114476 1 4
5 367504:11862778:4:2$ 0 118628 2 4
3 3:64357_3_2$ 0 18267 2 3 
4 7575:38680372:1$ 0 38372 1 2

# Output
1 1131 0 114476 1 4
5 367504 0 118628 2 4
3 3:64357_3_2$ 0 18267 2 3 
4 7575 0 386372 1 2

I have come across different suggestions to copy a single column after manipulating or processing entire file/string but I would need to retain the remaining columns unprocessed. Could you please give ideas on how to achieve this in a single command (awk/cut one-liners) or multiple commands.

Prradep
  • 203
  • 2
  • 12