Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to insert/ delete a line in txt-file from a script?

Status
Not open for further replies.

Madz

Technical User
Jan 31, 2001
33
0
0
SE
Is it possible to first delete a line in a textfile and then insert a number on that row (replace the number)
for example:
Hello-1
1 <- change this row to 2.
Hello-2



thanks in advance! /mats.


 
Hi, I think before I can give you a decent answer, I would need to know a bit more info, for example whether you are always going to be changing the same line, or whether you plan on changing lines that meet a certain criteria. If the latter, what criteria would you be using?

Also, in the brief example you have given, you have replaced 1 with 2... is that all you would want to do? Increment a number within a text file? Or would the changes differ from case to case?

A little more to go on would be helpful.

Regards, LHLTech
IBM CS - AIX V4.3 System Support
Halfway through CATE exams!
 
Thanks for your response!

all I want to do is to increase numbers in a file.
for example:

Hello <- always the same
1 <- the 2:nd row should be increased to 2, and then 3,,,
(I have the numbers in a variable)
Hello2 <- row 3. always the same
1 <-should be increased with 1
hello3 &quot;&quot;
5 <- should be increased with 1.

I know that I can increase the numbers with:
sed /s/1/2/ file

but this will change all the 1:s in the file to 2.
I am unpacking files that comes in and a want to automatically check if they meet a specific criteria.
== match the numbers in the file.

How to do this?
The program is working right now, but if numbers in the file are the same, all the numbers will increase.

thanks in advance!

/mats


 
Before I give you a proper answer... can I just ask..

The lines with the numbers on... do they have anything else
on the line to the right of the number

e.g.
1 some stuff here

or is it purely the number?

If there is additional stuff on the line, how many digits may the number be?

I'll post a working reply as soon as I see your answer.
Regards, LHLTech

IBM Certified Specialist - AIX System Support
Halfway through CATE exams!
 
Hi,
I am not sure if it's you want to get, but maybe you can use for your stuff. It's not elegant solution but ... ;)

test_file:
hello-1
hello-2
hello-3

command to run:
awk '{print &quot;hello-&quot;substr($1,7,length($1)-6)+1}' test_file

desc:
+1 -> value to increase

Hope this helps a little bit
Bogdan

 
Thanks you your support bogdanc & LHLTech !!
The program works fine now! It increased the numbers I wanted!

/mats
 
I would do it something like this.. though it's perhaps a little more involved...

# usage
# myscript <input file>

infile=$1
tmpfile=t_$$.tmp
rm -f $tmpfile
linecount=1
while read line
do
case $linecount in
2|4|6)
let linecount=linecount+1
numvar=`echo $line`
let numvar=numvar+1
echo &quot;$numvar&quot; >> $tmpfile
;;
*) let linecount=linecount+1
echo &quot;$line&quot; >> $tmpfile
;;
esac
done < $infile
mv $tmpfile $infile


Maybe you can take something from that and use it in your own script.

Regards,
LHLTech

IBM Certified Specialist - AIX System Support
Halfway through CATE exams!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top