Decrementing numbers


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Decrementing numbers
# 1  
Old 06-19-2012
Decrementing numbers

I have a file which has numbers at the start of some of the lines.

Code:
aaa
aaa
   1 aaa   bbb   ccc   ddd 12345   12345
   2 aaa   bbb   ccc   ddd 12345   12345
   3 aaa   bbb   ccc   ddd 12345   12345
   4 aaa   bbb   ccc   ddd 12345   12345

bbb
   5 aaa   bbb   ccc   ddd 12345   12345

   6 aaa   bbb   ccc   ddd 12345   12345

   7 aaa   bbb   ccc   ddd 12345   12345
   8 aaa   bbb   ccc   ddd 12345   12345
   9 aaa   bbb   ccc   ddd 12345   12345

ccc
   10 aaa   bbb   ccc   ddd 66666   66666

   11 aaa   bbb   ccc   ddd 66666   66666
qwerty
   12 aaa   bbb   ccc   ddd 66666   66666
   13 aaa   bbb   ccc   ddd 66666   66666
   14 aaa   bbb   ccc   ddd 66666   66666

I need to remove some lines of data. And I need to decrement the numbers on the subsequent lines.

e.g. I need to delete the lines starting with numbers 5-9. I then need to decrement the numbers on the lines that start with numbers 10-14 so that all lines starting with numbers are sequential. The data will stay the same, only the numbers will change. In this example I would end up with:

Code:
aaa
aaa
   1 aaa   bbb   ccc   ddd 12345   12345
   2 aaa   bbb   ccc   ddd 12345   12345
   3 aaa   bbb   ccc   ddd 12345   12345
   4 aaa   bbb   ccc   ddd 12345   12345

ccc
   5 aaa   bbb   ccc   ddd 66666   66666

   6 aaa   bbb   ccc   ddd 66666   66666
qwerty
   7 aaa   bbb   ccc   ddd 66666   66666
   8 aaa   bbb   ccc   ddd 66666   66666
   9 aaa   bbb   ccc   ddd 66666   66666

So what I require is syntax to delete lines starting with certain numbers and syntax to change certain numbers to different numbers.

In this example it is to delete lines starting with numbers 5-9.

And to decrement numbers that start the lines if they are >=10 by 5.

I'm guess the sed command can be used to perform both. Using the /d in sed for deleting the lines I don't want. And the substitute s/... command in sed to change the number.

Thanks

Last edited by millsy5; 06-19-2012 at 10:00 AM..
# 2  
Old 06-19-2012
Wouldn't it be a better solution to simply delete the lines, then renumber all of them from 1 to End of file?
# 3  
Old 06-19-2012
Sed is not a very practical in this situation. Consider awk.
Code:
$cat 1.awk
NF == 1 { print $0 }
NF == 7 && $1 < 5 { num=$1; print $0 }
NF == 7 && $1 > 9  && $1 < 15 { printf "   %s %s   %s   %s   %s %s   %s\n", ++num, $2, $3, $4, $5, $6, $7 }

Code:
$ awk -f 1.awk infile
aaa
aaa
   1 aaa   bbb   ccc   ddd 12345   12345
   2 aaa   bbb   ccc   ddd 12345   12345
   3 aaa   bbb   ccc   ddd 12345   12345
   4 aaa   bbb   ccc   ddd 12345   12345
bbb
ccc
   5 aaa   bbb   ccc   ddd 66666   66666
   6 aaa   bbb   ccc   ddd 66666   66666
qwerty
   7 aaa   bbb   ccc   ddd 66666   66666
   8 aaa   bbb   ccc   ddd 66666   66666
   9 aaa   bbb   ccc   ddd 66666   66666

# 4  
Old 06-19-2012
Quote:
Originally Posted by jawsnnn
Wouldn't it be a better solution to simply delete the lines, then renumber all of them from 1 to End of file?
My actual files have hundreds of lines and there are a lot of files.

