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 append to existing lines at specific position?

Status
Not open for further replies.

gopalm73

Programmer
Jul 10, 2001
2
0
0
US
Hi,

I am trying to append a particular text to each line of the file starting from 200th position. The file contains lines of varied length of below 200 characters.

So, I want to make each line 200 char long with padded blanks and then append some text startin from 201th position. Please help.

Thanks
Gopal M
 
Here is an awk program that will do what you want.

{
while (length < 200) $0 = $0 &quot; &quot;
$0 = $0 &quot;Text at end&quot;
print
}

Alternatively you could check thread271-141285 in the awk forum for another way to pad a line.

Hope this helps
CaKiwi
 
Hi,

The script is given below and it works fine except for the $PERIOD_NAME that does not pick the value of the variable. It instead prints the $PERIOD_NAME literally.

Is there a way to make it substitute the value instead of printing the text as such?

cat /tmp/$SYSRESP.$reqid.dat.tmp | nawk ' { while (length < 300) $0 = $0 &quot; &quot;
$0 = $0 &quot;$PERIOD_NAME&quot;
print
} ' > /tmp/$SYSRESP.$reqid.new


Any help would be greatly appreciated.

Thanks
Gopal M
 
Where is the value for $PERIOD_NAME coming from? Is it coming from an outside source?
I don't know about nawk but with awk you would use
cat /tmp/$SYSRESP.$reqid.dat.tmp | awk -v PERIOD_NAME=$VARIABLE ' { while (length < 300) $0 = $0 &quot; &quot;
$0 = $0 $PERIOD_NAME
print
} ' > /tmp/$SYSRESP.$reqid.new

SOL
The best thing about banging your head against a wall is when you stop.
 
SOL is correct and will work for nawk as well as awk but I believe it should be

$0 =$0 PERIOD_NAME

not

$0 =$0 $PERIOD_NAME

Also it is a little simpler to write

nawk '{...}' /tmp/$SYSRESP.$reqid.dat.tmp > /tmp/$SYSRESP.$reqid.new

rather than using cat.

Hope this helps.

CaKiwi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top