Replacing a particular position in a file thru shell script in Linux
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.
The file has three fields....Requirement is in the second column if the first 6 characters match the string then the first six characters only should be replaced with another string..[if the last 6 or the any combination of six characters in the second column matches other than first 6 characters, then it should not be replaced]...Any help on this??
How about this ($1 is the string, $2 is the replacement, $3 is the file):
Code:
#!/bin/bash
rm -f .$3
grep ".*" "$3" | while read nextline
do
{
begin="`echo "$nextline" | sed -r "s*\"[^\"\n]*\":\"[^\"\n]*\"$**"`"
end="`echo "$nextline" | sed -r "s*^$begin**"`"
end2="`echo "$end" | sed -r "s*^\"$1*/\"$2*"`"
echo "$begin$end2" >> .$3
}
done
echo "" > $3
grep ".*" ".$3" >> $3
rm -f .$3
Sorry, couldn't test it because I don't have a Linux box here right now.
It should take any line "...":"...":"..." and turn it into "...": and "...":"...". Then "...":"..." is checked for "(before) which is replaced with "(after) then is put back together with "...":. (I'm sure I over-complicated it...)
ta0kira
#!/bin/bash
text="[^\"\n]*"
quoted_text="\"$text\""
sed -ri "s/^($quoted_text:\")$1($text\":$quoted_text)$/\1$2\2/g" $3
Maybe this? Again, no Linux here so it might need to be tweaked. ($1 is the string, $2 is the replacement, $3 is the file)
ta0kira
PS
Quote:
i think the most ineffecient script ever written is this one! looping + temp files are fatal deficiencies; eh?
Yeah, I didn't like the looping in mine either, but I don't think you can really get around the temp file unless you store the entire file in a $ variable...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.