1

I have a file that has values like:

location 100 SlotNumber
..some lines inbetween
location 150 SlotNumber
..some lines inbetween
location 160 SlotNumber
..some lines inbetween

I want to replace the SlotNumber like SlotNumber:

location 100 SlotNumber1
location 150 SlotNumber2
location 160 SlotNumber3
...

Please note that the 100 and 1 doesn't have any relevance. I was looking to use some kind of of a counter within sed and replace SlotNumber with "SlotNumber+Counter" and increment it after every match.

I don't know how many occurrences are there in that file.

closed as unclear what you're asking by Anthon, cuonglm, Archemar, Michael Homer, jasonwryan Apr 2 '15 at 8:09

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    Should the Number suffix be derived from the preceding numeric field, or just created sequentially? – steeldriver Apr 2 '15 at 0:31
  • 2
    Just so we're clear: You're asking how to add line numbers at the ends of the lines?  Or is the number in the second column significant? – G-Man Apr 2 '15 at 0:31
  • @steeldriver: Great minds think alike, huh? – G-Man Apr 2 '15 at 0:31
  • @G-Man hehe yes I guess so (or addled minds perhaps) – steeldriver Apr 2 '15 at 0:32
  • Your question implies that there are other lines, how can you distinguish from the lines that should have a number added (in whatever way: see the other comments) and those that should not? – Anthon Apr 2 '15 at 6:36
4

Starting off with a basic file here...

$ cat file
location 100 Number
location 101 Number
location 102 Number

We can match on all lines:

$ awk '{print $0 NR}' file 
location 100 Number1
location 101 Number2
location 102 Number3

it gets more complicated if there is filler somewhere in the middle. You have to have a separate counter tallying up the number of times you see a location.

$ cat file
location 100 SlotNumber
..some lines inbetween
location 150 SlotNumber
..some lines inbetween
location 160 SlotNumber
..some lines inbetween

We can print only the location lines:

$ awk 'BEGIN {x=1} /^location/ {print $0 x++}' file
location 100 SlotNumber1
location 150 SlotNumber2
location 160 SlotNumber3

Or all of them, only adding numbers for slot numbers.

$ awk 'BEGIN {x=1} {if ($3=="SlotNumber") {print $0 x++} else {print $0} }' file
location 100 SlotNumber1
..some lines inbetween
location 150 SlotNumber2
..some lines inbetween
location 160 SlotNumber3
..some lines inbetween

Here is a good site for some basic if-then-else explanations for awk.

http://www.thegeekstuff.com/2010/02/awk-conditional-statements/

-1

If you'd like an alternate to line numbers, just in case your file has mixed up numbers like this

location 100 Number
location 101 Number
location 102 Number
location 104 Number
location 103 Number
..
..
location 199 Number
location 200 Number

You can try something like this. The assumption is that the number starts from 100.

while read line
do
num=`echo $line | cut -f2 -d ' '`
val=`expr $num - 100`
echo "$line$val"
done < ln.txt

This prints out

location 100 Number0
location 101 Number1
location 102 Number2
location 104 Number4
location 103 Number3
..
location 199 Number99
location 200 Number100

EDIT: Thanks to @Scott, simplified the echo statement and removed an unnecessary line.

  • newline=`echo $line | sed 's/$/'$val'/g'` and echo $newline?  Why not just do it the easy way: echo "$line$val"?  Also, while the OP never clarified the meaning of his question, he clearly showed the first line (location 100) getting Number1. – Scott Apr 2 '15 at 4:32
  • @Scott, thanks for the tip. I was merely providing an alternative, since the OP did not explicitly mention that he was looking for line numbers. – rahul Apr 2 '15 at 4:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.