LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-04-2017, 11:08 AM   #1
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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.
Code:
$fix"Image-113-1301.png"  
$fixed=$(echo $fix | awk 'NR==1,/0/{sub(/1/,"0")} 1')                             
$echo $fixed                                                                      
Image-013-1301.png
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.
Code:
$fix"Image-013-1301.png"                                                         
$fixed=$(echo $fix | awk 'NR==1,/0/{sub(/1/,"0")} 1')                             
$echo $fixed                                                                      
Image-003-1301.png
so I can rename all of them that have the leading number 1 to a zero before I can move on.
 
Old 07-04-2017, 11:28 AM   #2
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
I'm not sure that I understand the problem. Maybe this will do.

With this InFile ...
Code:
Image-113-1301.png
Image-113-1302.png
Image-013-1302.png
Image-013-1303.png
... this awk ...
Code:
awk '{sub(/Image-1/,"Image-0"); print}' $InFile >$OutFile
... produced this OutFile ...
Code:
Image-013-1301.png
Image-013-1302.png
Image-013-1302.png
Image-013-1303.png
Daniel B. Martin
 
1 members found this post helpful.
Old 07-04-2017, 11:35 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,309
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
I'm not sure I understand either but about looking for a pattern at the start of a line, you need to anchor it with a caret ^

Code:
s/^0/1/;
Otherwise, without the anchor, the pattern will find any 0 anywhere in the string.
 
Old 07-04-2017, 11:58 AM   #4
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by danielbmartin View Post
I'm not sure that I understand the problem. Maybe this will do.

With this InFile ...
Code:
Image-113-1301.png
Image-113-1302.png
Image-013-1302.png
Image-013-1303.png
... this awk ...
Code:
awk '{sub(/Image-1/,"Image-0"); print}' $InFile >$OutFile
... produced this OutFile ...
Code:
Image-013-1301.png
Image-013-1302.png
Image-013-1302.png
Image-013-1303.png
Daniel B. Martin
thanks that worked!
Code:
#!/bin/bash

working_dir=/media/data/FIX-Numbering

while read FILENAME
do

f=$FILENAME
path=${f%/*}
xfile=${f##*/}
title=${xfile%.*}
ext=${xfile##*.}
fix="$(echo "$title" | awk '{sub(/Image-1/,"Image-0"); print}' )"
#test first
#echo "$fix"
mv -v "$FILENAME" "$path/$fix"."$ext"
done< <(find "$working_dir" -type f)
 
Old 07-04-2017, 12:06 PM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by Turbocapitalist View Post
I'm not sure I understand either but about looking for a pattern at the start of a line, you need to anchor it with a caret ^

Code:
s/^0/1/;
Otherwise, without the anchor, the pattern will find any 0 anywhere in the string.
thanks I was pattern thinking only the first number (digit) in the line
if = 1 then change it to zero (0)

a-11134-1123023

becomes

a-01134-1123023

that ^ looks good, I am just not that accustom with syntax of sed and awk (yet).

got it though thanks to the other poster. ! cheers.
 
Old 07-04-2017, 02:31 PM   #6
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Quote:
Originally Posted by BW-userx View Post
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.

Last edited by scasey; 07-04-2017 at 02:33 PM.
 
Old 07-04-2017, 02:51 PM   #7
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by scasey View Post
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.

Last edited by BW-userx; 07-04-2017 at 02:54 PM.
 
Old 07-05-2017, 12:05 PM   #8
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
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
 
Old 07-05-2017, 12:15 PM   #9
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
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'
 
Old 07-05-2017, 02:08 PM   #10
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by szboardstretcher View Post
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 View Post
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
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
awk script for having number of beacon sent for gpsr ranjani Linux - Networking 1 04-03-2014 09:07 AM
[SOLVED] awk column number in for loop tomasdepomas Linux - General 2 09-25-2012 02:31 AM
Limit in number of fields that awk can handle ? chargaff Programming 4 03-03-2011 08:36 AM
Using awk to grab first number series buee Linux - Newbie 1 02-03-2010 04:08 PM
square root of number using sqrt and/or awk skuz_ball Programming 4 08-04-2008 04:18 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration