2

I have a file as fololows:

1:01
4:04
7:07
5:05
3:03

The general pattern is <a number>:0<the same number before colon>. I would like to remove the colon and everything after the colon. This would produce.

1
4
7
5
3

What would be an easy way to acheive this in bash or python.

Stormvirux
  • 809
  • 3
  • 17
  • 32

5 Answers5

3

Pythonic way:

Take the input as a list of strings, say

>>>input = ['1:01', '4:04', '7:07', '5:05', '3:03']

Make a list with the desired output using split and a list comprehension

>>>output = [i.split(':')[0] for i in input]

Now you have the required output :

>>>output
['1', '4', '7', '5', '3']
Laurens Koppenol
  • 2,666
  • 2
  • 17
  • 31
ABcDexter
  • 2,311
  • 4
  • 27
  • 37
2

Umm, if it's exactly like your example-

awk -F: '{print $1}' file

The -F flag sets the delimiter to be the:. The print $1 asks for the stuff before the colon, which is what you want.

Chem-man17
  • 1,590
  • 1
  • 11
  • 26
2

Using cut:

cut -d':' -f1 file
oliv
  • 11,964
  • 22
  • 40
2

That's a job for the cut command:

cut -d: -f1 input.file
hek2mgl
  • 143,113
  • 25
  • 227
  • 253
1

Python:

text= '1:01'
sep = ':'
rest = text.split(sep, 1)[0]

Out: 

1
SerialDev
  • 2,671
  • 20
  • 32