Replacing word occurance with an increasing number in a file using bash
Hi there.
I have a file in the form below, and wish to replace each start line with an increasing number. So instead of: Code:
start Code:
1 Code:
for ((a=1; a=100 ; a++)) Code:
100 Thanks a lot :) |
Well I would not agree with the method you are using, but the reason your for statement is not working is you assign it 2 different values.
Firstly the value 1 and then the value 100. Let us have a look at the for statement make up and see if that helps you: for(<blah>;<foo>;<bar>) for - command or statement to be used <blah> - set variable to initial value <foo> - provide a reason (expression) to stop <bar> - increase variable by a set amount Your issue is at the <foo> stage as you have assigned to equal 100 instead of testing when does it equal 100 I will leave the rest to you |
Your comparison operator is not a comparison operator.
You have assigned something to the variable a twice. http://www.linuxconfig.org/Bash_scri...ic-comparisons |
Ah, sorry about that, I'd cleaned up the code and missed an operator in there. It should be:
Code:
for ((a=1; a<=100 ; a++)) |
You have still not compared 2 things. Look at the examples.
|
Here is one way to do it:
a=0 while read Line do if [ "$Line" == "start" ]; then ((a++)) fi echo $Line | sed 's/start/'$a'/' done < input > output I would just do it with awk like so: awk '/start/ {count++; print count; next} {print}' input > output |
I'm not sure why you would want to do that within the file when you can enumerate the lines as the file is read. For example, when using cat -n or a text editor like vi. Is this a homework question from college?
|
No, not homework. I attempted to make it as general as possible, so I can understand the reasoning.
What I'm writing it for is an output file from a different program. In short, the program runs something, names the cycle=x on one line, provides stuff I need, then a variable amount of data. Earlier, each run was labeled cycle=1, cycle=2 etc, so that was simply a case of using "grep -A $numberoflines cycle=$a", successfully using the "for ((a=1; a<=100 ; a++))" to strip that from a file, and do what I wanted to it: Code:
#for ((n=1; n <= 9 ; n++)) From what I understood, the "for ((a=1; a<=100 ; a++))" line says start at 1, then go to another number (in this case 100) at regular intervals (a++), however it seems I'm wrong with this interpretation. The awk method does work and is murderously simple, but is admittedly something I haven't looked at at all yet (but this is a newbie forum, right? ;)) From what I understand from other posters I'm missing something small and easy to find from my first attempt, which I also admittedly haven't figured out yet, but I'll keep you posted. |
The thread is marked "[SOLVED]", what was the solution? (Be polite, give us feedback, let us & those who follow know what you did.)
I thought there was a missing '$', but Code:
for ((a=1; a<=10 ; a++)); do echo $a; done Code:
for ((a=1; $a<=10 ; a++)); do echo $a; done Personally, I would use awk. (See Taylor's Laws of Programming) |
sed isn't really the right tool for this: starting 10000 processes for a 10000 line file is ... not aesthetically pleasing. Plus, in circumstances other than casual use it can actually turn into a performance problem. So get used to avoiding things like that.
The awk oneliner is probably best: Code:
awk '/start/{sub(/start/,++n);{print}' Code:
awk '/^start$/{$0=++n};{print}' Code:
IFS='' n=0; while read; do [[ $REPLY == start ]] && REPLY=$((++n)) Code:
IFS=''; n=0 |
cat & sed .............too easy
cat & sed
as per: [root@hostest ~]# cat it.txt start content content start content content start content content [root@hostest ~]# sed '/^start/d' it.txt |cat -n |sed 's/content.*/\n&/' 1 content content 2 content content 3 content content [root@hostest ~]# |
rojee,
I think OP made a slight mistake in his sample file: 'start' is a literal, but 'content content' is not. Therefore you cannot expect a search for "content" to be useful. As I said, I would use awk & jthill was kind enough to provide 2 awk-based solutions. twoleggedtripod, It's a custom at LQ to give positive feedback to to those who tried to help you by telling what solution you adopted. That also helps create an archive of solutions for others in the future. Please give back to LQ, don't be a taker only. |
sed is powerfull ---- not as powerfull as "awk" but way easier
[root@hostest ~]# cat text.txt
start contantly changing content content start mary had a little lamb start and everwhere mary went [root@hostest ~]# [root@hostest ~]# [root@hostest ~]# sed '/^start/d' it.txt |cat -n |sed 's/\t.*/\n&/' 1 contantly changing content content 2 mary had a little lamb 3 and everwhere mary went [root@hostest ~]# |
Quote:
BTW, using '/' for the delimiter in a regex in both sed & perl is optional -- any character is allowed (RT:)M). Also, FWIW, code samples & ASCII files are best put in "Code:" blocks when posting on LQ. Long or wide ones go better in a pastebin. |
All times are GMT -5. The time now is 09:27 PM. |