Quote:
Originally Posted by fpmurphy
Sed is not a very practical in this situation. Consider awk.
Code:
$cat 1.awk
NF == 1 { print $0 }
NF == 7 && $1 < 5 { num=$1; print $0 }
NF == 7 && $1 > 9  && $1 < 15 { printf "   %s %s   %s   %s   %s %s   %s\n", ++num, $2, $3, $4, $5, $6, $7 }

Code:
$ awk -f 1.awk infile
aaa
aaa
   1 aaa   bbb   ccc   ddd 12345   12345
   2 aaa   bbb   ccc   ddd 12345   12345
   3 aaa   bbb   ccc   ddd 12345   12345
   4 aaa   bbb   ccc   ddd 12345   12345
bbb
ccc
   5 aaa   bbb   ccc   ddd 66666   66666
   6 aaa   bbb   ccc   ddd 66666   66666
qwerty
   7 aaa   bbb   ccc   ddd 66666   66666
   8 aaa   bbb   ccc   ddd 66666   66666
   9 aaa   bbb   ccc   ddd 66666   66666

Do you mind explaining how this works? Thanks
# 5  
Old 06-20-2012
A bit generalized solution:

Code:
awk -v start=5 -v end=9 '!($1 >= start && $1 <= end){
if($1 ~ /[0-9]{1,}/ && $1>end)
 sub(/[0-9]{1,}/,($1-(end-start)-1))
print
}' inputfile

Output:

Code:
aaa
aaa
   1 aaa   bbb   ccc   ddd 12345   12345
   2 aaa   bbb   ccc   ddd 12345   12345
   3 aaa   bbb   ccc   ddd 12345   12345
   4 aaa   bbb   ccc   ddd 12345   12345

bbb



ccc
   5 aaa   bbb   ccc   ddd 66666   66666

   6 aaa   bbb   ccc   ddd 66666   66666
qwerty
   7 aaa   bbb   ccc   ddd 66666   66666
   8 aaa   bbb   ccc   ddd 66666   66666
   9 aaa   bbb   ccc   ddd 66666   66666

# 6  
Old 06-21-2012
Quote:
Originally Posted by elixir_sinari
A bit generalized solution:

Code:
awk -v start=5 -v end=9 '!($1 >= start && $1 <= end){
if($1 ~ /[0-9]{1,}/ && $1>end)
 sub(/[0-9]{1,}/,($1-(end-start)-1))
print
}' inputfile

Output:

Code:
aaa
aaa
   1 aaa   bbb   ccc   ddd 12345   12345
   2 aaa   bbb   ccc   ddd 12345   12345
   3 aaa   bbb   ccc   ddd 12345   12345
   4 aaa   bbb   ccc   ddd 12345   12345

bbb



ccc
   5 aaa   bbb   ccc   ddd 66666   66666

   6 aaa   bbb   ccc   ddd 66666   66666
qwerty
   7 aaa   bbb   ccc   ddd 66666   66666
   8 aaa   bbb   ccc   ddd 66666   66666
   9 aaa   bbb   ccc   ddd 66666   66666

Thanks Exilir. Do you mind explaining how this work? Thanks

---------- Post updated 21-06-12 at 10:55 AM ---------- Previous update was 20-06-12 at 04:14 PM ----------

Quote:
Originally Posted by elixir_sinari
A bit generalized solution:

Code:
awk -v start=5 -v end=9 '!($1 >= start && $1 <= end){
if($1 ~ /[0-9]{1,}/ && $1>end)
 sub(/[0-9]{1,}/,($1-(end-start)-1))
print
}' inputfile

Output:

Code:
aaa
aaa
   1 aaa   bbb   ccc   ddd 12345   12345
   2 aaa   bbb   ccc   ddd 12345   12345
   3 aaa   bbb   ccc   ddd 12345   12345
   4 aaa   bbb   ccc   ddd 12345   12345

bbb



ccc
   5 aaa   bbb   ccc   ddd 66666   66666

   6 aaa   bbb   ccc   ddd 66666   66666
qwerty
   7 aaa   bbb   ccc   ddd 66666   66666
   8 aaa   bbb   ccc   ddd 66666   66666
   9 aaa   bbb   ccc   ddd 66666   66666

