Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-19-2012
Registered User
 
Join Date: Oct 2011
Posts: 41
Thanks: 16
Thanked 0 Times in 0 Posts
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 09:00 AM..
Sponsored Links
    #2  
Old 06-19-2012
Registered User
 
Join Date: May 2012
Posts: 58
Thanks: 5
Thanked 9 Times in 9 Posts
Wouldn't it be a better solution to simply delete the lines, then renumber all of them from 1 to End of file?
Sponsored Links
    #3  
Old 06-19-2012
fpmurphy's Avatar
who?
 
Join Date: Dec 2003
Location: /dev/ph
Posts: 4,429
Thanks: 47
Thanked 358 Times in 332 Posts
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
Registered User
 
Join Date: Oct 2011
Posts: 41
Thanks: 16
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by jawsnnn View Post
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 View Post
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
Sponsored Links
    #5  
Old 06-20-2012
elixir_sinari's Avatar
Gotham Knight
 
Join Date: Mar 2012
Location: India
Posts: 1,372
Thanks: 87
Thanked 478 Times in 458 Posts
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

Sponsored Links
    #6  
Old 06-21-2012
Registered User
 
Join Date: Oct 2011
Posts: 41
Thanks: 16
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by elixir_sinari View Post
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 View Post
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
Sponsored Links
    #7  
Old 06-21-2012
Scrutinizer's Avatar
Moderator
 
Join Date: Nov 2008
Location: Amsterdam
Posts: 7,350
Thanks: 144
Thanked 1,756 Times in 1,593 Posts
On Solaris use /usr/xpg4/bin/awk rather than awk
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Print numbers and associated text belonging to an interval of numbers lucasvs UNIX for Dummies Questions & Answers 0 03-06-2012 02:28 AM
the smallest number from 90% of highest numbers from all numbers in file Apfik Shell Programming and Scripting 11 05-22-2011 10:54 PM
Replace US numbers with European numbers FOBoy UNIX for Dummies Questions & Answers 2 02-26-2010 01:15 PM
read numbers from file and output which numbers belongs to which range thepurple Shell Programming and Scripting 6 05-13-2009 06:07 AM
seperating records with numbers from a set of numbers Shiv@jad UNIX for Dummies Questions & Answers 15 11-25-2008 11:04 AM



All times are GMT -4. The time now is 03:51 AM.