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.
Basic Examples, not the whole answer. You finish it.
Code:
a=(
0.500000
-0.50000
-1.001-1
-1.002-1
-0.50000
)
for i in "${a[@]}"; do
echo "Last 2 are : "${i: -2}""
done
for i in "${a[@]}"; do
if [[ "${i: -2}" =~ ^- ]]; then
echo "These end in -? : "$i""
fi
done
for i in "${a[@]}"; do
if [[ "${i: -2}" =~ ^- ]]; then
echo "${i%??}"
else
echo "$i"
fi
done
for i in "${a[@]}"; do
if [[ "${i: -2}" =~ ^- ]]; then
echo "${i%??}e-"
else
echo "$i"
fi
done
could do, but it still only replaces everything with "1e-" then.
Rather than telling others that their suggestions do not work for some case, try to understand why they do not work and from that point ask yourself how they might be modified to work for the case at hand and try to make the change! You will learn much more and more quickly that way!
When attempting to write more complex regular expressions, I find it very useful to write out a description of what I want in concise, plain language, then using that as a sort of specification try to implement the behavior in a regular expression.
Again, please review the Site FAQ for guidance in asking well formed questions. Especially visit the link from that page, How to Ask Questions the Smart Way for discussion of things to consider when asking others for help.
As others have said, your solution is in awk and not bash and there is no for loop, so the starting point would be to right your question with the correct information.
My first question would be, why are you setting FS and OFS to something that does not appear in your input?
Based on your requirement:
[quote]go through every line and replace, if present, the second minus sign by e-.[/code]
I assume this means we only want to worry about lines with 2 minus signs, so the following would be ignored:
Code:
1.000-1
The above being the case, your main function call should have something telling it to firstly, only act on lines with 2 minus signs:
Code:
awk 'split($0,a,"-") == 3{<do change here>}' file
Then the other nice thing you could look at in the manual is the gensub command:
Code:
gensub(regexp, replacement, how [, target])
Focus on the "how":
Quote:
If how is a string beginning with ‘g’ or ‘G’ (short for “global”), then replace all matches of regexp with replacement. Otherwise, treat how as a number indicating which match of regexp to replace.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.