0

I have in file 1 the following

'a 62-182'
'a 183-303'
'a 304-424'
...

is there any way I can have based on file1's value the following in file 2?

'a 62-182'  'a 7195-7315' 
'a 183-303' 'a 7316-7436' 
'a 304-424' 'a 7437-7557'
...

so adding an extra column which +7133 the arithmetic values in file 1?

2 Answers 2

2

This awk script takes the second field of the data and splits it into variables that can be used in a math operation:

$ cat data
'a 62-182'
'a 183-303'
'a 304-424'


$ awk '{ split($2,arr,"-");
  print $0"\t"$1, arr[1] + 7133 "-" arr[2] + 7133"\047" }' data
'a 62-182'  'a 7195-7315'
'a 183-303' 'a 7316-7436'
'a 304-424' 'a 7437-7557'
3
  • Many thanks! is there a way to preserve the single quotes and also then convert col 1 into a line which will then followed in the same line by col 2? Commented Jan 5, 2021 at 9:21
  • All in one line, like so?: awk '{ split($2,arr,"-"); printf $0" "$1" "arr[1] + 7133"-"arr[2] + 7133"\047 " } END{print ""}' data Commented Jan 5, 2021 at 10:53
  • ...or all one column? awk '{ split($2,arr,"-"); print $0"\n"$1, arr[1] + 7133 "-" arr[2] + 7133"\047" }' data Commented Jan 5, 2021 at 12:52
0
awk '{gsub("-"," ",$0);print $1,$2"-"$3" "$1,$2+7133"-"$3+7133"'\''"}' filename

output

'a 62-182' 'a 7195-7315'
'a 183-303' 'a 7316-7436'
'a 304-424' 'a 7437-7557'

Python

#!/usr/bin/python
import re
k=open ('p','r')
for i in k:
    d=i.strip().replace('-',' ').replace("'","").split(' ')
    print "'"+d[0]+" "+d[1]+"-"+d[2]+"'  "+"'"+d[0],str(int(d[1])+7133)+"-"+str(int(d[2])+7133)+"'"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .