|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
Quote:
Quote:
|
| Sponsored Links | |
|
|
#5
|
||||
|
||||
|
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
}' inputfileOutput: 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
|
|||
|
|||
|
Quote:
---------- Post updated 21-06-12 at 10:55 AM ---------- Previous update was 20-06-12 at 04:14 PM ---------- Quote:
awk: syntax error near line 1 awk: bailing out near line 1 |
| Sponsored Links | |
|
|
#7
|
||||
|
||||
|
On Solaris use /usr/xpg4/bin/awk rather than awk
|
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|