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

Replacing first 8 characters in each line of text

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I have a fixed width file and I want to be able to give a user (in a script) an option to view the first 8 characters and 24-30 and change the first 8 to what they want.

I have started a script using cut and sed, but haven't got very far.

Please help. Here is the first part of the script that get's me the info. (The second field is the unique identifier. )

cut -c 1-8,24-30 datafile >outfile
echo "\n\nHere are the variables that can be changed...\n"
cat outfile

echo "\nPlease Enter exactly the strings that you want to change...\c"
read ans1;export ans1

sed 's/ $ans1/ALEXGGG/' outfile >newfile

#EOF:

(the $ans1 also doesn't work with sed)
 
Give this a shot:

sed "s/$ans1/ALEXGGG/" datafile > newfile

Cheers,
ND
 
To conform to your header information I think you want something closer to this. Maybe I'm off target though...

echo "\n\nHere are the variables that can be changed...\n"
sed 's/\(.\{8\}\).\{15\}\(.\{0,7\}\)/\1 \2/' datafile

echo "\nPlease Enter exactly the strings that you want to change...\c"
read ans1;export ans1

sed "s/$ans1/ALEXGGG/" datafile > newfile

Cheers,
ND [smile]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top