Operating System - HP-UX
1837656 Members
3019 Online
110117 Solutions
New Discussion

Re: 3rd line of Script modification

 
la_cool
Occasional Advisor

3rd line of Script modification

Hi,

I need to change the third line of the script such as ...

for instance : 3rd line value is 250,
I need to update the record by 350 and so forth ..

6 REPLIES 6
harry d brown jr
Honored Contributor

Re: 3rd line of Script modification


use vi ??

Are you saying that you have a script or that you need to write one, that will change the value in some file on line 3 from the value 250 to 350 ??

If you are changing a file(s), then are these files text type files?

live free or die
harry
Live Free or Die
Abdul Rahiman
Esteemed Contributor

Re: 3rd line of Script modification

If you want to replace "250" in the third line in of a file to "350", either use vi or
use this simple awk script,
-----250.awk---
BEGIN {
FS="\n"
OFS="\n"
RS=""
}
{
gsub ("250","350", $3)
print $0
}
---------

#awk -f 250.awk myfile

regds,
Abdul.
No unix, no fun
la_cool
Occasional Advisor

Re: 3rd line of Script modification

Hi,

I think I was not clear enough .. let me elaborate more in detail ..


I have a master script, that would actually change the content of the sub script(2 file) dynamically, and content of the sub script would be something like this

When I run the Parentscript.sh

child script content will be something like this (childscript.sh)

Content has 3 lines in the script

"125,435
121,test
450"

The value 450(+100) will be changed dynamically to 550 when I run the master script and again I run the master script, the value will be changed to 650 and so forth .. (

In short:
Childscript.sh - Value should be changed dynamically to +100 everytime When I run the master script ........


Jan Sladky
Trusted Contributor

Re: 3rd line of Script modification

in one file you can change it vi editor

vi file

:/%s/250/350/ enter

:x #save and quit

if you want script, simply like this:

#!/bin/bash

sed 's/250/350/' file.txt > changed_file.txt


br Jan
GSM, Intelligent Networks, UNIX
Dan Martin_1
Frequent Advisor

Re: 3rd line of Script modification

If I understand what you are asking, in the parent script use these lines of code:

typeset -i LINENUM=3
typeset -i INCR=100
typeset CHSCR="childscript.sh"
typeset -i i=0
typeset -i x=0

while read inline
do
[[ -n "$inline" || "$inline" = \#* ]] && continue
line[$x]="$inline"
((x += 1))
done < $CHSCR

# convert to a number
i="${line[$LINENUM]}"
((i+=INCR))
line[$LINENUM]="$i"

i=${#line[*]}
x=0


while ((x < i ))
do
echo "${line[$x]}"
((x += 1))
done >$CHSCR

=========================================

I am assuming that your quotes (") in the example are not actually in the childscript.sh file. If they are, you will need to change the "convert" code to:

i="${line[$LINENUM]%\"}"
((i+=INCR))
line[$LINENUM]="$i"\"


curt larson_1
Honored Contributor

Re: 3rd line of Script modification

f1=yourfile
tmpfile=/tmp/something$$

awk '
NR == 3 {
x=$1+0;
x+=100;
$1=x;
print $0;
next;
}
{print;}' $f1 > $tmpfile

if [ $? = 0 ] ;then
mv $tmpfile $f1
else
print "awk error"
fi