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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi, I want to read n caracters o

Status
Not open for further replies.

rvarman

IS-IT--Management
Apr 24, 2003
91
FR
Hi,

I want to read n caracters on a line at a particular position from the input file ant replace with these caracters at particular positions in the output file.

Please give me your help.
 
are those characters typically the same, having some pattern or random but only in fixed positions? what about the ones you want to replace with?

would this fit what your file looks like, where you want to replace 3~7th characters?
xx00000xxx
xx01010xxxxx
xx00110xxx
xx10000xxxx
xx01010xxx
xx01110xxxxxxx
 
Input file looks as you describe. "date;time;card_nb;desc;"
In fact, the line doesn't exist in the output file.
But the line shd have a fixed length say 320.
So i think of creating an emty line with 320 stars or other caracters and then get the n caracters from the input file and replace it at the particular position replacing the stars in the output file.

Thanks for your help
 
This may not be the most efficient, but it should work the way you want, except there are only two * before and after the core items. you have to make it 320 long yourself.

#! /bin/ksh

while read line
do
echo `echo $line | cut -c3-7 ` >> /tmp/junk$$
done < oldfile

sed -n 's/^/\*\*/p' /tmp/junk$$ > /tmp/junk_$$
sed -n 's/$/\*\*/p' /tmp/junk_$$ > newfile

rm -f /tmp/junk$$ >/dev/null 2>&1
rm -f /tmp/junk_$$ >/dev/null 2>&1
 
Hi,
I've tried this. It gives me a good start.
But it is very dificult to construct the output file.
I want to replace the existing characters at nth position.

Thanks
 
Suppose you have a line of 320 long,
**...**abcde***.....****
suppose you have 100 before and 195 after the positions you want to replace. So you want to replace 101th~105th positions with ABCDE, command 'cut' is an immediate convenient way (though I have not tested. but reply if it does not work),

while read line
do
preline=`echo $line | cut -c3-100 `
sufline=`echo $line | cut -c106- `
newline=echo &quot;$preline&quot;ABCDE&quot;$SUFLINE >> newfile
unset preline
unset sufline
done < oldfile

But you could sense something is awkward. If possible, you could consider insert a special character b/t 5th & 6th and b/t 106th and to the end in the input file, so that you can use awk or sed to replace the 'abcde', wrapped the special characters, with the chars you want.
 
nice
I couldn't get the echo in a variable.

When i do :
preline=`echo $line | cut -c3-100 `
echo $preline

gives:
echo $line | cut -c3-100

How can i do ?

Thanx
 
make sure you use the back quotes in this line
preline=`echo $line | cut -c3-100 `

It sits under the ~
 
Thank you very much for your help, it works now ;-)

thanks again
 
it's me again, i have one last question about this script.

when i loop on line and do:
while read line
do
preline=`echo $line | cut -c3-100 `
sufline=`echo $line | cut -c106- `
newline=echo &quot;$preline&quot;ABCDE&quot;$SUFLINE >> newfile
unset preline
unset sufline
done < oldfile

it doesn't go until the last line, but the one before ... how can i get him to go to the last one ?
 
not totally understand your question. I had couple of typos, noteably, &quot;newline=&quot; should not be there. The location of unset commands should not matter. Here I tested on a small file called 'ain', as the oldfile. it looks like
abcdelast36913
Here is the script
#! /bin/ksh

> aout

while read line
do
unset preline
unset sufline
preline=`echo $line | cut -c1-4 `
sufline=`echo $line | cut -c10- `
echo &quot;$preline&quot;ABCDE&quot;$sufline&quot; >> aout
done < ain

Here is the new file &quot;aout&quot;
abcdelastABCDE
Could you use this example or your real files if you have more questions?
 
Hi,
your inputfile (oldfile) has 6 lines and also the output file has six.
Where as me, my output file has one line less.
i.e. it doesn't add the last line.

I've verified the input file. i've gone to the last line and did an enter. In such case, it works well and outputs all lines.
If i really stay in the last caracter of the last line(without making a CR), i never get the last line.

am i clear?
Thanks
 
If you were using my pseduo files, that is a little odd.
But if you use your own file and some lines have backslash,
command 'read' might continue to read from next line and combine the two lines before and after the blackslash. If that is the case, try 'read -r line' instead 'read line'.

also, when a line is too long, it might be wrapped up. As one of the post on this board suggested, in vi command mode, type &quot;:set list&quot;. Each line should end in a $ sign, when you can tell if a line is wrapped or in fact two lines.

However, if you want to turn your lines that are too long into two lines in output file, then you would need to know either a position or a signal as where to break that long line in the old file.

Hope it helps, but feel free to post.
 
Ok I'll try these things.
Thank you very much.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top