[SOLVED] A little help with awk, just the first number- if 1 then 0 else skip it.
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.
A little help with awk, just the first number- if 1 then 0 else skip it.
I got a bunch of image file with the numbering system as such
Image-113-1301.png
Image-113-1302.png
and others with
Image-013-1302.png
Image-013-1303.png
all I want to work with an change is the first set of digits with the leading number, or first number being
if 1 then 0 else skip it.
I've got awk to change the first instance of a 1 to 0, but that is not what I need.
that above one works but if the 1 is the second digit or more then it wil change that one instead, which I do not need. It needs to just do nothing to it.
I am just not that accustom with syntax of sed and awk (yet).
It's less about sed and awk (or perl or grep) than it is about "regular expressions" (regex) -- the power of those utilities lies in understanding how regular expressions work and the pattern matching therein. Web searches and study will help, but the learning curve is medium steep. Still, once you have your head wrapped around them, all those utilities become "easy" to use.
One thing I did was use grep to spin through web and email logs to understand how patterns get matched. Another great tool for me was in the SciTE editor, which has a "Regular Expression" option on it's Find and Replace functions. Very useful to have it highlight a regex find to see I did (or didn't) have the regex as I wanted it.
Learn regex first, then go to sed or awk or grep or perl to learn the syntax for applying them.
It's less about sed and awk (or perl or grep) than it is about "regular expressions" (regex) -- the power of those utilities lies in understanding how regular expressions work and the pattern matching therein. Web searches and study will help, but the learning curve is medium steep. Still, once you have your head wrapped around them, all those utilities become "easy" to use.
One thing I did was use grep to spin through web and email logs to understand how patterns get matched. Another great tool for me was in the SciTE editor, which has a "Regular Expression" option on it's Find and Replace functions. Very useful to have it highlight a regex find to see I did (or didn't) have the regex as I wanted it.
Learn regex first, then go to sed or awk or grep or perl to learn the syntax for applying them.
I do understand that is is /search pattern/replace pattern
it is not the first occurrence that matches then change, I was looking for the first char in a pattern that is a one to be changed among a bunch of data that has the same like pattern but different numbers - that is what got me. for example
image-110-1234
image-110-1235
image-001-1234
I was thinking- just looking at the entire string then moving down it left to right one char at a time until it sees the first number in the string then checking just that one digit -- is it a '1' one or other than a one?
if '1' then change it to '0' if not '1' then skip it goto next file line of thought I was stuck on.
it was more of a seek and destroy then a pattern match mind set I was using.
then I seen that Image-1/Image-0 then my brain just knew that was the answer I was needing.
I suppose there is a regex way to identify the first '1' in a string and then replace it. I've not had much luck with that. When I try to identify the first instance of a character, the match usually includes all instances. I've never figured out how to do that and just usually resort to something like the solution here. So, to be clear, while I'm saying that the power of sed/awk/grep lies in understanding regex, I'm not claiming to be an expert with it; I learn more each day
Python makes it easy to replace the first occurrence of a thing:
Code:
>>> import re
>>> filename = "Image-113-1301.png"
>>> re.sub("1","0",filename,1)
'Image-013-1301.png'
But that would change the first '1' in every filename, whereas the stated constraint is...
Quote:
Originally Posted by BW-userx
I got a bunch of image file with the numbering system as such
Image-113-1301.png
Image-113-1302.png
and others with
Image-013-1302.png
Image-013-1303.png
all I want to work with an change is the first set of digits with the leading number, or first number being
if 1 then 0 else skip it.
For that you need to provide some context for the '1' you want to change which is provided by the leading 'Image-' part, among other ways. Context is the idea behind regex anchors, ^=first of string, $=end of string, and user defined leading/trailing characters.
Last edited by astrogeek; 07-05-2017 at 02:13 PM.
Reason: typo, expanded context idea
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.