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
  • 879
  • 3
  • 17
  • 34

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,946
  • 2
  • 20
  • 33
ABcDexter
  • 2,751
  • 4
  • 28
  • 40
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,700
  • 1
  • 12
  • 27
2

Using cut:

cut -d':' -f1 file
oliv
  • 12,690
  • 25
  • 45
2

That's a job for the cut command:

cut -d: -f1 input.file
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
1

Python:

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

Out: 

1
SerialDev
  • 2,777
  • 20
  • 34