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.
What books and tutorials are you using?
I don't have any books.
Did you look at the grymoire site that I suggested?
I'm looking into the tutorial. But it'll take time. I posted this to see if I could get any clue in the mean time.
If you are trying to do this with AWK (as people have suggested), then what it the relevance of your question about SED?
I've tried this with awk, bt it doesn't seems working.
I asked about sed 'coz jschiwal (Moderator) & konsolebox (Senior Member) have suggested so.
Did you try the suggested SED solution? Did it work?
The suggestion is nothing particular. As I've no knowledge of sed I could nt modify it to my requirement.
Finally, in your post above, what is the distinction between "field" and "column"? (In most contexts, these terms would be synonymous)
By Field I mean --> there are many fields(columns) in a file that are comma/space separated.
By 8th Column I mean the 8th character of that row.
Q: What books and tutorials are you using?
A: I don't have any books.
Q: Did you look at the grymoire site that I suggested?
A: I'm looking into the tutorial. But it'll take time. I posted this to see if I could get any clue in the mean time.
The way this comes across is that you really don't want to put the effort into learning this stuff----that you would rather just have us give you the answer.
I'm not good with AWK, but I will tell you that trying to adapt someone else's suggestions without reading the man pages, tutorials, etc. is not likely to be very rewarding.
Quote:
By Field I mean --> there are many fields(columns) in a file that are comma/space separated.
By 8th Column I mean the 8th character of that row.
If there is no delimiter, then many programs (eg AWK) have no way of specifying position. You may want to look at things like "tr" or "cut".
In your example, you say it is trying to edit the 8th field---but there is only ONE field
In addition to the Grymoire tutorials, please go to http://tldp.org and get a copy of the Bash Guide for Beginners
That number specifies an address in the file. In this statement, it points to the 23rd row.
Code:
s
That command substitues values. Please see 'man sed' to know how the command works.
Code:
@
That one's the delimeter for the command. I use @ instead of /. If you look at the info about the s command of sed, / is the one that's always used.
Code:
\(....\)
That set matches the first four characters (. matches a single character so 4 of it should match 4. We place the dots around \( and \) so that we can use \1 to specify them back.
We can also change it to something like:
Code:
\(.\{4\}\)
to tell sed that . makes matches 4 times.
Code:
....
Now this will match the 4 characters that should be replaced.
Code:
@\1wxyz@
This should be the replacement for the first 8 characters. Remember that \1 will refer to the first 4.
If there is no delimiter, then many programs (eg AWK) have no way of specifying position.
that's so not true. setting FS to "" in (g)awk makes every character a field by itself. If you want position 8, then look for $8. likewise, using substr() as well.
Last edited by ghostdog74; 01-28-2010 at 03:24 AM.
that's so not true. setting FS to "" in (g)awk makes every character a field by itself. If you want position 8, then look for $8. likewise, using substr() as well.
My mistake---sorry
It's just that---at least for my pea-brain---it is not intuitive to think of every letter in a string as a field
#\033[$RowNum;$ColNumFrom H
#awk -v r=2 -v c=3 -v val=x 'BEGIN {NR!=r; NR==r {$c=val}}' Leena.csv
#r=2
#c=3
sed ''"$RoWNum"'s@\(.\{'"$ColNumFrom"'\}\)..@\1wx@' Leena_New
But its' throwing an error
Syntax error at line 11 : `"' is not matched.
I changed the sed command to
sed '"$RoWNum"s@\(.\{"$ColNumFrom"\}\)..@\1wx@' Leena_New
still the same error occured...
I again changed the sed to
sed ''$RoWNum's@\(.\{'$ColNumFrom'\}\)..@\1wx@' Leena_New
Error persisted.
I've removed "' from the script but still the error talks about it.
We have to copy a the file into a directory called My_Dir
But we don't have rights to copy a file in this particular dir (no rights of cp command on this dir).
So we ftp it.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.