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.
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.
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
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.
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 "$preline"ABCDE"$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.
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 "$preline"ABCDE"$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, "newline=" 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
a
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.
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 ":set list". 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.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.