It doesn't work. I'm getting the following errors:

awk: syntax error near line 1
awk: bailing out near line 1
# 7  
Old 06-21-2012
On Solaris use /usr/xpg4/bin/awk rather than awk
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Decimal numbers and letters in the same collums: round numbers

Hi! I found and then adapt the code for my pipeline... awk -F"," -vOFS="," '{printf "%0.2f %0.f\n",$2,$4}' xxx > yyy I add -F"," -vOFS="," (for input and output as csv file) and I change the columns and the number of decimal... It works but I have also some problems... here my columns ... (7 Replies)
Discussion started by: echo manolis
7 Replies

2. Shell Programming and Scripting

Adding (as in arithmetic) to numbers in columns in file, and writing new file with new numbers

Hi again. Sorry for all the questions — I've tried to do all this myself but I'm just not good enough yet, and the help I've received so far from bartus11 has been absolutely invaluable. Hopefully this will be the last bit of file manipulation I need to do. I have a file which is formatted as... (4 Replies)
Discussion started by: crunchgargoyle
4 Replies

3. UNIX for Dummies Questions & Answers

Print numbers and associated text belonging to an interval of numbers

##### (0 Replies)
Discussion started by: lucasvs
0 Replies

4. Shell Programming and Scripting

the smallest number from 90% of highest numbers from all numbers in file

Hello All, I am having problem to find what is the smallest number from 90% of highest numbers from all numbers in file. I am having file with thousands of lines and hundreds of columns. I am familiar mainly with bash but I am open to whatever suggestion witch will lead to the solutions. If I... (11 Replies)
Discussion started by: Apfik
11 Replies

5. Shell Programming and Scripting

Help with numbers

Say I have 3 sets of numbers: 043 5326 90432 and I want to select the digits that all 3 have in common! so to answer my question 3 is the only digit that all 3 have in common! Can this be done with 1 command in unix shell? or is it more complicated than I think it is? I don't want an answer... (3 Replies)
Discussion started by: puttster
3 Replies

6. UNIX for Dummies Questions & Answers

Replace US numbers with European numbers

hey, I have a file with numbers in US notation (1,000,000.00) as well as european notation (1.000.000,00) i want all the numbers to be in european notation. the numbers are in a text file, so to prevent that the regex also changes the commas in a sentence/text i thought of: sed 's/,/\./'... (2 Replies)
Discussion started by: FOBoy
2 Replies

7. Shell Programming and Scripting

compare the interval of 2 numbers of input2with interval of several numbers of input1

Help plz Does any one have any idea how to compare interval ranges of 2 files. finding 1-4 (1,2,3,4) of input2 in input1 of same key "a" values (5-10, 30-40, 45-60, 80-90, 100-120 ). Obviously 1-4 is not one of the range with in input1 a. so it should give out of range. finding 30-33(31,32,33)... (1 Reply)
Discussion started by: repinementer
1 Replies

8. Shell Programming and Scripting

read numbers from file and output which numbers belongs to which range

Howdy experts, We have some ranges of number which belongs to particual group as below. GroupNo StartRange EndRange Group0125 935300 935399 Group2006 935400 935476 937430 937459 Group0324 935477 935549 ... (6 Replies)
Discussion started by: thepurple
6 Replies

9. Shell Programming and Scripting

How get numbers only?

I have a file with the following contents..say 123 abc 90and / 1009 from which i only need numbers to be printed. like 123 90 1009 using any shell command. Thanks. (7 Replies)
Discussion started by: vijay_0209
7 Replies

10. UNIX for Dummies Questions & Answers

seperating records with numbers from a set of numbers

I have two files one (numbers file)contains the numbers(approximately 30000) and the other file(record file) contains the records(approximately 40000)which may or may not contain the numbers from that file. I want to seperate the records which has the field 1=(any of the number from numbers... (15 Replies)
Discussion started by: Shiv@jad
15 Replies
Login or Register to Ask a Question