How can I use Shell script to edit a data at a particular location in a txt file?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
previously, file was like
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
H D R 3 0 1 0 2 0 0 9 1 0 3 0 5 0
D T A 1 0 0 0 5 X 1 0 0 0 0 0 . 5 0
D T A 1 0 2 0 1 X 2 0 0 0 0 0 . 5 0
T R L 3 0 0 0 0 1 . 0 0 0 0 0 0 0 0 . 0 0 0 4
I say, edit at row 2, col 9-10 location & whatever be the previous data, change it to xy
User I/P would be file name, row num, column from & to, and new val.
Later (after running the script)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
H D R 3 0 1 0 2 0 0 9 1 0 3 0 5 0
D T A 1 0 0 0 x y 1 0 0 0 0 0 . 5 0
D T A 1 0 2 0 1 X 2 0 0 0 0 0 . 5 0
T R L 3 0 0 0 0 1 . 0 0 0 0 0 0 0 0 . 0 0 0 4
the file is not space separated.
the above file is a flat file with nothing called as field.
Though it looks like, bt assume that there are no spaces in the above example, & the data is continuous.
I tried
awk -v r=3 -v c=8 -v val=x 'BEGIN {}; NR != r {print}; NR == r {$c = val; print}' Leena_New
But this treats the file as a space separated file & edits the 8th Field insted of 8th column.
I'm still not sure from your description whether the line you are editing is:
D T A 1 0 0 0 5 X 1 0 0 0 0 0 . 5 0
or
DTA10005x100000.50
When talking about rows you are starting with record 0. When you talk about columns, you start counting with 1.
For "DTA10005x100000.50", you can use sed. It will be easier if you use extended regular expressions. (less escaping)
Use storage registers to save what is before and after the characters to replace.
You could also look at reading each line into a bash array variable and editing the elements.
awk has functions that deal with manipulating substrings.
So there are a number of ways of doing what you want.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.