Increasing a number and appending it to next line of a text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Increasing a number and appending it to next line of a text file
# 1  
Old 12-23-2010
Increasing a number and appending it to next line of a text file

Hi all,

I have text file having a number P100. what i need is when i run a script, it should add 1 to the above number and append it to the next line of a same text file.. when i use the script next time it should check the last line and add 1 to the last number and so on..

like the text file should something like below after several runs of a script..

Quote:
P100
P101
P102
P103 and so on...
Pls help me..

Thank you
# 2  
Old 12-23-2010
Code:
awk -FP '{no=$2;print;}END{no++;print "P"no >> file}' file=inputFile inputFile

# 3  
Old 12-23-2010
Or:
Code:
lastline=$(tail -1 file)
echo "$lastline" |awk '{sub("^P",""); print "P" ++$0}' >> file

# 4  
Old 12-23-2010
Quote:
Originally Posted by Franklin52
Or:
Code:
lastline=$(tail -1 file)
echo "$lastline" |awk '{sub("^P",""); print "P" ++$0}' >> file


after the first iteration when i try to run the script again it shows the last like is P100P101
# 5  
Old 12-23-2010
Franklin52's command may be rewritten as:
Code:
tail -1 file | awk '{sub("^P",""); print "P" ++$0}' >> file

Also this command should work.
Pls try again, if doesn't work, post the file content before and after running command. command also.
# 6  
Old 12-23-2010
yes working fine... thank you Franklin and Anurag
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Insert the line number from text file to filename output

Hi everyone :) I have a file "words.txt" containing hundreds of lines of text. Each line contains a slogan. Using the code below i am able to generate an image with the slogan text from each line. The image filename is saved matching the last word on each line. Example: Line 1: We do... (2 Replies)
Discussion started by: martinsmith
2 Replies

2. Shell Programming and Scripting

Appending a text to the top of each line

Platform : Oracle Linux 6.8 Shell : bash I have a file which has lines like below. These are SELECT queries (SQL) In each line, I want the word just after FROM keyword to be copied and printed on the top along with the word PROMPT. The words after FROM clause below are table names. So, they... (6 Replies)
Discussion started by: John K
6 Replies

3. Shell Programming and Scripting

editing line in text file adding number to value in file

I have a text file that has data like: Data "12345#22" Fred ID 12345 Age 45 Wilma Dino Data "123#22" Tarzan ID 123 Age 33 Jane I need to figure out a way of adding 1,000,000 to the specific lines (always same format) in the file, so it becomes: Data "1012345#22" Fred ID... (16 Replies)
Discussion started by: say170
16 Replies

4. Shell Programming and Scripting

add serial number begining of each line in a text file

Dear All, i am having text file below rama 123 done raju 456 not done keshav 123 done ............... ............ i want to add a serial number to this file, the output should be 1 rama 123 done 2 raju 456 not done 3 keshav 123 done 99 ............... 100 ............ ... (3 Replies)
Discussion started by: suryanarayana
3 Replies

5. Linux

Get a specific line number from a text file

Hello! All, Could you please tell me how to get a specific line number from a text file? For example below, ABC DEF ---> Get this line number, return to an variable GHI My OS is Linux. Thank you so much for your help in advance! (3 Replies)
Discussion started by: barryxian
3 Replies

6. Shell Programming and Scripting

extract the lines between specific line number from a text file

Hi I want to extract certain text between two line numbers like 23234234324 and 54446655567567 How do I do this with a simple sed or awk command? Thank you. ---------- Post updated at 06:16 PM ---------- Previous update was at 05:55 PM ---------- found it: sed -n '#1,#2p'... (1 Reply)
Discussion started by: return_user
1 Replies

7. Shell Programming and Scripting

Appending line number to each line and getting total number of lines

Hello, I need help in appending the line number of each line to the file and also to get the total number of lines. Can somebody please help me. I have a file say: abc def ccc ddd ffff The output should be: Instance1=abc Instance2=def Instance3=ccc Instance4=ddd Instance5=ffff ... (2 Replies)
Discussion started by: chiru_h
2 Replies

8. UNIX for Dummies Questions & Answers

Appending text to a number of similar filenames

Hi, I was wondering if there was a way to append something to filenames based on a wildcard. For example, if I have the following files in a directory: blah1 blah2 blah3 blah4 blah5 I want to rename these all to: blah1.txt blah2.txt blah3.txt blah4.txt blah5.txt Is there a... (4 Replies)
Discussion started by: Djaunl
4 Replies

9. Shell Programming and Scripting

Appending the line number and a seperator to each line of a file ?

Hi, I am a newb as far as shell scripting and SED goes so bear with me on this one. I want to basically append to each line in a file a delimiter character and the line's line number e.g Change the file from :- aaaaaa bbbbbb cccccc to:- aaaaaa;1 bbbbbb;2 cccccc;3 I have worked... (4 Replies)
Discussion started by: pjcwhite
4 Replies

10. Shell Programming and Scripting

Appending Text To Each Line That Matches Grep

I'm currently digging for a way to append a line to a text file where each line begins with the word "setmqaut". This is a continuation of my IBM MQSeries backup script I'm working on to make my life a little easier. What I would like to do is have each line that looks like this: setmqaut -m... (4 Replies)
Discussion started by: sysera
4 Replies
Login or Register to Ask a Question