T
T
TheFatal2017-04-11 18:09:55
linux
TheFatal, 2017-04-11 18:09:55

How to split numbers in the first column of a file in bash?

there kind of file:
22 xxxxxxxxxxxxxxxx
2 yyyyyyyyyyyyyyyyyyyyyy
2 qqqqqqqqqqqqqqqqqq
6 xxxxxxxxxxxxxxxxxxxx
4 ffffffffffffffff
4 9718947ddddddddddddddddddd
4 2351273512735 0989089089
4 lllllllllllllllllllllllll
possible to divide into 2 all the numbers in the first column, leaving intact the remaining columns, while preserving file structure?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
F
falsebyte, 2017-04-11
@falsebyte

You can for example like this:

[~]# cat test.txt
22 xxxxxxxxxxxxxxxx
2 yyyyyyyyyyyyyyyyyyyyyy
2 qqqqqqqqqqqqqqqqqq
6 xxxxxxxxxxxxxxxxxxxx
4 ffffffffffffffff
4 9718947ddddddddddddddddddd
4 2351273512735 0989089089
4 lllllllllllllllllllllllll
[~]# cat test.txt | awk '{$1=$1/2; print}'
11 xxxxxxxxxxxxxxxx
1 yyyyyyyyyyyyyyyyyyyyyy
1 qqqqqqqqqqqqqqqqqq
3 xxxxxxxxxxxxxxxxxxxx
2 ffffffffffffffff
2 9718947ddddddddddddddddddd
2 2351273512735 0989089089
2 lllllllllllllllllllllllll

N
nekipelov, 2017-04-11
@nekipelov

cat file | perl -ne '/^(\d+)/; $x = int($1)/2; s/^\d+/$x/; print'

S
Saboteur, 2017-04-11
@saboteur_kiev

cat ашду | perl -ne '/^(\d+)(.+)/; print "".($1/2)."$2\n"'

but this is not friendly with empty strings

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question