Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

Weekends were made for programming. -- Karl Lehenbauer


devel / comp.lang.awk / Copying files with generated filenames in a directory from inside teh AWKprogram

SubjectAuthor
* Copying files with generated filenames in a directory from inside teh AWKprogramMohsen Owzar
+* Re: Copying files with generated filenames in a directory from insideJanis Papanagnou
|+- Re: Copying files with generated filenames in a directory from insideMohsen Owzar
|`* Re: Copying files with generated filenames in a directory from insideMohsen Owzar
| `* Re: Copying files with generated filenames in a directory from insideJanis Papanagnou
|  `* Re: Copying files with generated filenames in a directory from insideJanis Papanagnou
|   `* Re: Copying files with generated filenames in a directory from insideMohsen Owzar
|    `* Re: Copying files with generated filenames in a directory from insideJanis Papanagnou
|     `- Re: Copying files with generated filenames in a directory from insideMohsen Owzar
+- Re: Copying files with generated filenames in a directory from insideEd Morton
`* Re: Copying files with generated filenames in a directory from insideBruce Horrocks
 `* Re: Copying files with generated filenames in a directory from insideJanis Papanagnou
  `* Re: Copying files with generated filenames in a directory from insideBruce Horrocks
   +- Re: Copying files with generated filenames in a directory from insideJanis Papanagnou
   +* Re: Copying files with generated filenames in a directory from insideKenny McCormack
   |`- Re: Copying files with generated filenames in a directory from insideJanis Papanagnou
   `* Re: Copying files with generated filenames in a directory from insideEd Morton
    `* Re: Copying files with generated filenames in a directory from insideBruce Horrocks
     `* Re: Copying files with generated filenames in a directory from insideMohsen Owzar
      +* Re: Copying files with generated filenames in a directory from insideJanis Papanagnou
      |`* Re: Copying files with generated filenames in a directory from insideBruce Horrocks
      | +- Re: Copying files with generated filenames in a directory from insideEd Morton
      | `* Re: Copying files with generated filenames in a directory from insideKenny McCormack
      |  `* Re: Copying files with generated filenames in a directory from inside teh AWKproMohsen Owzar
      |   `* Re: Copying files with generated filenames in a directory from insideJ Naman
      |    `- Re: Copying files with generated filenames in a directory from insideMohsen Owzar
      `- Re: Copying files with generated filenames in a directory from insideEd Morton

Pages:12
Copying files with generated filenames in a directory from inside teh AWKprogram

<7faa87f4-881f-4b0b-8e79-d25a638bfe75n@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=905&group=comp.lang.awk#905

  copy link   Newsgroups: comp.lang.awk
X-Received: by 2002:a0c:816b:: with SMTP id 98mr10887978qvc.37.1632195196318;
Mon, 20 Sep 2021 20:33:16 -0700 (PDT)
X-Received: by 2002:a5b:603:: with SMTP id d3mr35572665ybq.421.1632195196088;
Mon, 20 Sep 2021 20:33:16 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.awk
Date: Mon, 20 Sep 2021 20:33:15 -0700 (PDT)
Injection-Info: google-groups.googlegroups.com; posting-host=31.165.67.106; posting-account=BtTavwoAAACMi3AFrA3U-JBERbxGUGhD
NNTP-Posting-Host: 31.165.67.106
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <7faa87f4-881f-4b0b-8e79-d25a638bfe75n@googlegroups.com>
Subject: Copying files with generated filenames in a directory from inside teh AWKprogram
From: mohsen.owzar@gmail.com (Mohsen Owzar)
Injection-Date: Tue, 21 Sep 2021 03:33:16 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 171
 by: Mohsen Owzar - Tue, 21 Sep 2021 03:33 UTC

Hi Guys
I'm working with Cygwin under Windows 10
I have written a program in AWK / Sh to copy or rename some selected files, filtered by a string given by the user, from 01 to the length of the selected files by the filter.
Before you say that this task can be done with another software than AWK, why I used this, OK, I know a bit only AWK and Bash scripting, and if you know another and more effective method than this one, do not hesitate and offer your suggestion.
But the thing is after so many hours struggling with this stuff, I want to know, how can I get these two variables (old and new filenames) out of AWK to copy or move
When I see a video tutorial, I take screenshots sporadically when some actions are done.
The thing is that the screenshots have generic names derived from the date and the name of the video and therefore can be very long, as you can see in the list below:
----------------------------------------------------
2021-09-08 10_10_42-PyQt5 - Apply your own Style using CSS - second method - updated - YouTube.png
2021-09-08 10_10_55-PyQt5 - Apply your own Style using CSS - second method - updated - YouTube.png
2021-09-08 10_13_37-Python PyQt5 -5- Make your first interaction with buttons using Signals Slots - .png
2021-09-08 10_13_46-Python PyQt5 -5- Make your first interaction with buttons using Signals Slots - .png
2021-09-10 07_08_53-Make Button Flashing _ PyQt5 Tutorial - YouTube.png
2021-09-10 07_12_40-Make Button Flashing _ PyQt5 Tutorial - YouTube.png
2021-09-15 09_19_39-Window.Window.Window.Window.png
2021-09-15 09_19_54-Window.png
2021-09-15 09_20_06-Window.png
----------------------------------------------------
Due to his issue, I wanted to give the user the possibility, to run my script with an argument, which is a part of the screenshot name. My AWK program picks this string and takes files containing this string and change their names to names beginning with two digits for 01 to the length of the selected files and concatenated with the given string.
For example:
2021-09-10 07_08_53-Make Button Flashing _ PyQt5 Tutorial - YouTube.png
2021-09-10 07_12_40-Make Button Flashing _ PyQt5 Tutorial - YouTube.png
with the given string as: " Button Flashing" is changed to
01_Button Flashing.png
02_Button Flashing.png
Now my problem:
I have managed to get the string from the user and sort the files in the directory by time and this string, and to get the old_file and new_file names..
I'm struggling since a couple of days to copy or move these files from old names to new names, vain and futile. It brings, all the time, that something is wrong with the destination and I have to check with "cp –help"..
I think one problem is due to the fact that I'm using Linux commands (Cygwin) under Windows (CR/LF problem).
Another problem is that the filenames have spaces, and therefore I had to replace them forwards and backwards with some Characters. And the error messages after running the script has to do with this issue.
Because I found after a couple of hours, the given string had one character more than I need (I think CR/LF). Therefore, I had to get rid of the last character, as you can see in my code.
I have tried all combinations with system() command or print, no success. I hope someone can help me with this. I don't see the jungle because of all the trees.
Any help would be very appreciated.
Here is the output of the script:
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
$> Rename_Screenshots_For_String.sh "Button Flashing"

===============================================Date and given arguments
===============================================Generated on: 2021_09_21.05_05
TOP = /cygdrive/c/Users/m.owzar/Desktop/teesst
String = Button Flashing
DATUM = 2021_09_21.05_05
===============================================*** 2021-09-10 07_08_53-Make Button Flashing _ PyQt5 Tutorial - YouTube.png --> 01_Button_Flashing.png
*** 2021-09-10 07_12_40-Make Button Flashing _ PyQt5 Tutorial - YouTube.png --> 02_Button_Flashing.png

2 Files were found!
--------------------------------------------
71 : 2021-09-10 07_08_53-Make Button Flashing _ PyQt5 Tutorial - YouTube.png
22 : 01_Button_Flashing.png
/bin/cp: zusätzlicher Operand 'Button'
„/bin/cp --help“ liefert weitere Informationen.
71 : 2021-09-10 07_12_40-Make Button Flashing _ PyQt5 Tutorial - YouTube.png
22 : 02_Button_Flashing.png
/bin/cp: zusätzlicher Operand 'Button'
„/bin/cp --help“ liefert weitere Informationen.
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
And here is my code:
#! /usr/bin/bash
# Filename: Rename_Screenshots_For_String.sh
#########################################################
String=$1
DATUM=`date +"%Y_%m_%d.%H_%M"`
echo ""
echo "================================================"
echo "Date and given arguments"
echo "================================================"
echo "Generated on: " $DATUM
# ======================================================ls -rt | grep -i "$1" | awk -v TOP="$(pwd)" -v String="$String" -v DATUM="$DATUM" '
########################################
## Begin Part
########################################
BEGIN {
print "TOP = " TOP
print "String = " String
print "DATUM = " DATUM
print "================================================"
}

########################################
## Main Part
########################################
{
if ($0 ~ / /) {
A = gensub(/ /, "_XXX_", "g", $0)
Arr_A[++N] = A
}

B = gensub(/_XXX_/, " ", "g", A)
Arr_B[++M] = B
}

########################################
## End Part
########################################

END {
Str = gensub(/ /, "_", "g", String)

for (i = 1; i <= M; i++) {
old_file[i] = Arr_B[i]

K = split(old_file[i], ARRAY, ".")
Extension = ARRAY[K]

Str_2 = substr(Str, 1, length(Str)-1)
new_file[i] = sprintf("%02d_%s.%s", i, Str_2, Extension)

printf "*** %s --> %s\n", old_file[i], new_file[i]

}
printf "\n%s Files were found!\n", M
print "--------------------------------------------"
for (i = 1; i <= M; i++) {
print length(old_file[i]), ": ", old_file[i]
print length(new_file[i]), ": ", new_file[i]

system("/bin/cp -T " old_file[i] " " new_file[i])
# print ""old_file[i]"" > new_file[i]
}
}
'

Re: Copying files with generated filenames in a directory from inside teh AWKprogram

<sic49t$1pok$1@gioia.aioe.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=906&group=comp.lang.awk#906

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!i2pn.org!aioe.org!LTi+xflewmQTbbz7H4hZOQ.user.46.165.242.75.POSTED!not-for-mail
From: janis_papanagnou@hotmail.com (Janis Papanagnou)
Newsgroups: comp.lang.awk
Subject: Re: Copying files with generated filenames in a directory from inside
teh AWKprogram
Date: Tue, 21 Sep 2021 10:14:53 +0200
Organization: Aioe.org NNTP Server
Message-ID: <sic49t$1pok$1@gioia.aioe.org>
References: <7faa87f4-881f-4b0b-8e79-d25a638bfe75n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="59156"; posting-host="LTi+xflewmQTbbz7H4hZOQ.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.13.0
Content-Language: en-US
X-Notice: Filtered by postfilter v. 0.9.2
 by: Janis Papanagnou - Tue, 21 Sep 2021 08:14 UTC

A couple comments, pick your options...

Generally it's simpler to do the file manipulation in shell.
(If you provide a terse example and clear description the folks
(here or better in comp.unix.shell) will help you.)

If possible, get rid of punctuation characters and spaces.
Specifically get rid of the quotes as part of the file names.
Keep punctuation in date/time and gsub() all other punctuation
to become spaces. Then gsub() all sequences of spaces to single
underscore (for example).
Since the source file name still has spaces you need to quote the
whole file name that you provide as argument to mv/cp as in
filename = "\"" filename "\""
To do the renaming either construct the shell commands and print
them for execution awk '{printf ... }' | sh or use system(...)
to do it
cmd = "mv -i \"" filename "\" " newname
To prevent name clashes of the transformed names memorize them in
an array and (for example) add numbering if necessary
if (newname in names) names[newname]++
newname = names[newname] newname
For the shell 'mv' command use shell option '-i' to create an
error instead of overwriting existing files (just in case that
even the numbering creates an already existing file name).

There's probably even more hints necessary. A concise post with
a clear example and concrete questions will help you to get your
answers.

HTH.

Janis

PS: And try to keep your line lengths in your posts <78 characters;
this is still Usenet (not a webforum), search for Usenet Netiquette.

On 21.09.21 05:33, Mohsen Owzar wrote:
> Hi Guys
> I'm working with Cygwin under Windows 10 I have written a program in
> AWK / Sh to copy or rename some selected
files, filtered by a string given by the user, from 01 to the length of
the selected files by the filter.
> Before you say that this task can be done with another software than
AWK, why I used this, OK, I know a bit only AWK and Bash scripting, and
if you know another and more effective method than this one, do not
hesitate and offer your suggestion.
> But the thing is after so many hours struggling with this stuff, I
want to know, how can I get these two variables (old and new filenames)
out of AWK to copy or move
> When I see a video tutorial, I take screenshots sporadically when
> some
actions are done.
> The thing is that the screenshots have generic names derived from
> the
date and the name of the video and therefore can be very long, as you
can see in the list below:
> ----------------------------------------------------
> 2021-09-08 10_10_42-PyQt5 - Apply your own Style using CSS - second method - updated - YouTube.png
> 2021-09-08 10_10_55-PyQt5 - Apply your own Style using CSS - second method - updated - YouTube.png
> 2021-09-08 10_13_37-Python PyQt5 -5- Make your first interaction with buttons using Signals Slots - .png
> 2021-09-08 10_13_46-Python PyQt5 -5- Make your first interaction with buttons using Signals Slots - .png
> 2021-09-10 07_08_53-Make Button Flashing _ PyQt5 Tutorial - YouTube.png
> 2021-09-10 07_12_40-Make Button Flashing _ PyQt5 Tutorial - YouTube.png
> 2021-09-15 09_19_39-Window.Window.Window.Window.png
> 2021-09-15 09_19_54-Window.png
> 2021-09-15 09_20_06-Window.png
> ----------------------------------------------------
> Due to his issue, I wanted to give the user the possibility, to run
> my
script with an argument, which is a part of the screenshot name. My AWK
program picks this string and takes files containing this string and
change their names to names beginning with two digits for 01 to the
length of the selected files and concatenated with the given string.
> For example:
> 2021-09-10 07_08_53-Make Button Flashing _ PyQt5 Tutorial - YouTube.png
> 2021-09-10 07_12_40-Make Button Flashing _ PyQt5 Tutorial - YouTube.png
> with the given string as: " Button Flashing" is changed to
> 01_Button Flashing.png
> 02_Button Flashing.png
> Now my problem:
> I have managed to get the string from the user and sort the files in the directory by time and this string, and to get the old_file and new_file names.
> I'm struggling since a couple of days to copy or move these files from old names to new names, vain and futile. It brings, all the time, that something is wrong with the destination and I have to check with "cp –help".
> I think one problem is due to the fact that I'm using Linux commands (Cygwin) under Windows (CR/LF problem).
> Another problem is that the filenames have spaces, and therefore I had to replace them forwards and backwards with some Characters. And the error messages after running the script has to do with this issue.
> Because I found after a couple of hours, the given string had one character more than I need (I think CR/LF). Therefore, I had to get rid of the last character, as you can see in my code.
> I have tried all combinations with system() command or print, no success. I hope someone can help me with this. I don't see the jungle because of all the trees.
> Any help would be very appreciated.
> Here is the output of the script:
> &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
> $> Rename_Screenshots_For_String.sh "Button Flashing"
>
> ================================================
> Date and given arguments
> ================================================
> Generated on: 2021_09_21.05_05
> TOP = /cygdrive/c/Users/m.owzar/Desktop/teesst
> String = Button Flashing
> DATUM = 2021_09_21.05_05
> ================================================
> *** 2021-09-10 07_08_53-Make Button Flashing _ PyQt5 Tutorial - YouTube.png --> 01_Button_Flashing.png
> *** 2021-09-10 07_12_40-Make Button Flashing _ PyQt5 Tutorial - YouTube.png --> 02_Button_Flashing.png
>
> 2 Files were found!
> --------------------------------------------
> 71 : 2021-09-10 07_08_53-Make Button Flashing _ PyQt5 Tutorial - YouTube.png
> 22 : 01_Button_Flashing.png
> /bin/cp: zusätzlicher Operand 'Button'
> „/bin/cp --help“ liefert weitere Informationen.
> 71 : 2021-09-10 07_12_40-Make Button Flashing _ PyQt5 Tutorial - YouTube.png
> 22 : 02_Button_Flashing.png
> /bin/cp: zusätzlicher Operand 'Button'
> „/bin/cp --help“ liefert weitere Informationen.
> &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
> And here is my code:
> #! /usr/bin/bash
> # Filename: Rename_Screenshots_For_String.sh
> #########################################################
> String=$1
> DATUM=`date +"%Y_%m_%d.%H_%M"`
> echo ""
> echo "================================================"
> echo "Date and given arguments"
> echo "================================================"
> echo "Generated on: " $DATUM
> # =======================================================
> ls -rt | grep -i "$1" | awk -v TOP="$(pwd)" -v String="$String" -v DATUM="$DATUM" '
> ########################################
> ## Begin Part
> ########################################
> BEGIN {
> print "TOP = " TOP
> print "String = " String
> print "DATUM = " DATUM
> print "================================================"
> }
>
> ########################################
> ## Main Part
> ########################################
> {
> if ($0 ~ / /) {
> A = gensub(/ /, "_XXX_", "g", $0)
> Arr_A[++N] = A
> }
>
> B = gensub(/_XXX_/, " ", "g", A)
> Arr_B[++M] = B
> }
>
> ########################################
> ## End Part
> ########################################
>
> END {
> Str = gensub(/ /, "_", "g", String)
>
> for (i = 1; i <= M; i++) {
> old_file[i] = Arr_B[i]
>
> K = split(old_file[i], ARRAY, ".")
> Extension = ARRAY[K]
>
> Str_2 = substr(Str, 1, length(Str)-1)
> new_file[i] = sprintf("%02d_%s.%s", i, Str_2, Extension)
>
> printf "*** %s --> %s\n", old_file[i], new_file[i]
>
> }
> printf "\n%s Files were found!\n", M
> print "--------------------------------------------"
>
> for (i = 1; i <= M; i++) {
> print length(old_file[i]), ": ", old_file[i]
> print length(new_file[i]), ": ", new_file[i]
>
> system("/bin/cp -T " old_file[i] " " new_file[i])
> # print ""old_file[i]"" > new_file[i]
> }
> }
> '
>


Click here to read the complete article
Re: Copying files with generated filenames in a directory from inside teh AWKprogram

<7b134ecf-5304-47e1-bdce-0fe5a49231a1n@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=907&group=comp.lang.awk#907

  copy link   Newsgroups: comp.lang.awk
X-Received: by 2002:ac8:5142:: with SMTP id h2mr14391485qtn.92.1632213781025;
Tue, 21 Sep 2021 01:43:01 -0700 (PDT)
X-Received: by 2002:a25:b9cf:: with SMTP id y15mr34171629ybj.110.1632213780819;
Tue, 21 Sep 2021 01:43:00 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.awk
Date: Tue, 21 Sep 2021 01:43:00 -0700 (PDT)
In-Reply-To: <sic49t$1pok$1@gioia.aioe.org>
Injection-Info: google-groups.googlegroups.com; posting-host=31.165.67.106; posting-account=BtTavwoAAACMi3AFrA3U-JBERbxGUGhD
NNTP-Posting-Host: 31.165.67.106
References: <7faa87f4-881f-4b0b-8e79-d25a638bfe75n@googlegroups.com> <sic49t$1pok$1@gioia.aioe.org>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <7b134ecf-5304-47e1-bdce-0fe5a49231a1n@googlegroups.com>
Subject: Re: Copying files with generated filenames in a directory from inside
teh AWKprogram
From: mohsen.owzar@gmail.com (Mohsen Owzar)
Injection-Date: Tue, 21 Sep 2021 08:43:01 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 231
 by: Mohsen Owzar - Tue, 21 Sep 2021 08:43 UTC

Janis Papanagnou schrieb am Dienstag, 21. September 2021 um 10:14:55 UTC+2:
> A couple comments, pick your options...
>
> Generally it's simpler to do the file manipulation in shell.
> (If you provide a terse example and clear description the folks
> (here or better in comp.unix.shell) will help you.)
>
> If possible, get rid of punctuation characters and spaces.
> Specifically get rid of the quotes as part of the file names.
> Keep punctuation in date/time and gsub() all other punctuation
> to become spaces. Then gsub() all sequences of spaces to single
> underscore (for example).
> Since the source file name still has spaces you need to quote the
> whole file name that you provide as argument to mv/cp as in
> filename = "\"" filename "\""
> To do the renaming either construct the shell commands and print
> them for execution awk '{printf ... }' | sh or use system(...)
> to do it
> cmd = "mv -i \"" filename "\" " newname
> To prevent name clashes of the transformed names memorize them in
> an array and (for example) add numbering if necessary
> if (newname in names) names[newname]++
> newname = names[newname] newname
> For the shell 'mv' command use shell option '-i' to create an
> error instead of overwriting existing files (just in case that
> even the numbering creates an already existing file name).
>
> There's probably even more hints necessary. A concise post with
> a clear example and concrete questions will help you to get your
> answers.
>
> HTH.
>
> Janis
>
> PS: And try to keep your line lengths in your posts <78 characters;
> this is still Usenet (not a webforum), search for Usenet Netiquette.
> On 21.09.21 05:33, Mohsen Owzar wrote:
> > Hi Guys
> > I'm working with Cygwin under Windows 10 I have written a program in
> > AWK / Sh to copy or rename some selected
> files, filtered by a string given by the user, from 01 to the length of
> the selected files by the filter.
> > Before you say that this task can be done with another software than
> AWK, why I used this, OK, I know a bit only AWK and Bash scripting, and
> if you know another and more effective method than this one, do not
> hesitate and offer your suggestion.
> > But the thing is after so many hours struggling with this stuff, I
> want to know, how can I get these two variables (old and new filenames)
> out of AWK to copy or move
> > When I see a video tutorial, I take screenshots sporadically when
> > some
> actions are done.
> > The thing is that the screenshots have generic names derived from
> > the
> date and the name of the video and therefore can be very long, as you
> can see in the list below:
> > ----------------------------------------------------
> > 2021-09-08 10_10_42-PyQt5 - Apply your own Style using CSS - second method - updated - YouTube.png
> > 2021-09-08 10_10_55-PyQt5 - Apply your own Style using CSS - second method - updated - YouTube.png
> > 2021-09-08 10_13_37-Python PyQt5 -5- Make your first interaction with buttons using Signals Slots - .png
> > 2021-09-08 10_13_46-Python PyQt5 -5- Make your first interaction with buttons using Signals Slots - .png
> > 2021-09-10 07_08_53-Make Button Flashing _ PyQt5 Tutorial - YouTube.png
> > 2021-09-10 07_12_40-Make Button Flashing _ PyQt5 Tutorial - YouTube.png
> > 2021-09-15 09_19_39-Window.Window.Window.Window.png
> > 2021-09-15 09_19_54-Window.png
> > 2021-09-15 09_20_06-Window.png
> > ----------------------------------------------------
> > Due to his issue, I wanted to give the user the possibility, to run
> > my
> script with an argument, which is a part of the screenshot name. My AWK
> program picks this string and takes files containing this string and
> change their names to names beginning with two digits for 01 to the
> length of the selected files and concatenated with the given string.
> > For example:
> > 2021-09-10 07_08_53-Make Button Flashing _ PyQt5 Tutorial - YouTube.png
> > 2021-09-10 07_12_40-Make Button Flashing _ PyQt5 Tutorial - YouTube.png
> > with the given string as: " Button Flashing" is changed to
> > 01_Button Flashing.png
> > 02_Button Flashing.png
> > Now my problem:
> > I have managed to get the string from the user and sort the files in the directory by time and this string, and to get the old_file and new_file names.
> > I'm struggling since a couple of days to copy or move these files from old names to new names, vain and futile. It brings, all the time, that something is wrong with the destination and I have to check with "cp –help".
> > I think one problem is due to the fact that I'm using Linux commands (Cygwin) under Windows (CR/LF problem).
> > Another problem is that the filenames have spaces, and therefore I had to replace them forwards and backwards with some Characters. And the error messages after running the script has to do with this issue.
> > Because I found after a couple of hours, the given string had one character more than I need (I think CR/LF). Therefore, I had to get rid of the last character, as you can see in my code.
> > I have tried all combinations with system() command or print, no success. I hope someone can help me with this. I don't see the jungle because of all the trees.
> > Any help would be very appreciated.
> > Here is the output of the script:
> > &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
> > $> Rename_Screenshots_For_String.sh "Button Flashing"
> >
> > ================================================
> > Date and given arguments
> > ================================================
> > Generated on: 2021_09_21.05_05
> > TOP = /cygdrive/c/Users/m.owzar/Desktop/teesst
> > String = Button Flashing
> > DATUM = 2021_09_21.05_05
> > ================================================
> > *** 2021-09-10 07_08_53-Make Button Flashing _ PyQt5 Tutorial - YouTube..png --> 01_Button_Flashing.png
> > *** 2021-09-10 07_12_40-Make Button Flashing _ PyQt5 Tutorial - YouTube..png --> 02_Button_Flashing.png
> >
> > 2 Files were found!
> > --------------------------------------------
> > 71 : 2021-09-10 07_08_53-Make Button Flashing _ PyQt5 Tutorial - YouTube.png
> > 22 : 01_Button_Flashing.png
> > /bin/cp: zusätzlicher Operand 'Button'
> > „/bin/cp --help“ liefert weitere Informationen.
> > 71 : 2021-09-10 07_12_40-Make Button Flashing _ PyQt5 Tutorial - YouTube.png
> > 22 : 02_Button_Flashing.png
> > /bin/cp: zusätzlicher Operand 'Button'
> > „/bin/cp --help“ liefert weitere Informationen.
> > &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
> > And here is my code:
> > #! /usr/bin/bash
> > # Filename: Rename_Screenshots_For_String.sh
> > #########################################################
> > String=$1
> > DATUM=`date +"%Y_%m_%d.%H_%M"`
> > echo ""
> > echo "================================================"
> > echo "Date and given arguments"
> > echo "================================================"
> > echo "Generated on: " $DATUM
> > # =======================================================
> > ls -rt | grep -i "$1" | awk -v TOP="$(pwd)" -v String="$String" -v DATUM="$DATUM" '
> > ########################################
> > ## Begin Part
> > ########################################
> > BEGIN {
> > print "TOP = " TOP
> > print "String = " String
> > print "DATUM = " DATUM
> > print "================================================"
> > }
> >
> > ########################################
> > ## Main Part
> > ########################################
> > {
> > if ($0 ~ / /) {
> > A = gensub(/ /, "_XXX_", "g", $0)
> > Arr_A[++N] = A
> > }
> >
> > B = gensub(/_XXX_/, " ", "g", A)
> > Arr_B[++M] = B
> > }
> >
> > ########################################
> > ## End Part
> > ########################################
> >
> > END {
> > Str = gensub(/ /, "_", "g", String)
> >
> > for (i = 1; i <= M; i++) {
> > old_file[i] = Arr_B[i]
> >
> > K = split(old_file[i], ARRAY, ".")
> > Extension = ARRAY[K]
> >
> > Str_2 = substr(Str, 1, length(Str)-1)
> > new_file[i] = sprintf("%02d_%s.%s", i, Str_2, Extension)
> >
> > printf "*** %s --> %s\n", old_file[i], new_file[i]
> >
> > }
> > printf "\n%s Files were found!\n", M
> > print "--------------------------------------------"
> >
> > for (i = 1; i <= M; i++) {
> > print length(old_file[i]), ": ", old_file[i]
> > print length(new_file[i]), ": ", new_file[i]
> >
> > system("/bin/cp -T " old_file[i] " " new_file[i])
> > # print ""old_file[i]"" > new_file[i]
> > }
> > }
> > '
> >
Oh Thanks alot for your explanation.
I'll try to follow your steps to see if I'm able to manage this task.


Click here to read the complete article
Re: Copying files with generated filenames in a directory from inside teh AWKprogram

<60f87554-ede5-4c63-b57a-2b81e4b3a19dn@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=908&group=comp.lang.awk#908

  copy link   Newsgroups: comp.lang.awk
X-Received: by 2002:ac8:86:: with SMTP id c6mr10068477qtg.78.1632222985149;
Tue, 21 Sep 2021 04:16:25 -0700 (PDT)
X-Received: by 2002:a25:3894:: with SMTP id f142mr40823612yba.464.1632222984919;
Tue, 21 Sep 2021 04:16:24 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.awk
Date: Tue, 21 Sep 2021 04:16:24 -0700 (PDT)
In-Reply-To: <sic49t$1pok$1@gioia.aioe.org>
Injection-Info: google-groups.googlegroups.com; posting-host=31.165.67.106; posting-account=BtTavwoAAACMi3AFrA3U-JBERbxGUGhD
NNTP-Posting-Host: 31.165.67.106
References: <7faa87f4-881f-4b0b-8e79-d25a638bfe75n@googlegroups.com> <sic49t$1pok$1@gioia.aioe.org>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <60f87554-ede5-4c63-b57a-2b81e4b3a19dn@googlegroups.com>
Subject: Re: Copying files with generated filenames in a directory from inside
teh AWKprogram
From: mohsen.owzar@gmail.com (Mohsen Owzar)
Injection-Date: Tue, 21 Sep 2021 11:16:25 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 277
 by: Mohsen Owzar - Tue, 21 Sep 2021 11:16 UTC

Janis Papanagnou schrieb am Dienstag, 21. September 2021 um 10:14:55 UTC+2:
> A couple comments, pick your options...
>
> Generally it's simpler to do the file manipulation in shell.
> (If you provide a terse example and clear description the folks
> (here or better in comp.unix.shell) will help you.)
>
> If possible, get rid of punctuation characters and spaces.
> Specifically get rid of the quotes as part of the file names.
> Keep punctuation in date/time and gsub() all other punctuation
> to become spaces. Then gsub() all sequences of spaces to single
> underscore (for example).
> Since the source file name still has spaces you need to quote the
> whole file name that you provide as argument to mv/cp as in
> filename = "\"" filename "\""
> To do the renaming either construct the shell commands and print
> them for execution awk '{printf ... }' | sh or use system(...)
> to do it
> cmd = "mv -i \"" filename "\" " newname
> To prevent name clashes of the transformed names memorize them in
> an array and (for example) add numbering if necessary
> if (newname in names) names[newname]++
> newname = names[newname] newname
> For the shell 'mv' command use shell option '-i' to create an
> error instead of overwriting existing files (just in case that
> even the numbering creates an already existing file name).
>
> There's probably even more hints necessary. A concise post with
> a clear example and concrete questions will help you to get your
> answers.
>
> HTH.
>
> Janis
>
> PS: And try to keep your line lengths in your posts <78 characters;
> this is still Usenet (not a webforum), search for Usenet Netiquette.
> On 21.09.21 05:33, Mohsen Owzar wrote:
> > Hi Guys
> > I'm working with Cygwin under Windows 10 I have written a program in
> > AWK / Sh to copy or rename some selected
> files, filtered by a string given by the user, from 01 to the length of
> the selected files by the filter.
> > Before you say that this task can be done with another software than
> AWK, why I used this, OK, I know a bit only AWK and Bash scripting, and
> if you know another and more effective method than this one, do not
> hesitate and offer your suggestion.
> > But the thing is after so many hours struggling with this stuff, I
> want to know, how can I get these two variables (old and new filenames)
> out of AWK to copy or move
> > When I see a video tutorial, I take screenshots sporadically when
> > some
> actions are done.
> > The thing is that the screenshots have generic names derived from
> > the
> date and the name of the video and therefore can be very long, as you
> can see in the list below:
> > ----------------------------------------------------
> > 2021-09-08 10_10_42-PyQt5 - Apply your own Style using CSS - second method - updated - YouTube.png
> > 2021-09-08 10_10_55-PyQt5 - Apply your own Style using CSS - second method - updated - YouTube.png
> > 2021-09-08 10_13_37-Python PyQt5 -5- Make your first interaction with buttons using Signals Slots - .png
> > 2021-09-08 10_13_46-Python PyQt5 -5- Make your first interaction with buttons using Signals Slots - .png
> > 2021-09-10 07_08_53-Make Button Flashing _ PyQt5 Tutorial - YouTube.png
> > 2021-09-10 07_12_40-Make Button Flashing _ PyQt5 Tutorial - YouTube.png
> > 2021-09-15 09_19_39-Window.Window.Window.Window.png
> > 2021-09-15 09_19_54-Window.png
> > 2021-09-15 09_20_06-Window.png
> > ----------------------------------------------------
> > Due to his issue, I wanted to give the user the possibility, to run
> > my
> script with an argument, which is a part of the screenshot name. My AWK
> program picks this string and takes files containing this string and
> change their names to names beginning with two digits for 01 to the
> length of the selected files and concatenated with the given string.
> > For example:
> > 2021-09-10 07_08_53-Make Button Flashing _ PyQt5 Tutorial - YouTube.png
> > 2021-09-10 07_12_40-Make Button Flashing _ PyQt5 Tutorial - YouTube.png
> > with the given string as: " Button Flashing" is changed to
> > 01_Button Flashing.png
> > 02_Button Flashing.png
> > Now my problem:
> > I have managed to get the string from the user and sort the files in the directory by time and this string, and to get the old_file and new_file names.
> > I'm struggling since a couple of days to copy or move these files from old names to new names, vain and futile. It brings, all the time, that something is wrong with the destination and I have to check with "cp –help".
> > I think one problem is due to the fact that I'm using Linux commands (Cygwin) under Windows (CR/LF problem).
> > Another problem is that the filenames have spaces, and therefore I had to replace them forwards and backwards with some Characters. And the error messages after running the script has to do with this issue.
> > Because I found after a couple of hours, the given string had one character more than I need (I think CR/LF). Therefore, I had to get rid of the last character, as you can see in my code.
> > I have tried all combinations with system() command or print, no success. I hope someone can help me with this. I don't see the jungle because of all the trees.
> > Any help would be very appreciated.
> > Here is the output of the script:
> > &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
> > $> Rename_Screenshots_For_String.sh "Button Flashing"
> >
> > ================================================
> > Date and given arguments
> > ================================================
> > Generated on: 2021_09_21.05_05
> > TOP = /cygdrive/c/Users/m.owzar/Desktop/teesst
> > String = Button Flashing
> > DATUM = 2021_09_21.05_05
> > ================================================
> > *** 2021-09-10 07_08_53-Make Button Flashing _ PyQt5 Tutorial - YouTube..png --> 01_Button_Flashing.png
> > *** 2021-09-10 07_12_40-Make Button Flashing _ PyQt5 Tutorial - YouTube..png --> 02_Button_Flashing.png
> >
> > 2 Files were found!
> > --------------------------------------------
> > 71 : 2021-09-10 07_08_53-Make Button Flashing _ PyQt5 Tutorial - YouTube.png
> > 22 : 01_Button_Flashing.png
> > /bin/cp: zusätzlicher Operand 'Button'
> > „/bin/cp --help“ liefert weitere Informationen.
> > 71 : 2021-09-10 07_12_40-Make Button Flashing _ PyQt5 Tutorial - YouTube.png
> > 22 : 02_Button_Flashing.png
> > /bin/cp: zusätzlicher Operand 'Button'
> > „/bin/cp --help“ liefert weitere Informationen.
> > &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
> > And here is my code:
> > #! /usr/bin/bash
> > # Filename: Rename_Screenshots_For_String.sh
> > #########################################################
> > String=$1
> > DATUM=`date +"%Y_%m_%d.%H_%M"`
> > echo ""
> > echo "================================================"
> > echo "Date and given arguments"
> > echo "================================================"
> > echo "Generated on: " $DATUM
> > # =======================================================
> > ls -rt | grep -i "$1" | awk -v TOP="$(pwd)" -v String="$String" -v DATUM="$DATUM" '
> > ########################################
> > ## Begin Part
> > ########################################
> > BEGIN {
> > print "TOP = " TOP
> > print "String = " String
> > print "DATUM = " DATUM
> > print "================================================"
> > }
> >
> > ########################################
> > ## Main Part
> > ########################################
> > {
> > if ($0 ~ / /) {
> > A = gensub(/ /, "_XXX_", "g", $0)
> > Arr_A[++N] = A
> > }
> >
> > B = gensub(/_XXX_/, " ", "g", A)
> > Arr_B[++M] = B
> > }
> >
> > ########################################
> > ## End Part
> > ########################################
> >
> > END {
> > Str = gensub(/ /, "_", "g", String)
> >
> > for (i = 1; i <= M; i++) {
> > old_file[i] = Arr_B[i]
> >
> > K = split(old_file[i], ARRAY, ".")
> > Extension = ARRAY[K]
> >
> > Str_2 = substr(Str, 1, length(Str)-1)
> > new_file[i] = sprintf("%02d_%s.%s", i, Str_2, Extension)
> >
> > printf "*** %s --> %s\n", old_file[i], new_file[i]
> >
> > }
> > printf "\n%s Files were found!\n", M
> > print "--------------------------------------------"
> >
> > for (i = 1; i <= M; i++) {
> > print length(old_file[i]), ": ", old_file[i]
> > print length(new_file[i]), ": ", new_file[i]
> >
> > system("/bin/cp -T " old_file[i] " " new_file[i])
> > # print ""old_file[i]"" > new_file[i]
> > }
> > }
> > '
> >
Hi Janis,


Click here to read the complete article
Re: Copying files with generated filenames in a directory from inside teh AWKprogram

<sicl7p$4bd$1@gioia.aioe.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=909&group=comp.lang.awk#909

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!i2pn.org!aioe.org!LTi+xflewmQTbbz7H4hZOQ.user.46.165.242.75.POSTED!not-for-mail
From: janis_papanagnou@hotmail.com (Janis Papanagnou)
Newsgroups: comp.lang.awk
Subject: Re: Copying files with generated filenames in a directory from inside
teh AWKprogram
Date: Tue, 21 Sep 2021 15:03:37 +0200
Organization: Aioe.org NNTP Server
Message-ID: <sicl7p$4bd$1@gioia.aioe.org>
References: <7faa87f4-881f-4b0b-8e79-d25a638bfe75n@googlegroups.com>
<sic49t$1pok$1@gioia.aioe.org>
<60f87554-ede5-4c63-b57a-2b81e4b3a19dn@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="4461"; posting-host="LTi+xflewmQTbbz7H4hZOQ.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.13.0
Content-Language: en-US
X-Notice: Filtered by postfilter v. 0.9.2
 by: Janis Papanagnou - Tue, 21 Sep 2021 13:03 UTC

On 21.09.21 13:16, Mohsen Owzar wrote:
> [...]
>
> Other question in this regard
> If I want to ask user to copy, rename or abort (Cc, Rr or Nn)
> before invoking the system() command
> I used the following snippet code
>
> ::::::::::::::::::::::::::::::::::::::::::
> for (i = 1; i <= M; i++) {
> print length(old_file[i]), ": ", old_file[i]
> print length(new_file[i]), ": ", new_file[i]
>
> printf "Do you want to Copy [Cc] or Rename [Rr], otherwise [Nn]:"
> getline answer < "-"
>
> answer = tolower(answer)
>
> if (answer == "c")
> cmd = "cp -i \"" old_file[i] "\" " new_file[i]
> else if (answer == "r")
> cmd = "mv -i \"" old_file[i] "\" " new_file[i]
> else
> print "You ommited Copying / Renaming files!"
>
> system(cmd)
> }
> ::::::::::::::::::::::::::::::::::::::::::
> It doesn't stop at the line containing the "getline" command and waits for user answer.
> It goes through all if, else if and else part and gives out
> only the message from the else part, e.g.
>
> Do you want to Copy [Cc] or Rename [Rr], otherwise [Nn]:You ommited Copying / Renaming files!
>
> can you also help me out of this dilema?

Probably not, because there's not enough information for me to
judge, and 'getline' is a problematic statement anyway. There's
several uncertainties I'd have. - Is your code above part of the
regular standard-input processing loop or executed in the BEGIN
clause? Is "-" a valid standard representation for the standard
input channel in your version of awk? What awk are you using?
There's also no >0 test coded whether getline succedded; it may
provide useful diagnostics. Maybe getline answer < "/dev/tty"
works better? How is variable 'cmd' initialized? What are the
requirements when nothing is entered; executing an empty string,
or the 'cmd' variable's value from the previously looped file?

Janis

Re: Copying files with generated filenames in a directory from inside teh AWKprogram

<sicpie$be2$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=910&group=comp.lang.awk#910

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: mortonspam@gmail.com (Ed Morton)
Newsgroups: comp.lang.awk
Subject: Re: Copying files with generated filenames in a directory from inside
teh AWKprogram
Date: Tue, 21 Sep 2021 09:17:50 -0500
Organization: A noiseless patient Spider
Lines: 31
Message-ID: <sicpie$be2$1@dont-email.me>
References: <7faa87f4-881f-4b0b-8e79-d25a638bfe75n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 21 Sep 2021 14:17:50 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="75915f19ed66884a8c4b3a69880c9541";
logging-data="11714"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+7+7rctCICCdqPt0lyiJdg"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.14.0
Cancel-Lock: sha1:aGH7MrbEMCuBXUBTlfJ+DwIQUtk=
In-Reply-To: <7faa87f4-881f-4b0b-8e79-d25a638bfe75n@googlegroups.com>
X-Antivirus-Status: Clean
Content-Language: en-US
X-Antivirus: Avast (VPS 210921-0, 9/20/2021), Outbound message
 by: Ed Morton - Tue, 21 Sep 2021 14:17 UTC

On 9/20/2021 10:33 PM, Mohsen Owzar wrote:
> Hi Guys
> I'm working with Cygwin under Windows 10
> I have written a program in AWK / Sh to copy or rename some selected files, filtered by a string given by the user, from 01 to the length of the selected files by the filter.
<snip>

Please create and post a minimal example of just the problem you're
asking for help with. Include that minimal code plus concise, testable
sample input and expected output so we can help you.

Keep in mind, though, that awk is a tool to manipulate text while shell
is a tool to manipulate files and processes with a language to sequence
calls to other tools so this approach I see in your code where shell
calls awk to call system to spawn a subshell to call another command
such as cp:

shell { awk { system { shell { cp } } } }

is usually wrong as it's extremely inefficient and usually written in a
fragile way (e.g. in your code you're leaving the arguments to cp
unquoted and so open to globbing, filename expansion, malicious command
execution, etc.), and the correct approach, if some kind of text
manipulation means it's useful to call awk for part of the job, would be
this instead where shell calls awk to parse some text and return some
strings and then shell calls cp using that awk output:

shell { awk; cp }

Regards,

Ed.

Re: Copying files with generated filenames in a directory from inside teh AWKprogram

<sicv8d$1c0r$1@gioia.aioe.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=911&group=comp.lang.awk#911

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!i2pn.org!aioe.org!LTi+xflewmQTbbz7H4hZOQ.user.46.165.242.75.POSTED!not-for-mail
From: janis_papanagnou@hotmail.com (Janis Papanagnou)
Newsgroups: comp.lang.awk
Subject: Re: Copying files with generated filenames in a directory from inside
teh AWKprogram
Date: Tue, 21 Sep 2021 17:54:52 +0200
Organization: Aioe.org NNTP Server
Message-ID: <sicv8d$1c0r$1@gioia.aioe.org>
References: <7faa87f4-881f-4b0b-8e79-d25a638bfe75n@googlegroups.com>
<sic49t$1pok$1@gioia.aioe.org>
<60f87554-ede5-4c63-b57a-2b81e4b3a19dn@googlegroups.com>
<sicl7p$4bd$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="45083"; posting-host="LTi+xflewmQTbbz7H4hZOQ.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.13.0
Content-Language: en-US
X-Notice: Filtered by postfilter v. 0.9.2
 by: Janis Papanagnou - Tue, 21 Sep 2021 15:54 UTC

On 21.09.21 15:03, Janis Papanagnou wrote:
> On 21.09.21 13:16, Mohsen Owzar wrote:
>> [...]
>>
>> Other question in this regard
>> If I want to ask user to copy, rename or abort (Cc, Rr or Nn)
>> before invoking the system() command
>> I used the following snippet code
>>
>> ::::::::::::::::::::::::::::::::::::::::::
>> for (i = 1; i <= M; i++) {
>> print length(old_file[i]), ": ", old_file[i]
>> print length(new_file[i]), ": ", new_file[i]
>>
>> printf "Do you want to Copy [Cc] or Rename [Rr], otherwise [Nn]:"
>> getline answer < "-"
>>
>> answer = tolower(answer)
>>
>> if (answer == "c")
>> cmd = "cp -i \"" old_file[i] "\" " new_file[i]
>> else if (answer == "r")
>> cmd = "mv -i \"" old_file[i] "\" " new_file[i]
>> else
>> print "You ommited Copying / Renaming files!"
>>
>> system(cmd)
>> }
>> ::::::::::::::::::::::::::::::::::::::::::
>> It doesn't stop at the line containing the "getline" command and waits for user answer.
>> It goes through all if, else if and else part and gives out
>> only the message from the else part, e.g.
>>
>> Do you want to Copy [Cc] or Rename [Rr], otherwise [Nn]:You ommited Copying / Renaming files!
>>
>> can you also help me out of this dilema?
>
> Probably not, because there's not enough information for me to
> judge, and 'getline' is a problematic statement anyway. There's
> several uncertainties I'd have. - Is your code above part of the
> regular standard-input processing loop or executed in the BEGIN
> clause? Is "-" a valid standard representation for the standard
> input channel in your version of awk? What awk are you using?
> There's also no >0 test coded whether getline succedded; it may
> provide useful diagnostics. Maybe getline answer < "/dev/tty"
> works better? How is variable 'cmd' initialized? What are the
> requirements when nothing is entered; executing an empty string,
> or the 'cmd' variable's value from the previously looped file?

Reinspecting code from your original post it seems you are feeding
filenames through the standard input channel (i.e. using pipes),
so if you also use the same channel for user's commands selection
input you will have a problem. Using, as proposed, "/dev/tty"
instead of "- might thus already solve the issue with your input
channels, but I haven't analyzed your code so that there might be
yet more issues (like the also mentioned uninitialized variables,
etc.).

> Janis
>

Re: Copying files with generated filenames in a directory from inside teh AWKprogram

<544ddaaa-075d-4f29-9784-778491eacf9dn@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=912&group=comp.lang.awk#912

  copy link   Newsgroups: comp.lang.awk
X-Received: by 2002:ac8:7dd4:: with SMTP id c20mr26033568qte.46.1632306587038;
Wed, 22 Sep 2021 03:29:47 -0700 (PDT)
X-Received: by 2002:a5b:603:: with SMTP id d3mr44587446ybq.421.1632306586836;
Wed, 22 Sep 2021 03:29:46 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.awk
Date: Wed, 22 Sep 2021 03:29:46 -0700 (PDT)
In-Reply-To: <sicv8d$1c0r$1@gioia.aioe.org>
Injection-Info: google-groups.googlegroups.com; posting-host=31.165.67.106; posting-account=BtTavwoAAACMi3AFrA3U-JBERbxGUGhD
NNTP-Posting-Host: 31.165.67.106
References: <7faa87f4-881f-4b0b-8e79-d25a638bfe75n@googlegroups.com>
<sic49t$1pok$1@gioia.aioe.org> <60f87554-ede5-4c63-b57a-2b81e4b3a19dn@googlegroups.com>
<sicl7p$4bd$1@gioia.aioe.org> <sicv8d$1c0r$1@gioia.aioe.org>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <544ddaaa-075d-4f29-9784-778491eacf9dn@googlegroups.com>
Subject: Re: Copying files with generated filenames in a directory from inside
teh AWKprogram
From: mohsen.owzar@gmail.com (Mohsen Owzar)
Injection-Date: Wed, 22 Sep 2021 10:29:47 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
 by: Mohsen Owzar - Wed, 22 Sep 2021 10:29 UTC

Janis Papanagnou schrieb am Dienstag, 21. September 2021 um 17:54:56 UTC+2:
> On 21.09.21 15:03, Janis Papanagnou wrote:
> > On 21.09.21 13:16, Mohsen Owzar wrote:
> >> [...]
> >>
> >> Other question in this regard
> >> If I want to ask user to copy, rename or abort (Cc, Rr or Nn)
> >> before invoking the system() command
> >> I used the following snippet code
> >>
> >> ::::::::::::::::::::::::::::::::::::::::::
> >> for (i = 1; i <= M; i++) {
> >> print length(old_file[i]), ": ", old_file[i]
> >> print length(new_file[i]), ": ", new_file[i]
> >>
> >> printf "Do you want to Copy [Cc] or Rename [Rr], otherwise [Nn]:"
> >> getline answer < "-"
> >>
> >> answer = tolower(answer)
> >>
> >> if (answer == "c")
> >> cmd = "cp -i \"" old_file[i] "\" " new_file[i]
> >> else if (answer == "r")
> >> cmd = "mv -i \"" old_file[i] "\" " new_file[i]
> >> else
> >> print "You ommited Copying / Renaming files!"
> >>
> >> system(cmd)
> >> }
> >> ::::::::::::::::::::::::::::::::::::::::::
> >> It doesn't stop at the line containing the "getline" command and waits for user answer.
> >> It goes through all if, else if and else part and gives out
> >> only the message from the else part, e.g.
> >>
> >> Do you want to Copy [Cc] or Rename [Rr], otherwise [Nn]:You ommited Copying / Renaming files!
> >>
> >> can you also help me out of this dilema?
> >
> > Probably not, because there's not enough information for me to
> > judge, and 'getline' is a problematic statement anyway. There's
> > several uncertainties I'd have. - Is your code above part of the
> > regular standard-input processing loop or executed in the BEGIN
> > clause? Is "-" a valid standard representation for the standard
> > input channel in your version of awk? What awk are you using?
> > There's also no >0 test coded whether getline succedded; it may
> > provide useful diagnostics. Maybe getline answer < "/dev/tty"
> > works better? How is variable 'cmd' initialized? What are the
> > requirements when nothing is entered; executing an empty string,
> > or the 'cmd' variable's value from the previously looped file?
> Reinspecting code from your original post it seems you are feeding
> filenames through the standard input channel (i.e. using pipes),
> so if you also use the same channel for user's commands selection
> input you will have a problem. Using, as proposed, "/dev/tty"
> instead of "- might thus already solve the issue with your input
> channels, but I haven't analyzed your code so that there might be
> yet more issues (like the also mentioned uninitialized variables,
> etc.).
>
> > Janis
> >
Hi Janis

I'm answering to your last post at 15:03 because of your questions.

First, my GAWK version on Windows 10 under Cygwin:

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
$> gawk --version
GNU Awk 5.1.0, API: 3.0 (GNU MPFR 4.0.2-p6, GNU MP 6.2.0)
Copyright © 1989, 1991-2020 Free Software Foundation.
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::

I tried to use getline with GAWK to see if 'getline a < "-"' works.
The following ist the output:

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
$> gawk ' {printf "Type a number: "; getline a < "-"; print "You typed " a} '

Type a number: 123
You typed 123

Type a number:
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::

With /dev/tty has the same output:

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
$> gawk ' {printf "number?: "; getline a < "/dev/tty"; print "a = " a} '

number?: 5
a = 5

number?:
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::

It seems to be that both versions work.
And your last question if this "for loop" is placed in the BEGIN part
No

I didn't change anything in this script except the last for loop
in the END part with inserted "getline". I put again this END part below:

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
END {
Str = gensub(/ /, "_", "g", String)

for (i = 1; i <= M; i++) {
old_file[i] = Arr_B[i]

K = split(old_file[i], ARRAY, ".")
Extension = ARRAY[K]

Str_2 = substr(Str, 1, length(Str)-1)
new_file[i] = sprintf("%02d_%s.%s", i, Str_2, Extension)

printf "*** %s --> %s\n", old_file[i], new_file[i]

}
printf "\n%s Files were found!\n", M
print "--------------------------------------------"
for (i = 1; i <= M; i++) {
print length(old_file[i]), ": ", old_file[i]
print length(new_file[i]), ": ", new_file[i]

printf "Do you want to Copy [Cc] or Rename [Rr], otherwise [Nn]: "
getline answer < "/dev/tty"

answer = tolower(answer)

if (answer == "c")
cmd = "/bin/cp -i \"" old_file[i] "\" " new_file[i]
else if (answer == "r")
cmd = "/bin/mv -i \"" old_file[i] "\" " new_file[i]
else
print "You ommited Copying / Renaming files!"

system(cmd)
}
}
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::

After inserting "/dev/tty" instead of "-", it waits for the user input
and it copies when I type "c" and Enter.

In other words, it means that I can have user inputs from within AWK only with:

getline answer < "/dev/tty"

Other than this method, there is no other possibility to get user input,
not in the BEGIN part for the command arguments?

Nevertheless, I thank you and Ed very much to show me the correct way
and how can I get running my code in spite of my bad coding style.

Mohsen

Re: Copying files with generated filenames in a directory from inside teh AWKprogram

<sif36u$vqt$1@gioia.aioe.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=913&group=comp.lang.awk#913

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!i2pn.org!aioe.org!LTi+xflewmQTbbz7H4hZOQ.user.46.165.242.75.POSTED!not-for-mail
From: janis_papanagnou@hotmail.com (Janis Papanagnou)
Newsgroups: comp.lang.awk
Subject: Re: Copying files with generated filenames in a directory from inside
teh AWKprogram
Date: Wed, 22 Sep 2021 13:14:37 +0200
Organization: Aioe.org NNTP Server
Message-ID: <sif36u$vqt$1@gioia.aioe.org>
References: <7faa87f4-881f-4b0b-8e79-d25a638bfe75n@googlegroups.com>
<sic49t$1pok$1@gioia.aioe.org>
<60f87554-ede5-4c63-b57a-2b81e4b3a19dn@googlegroups.com>
<sicl7p$4bd$1@gioia.aioe.org> <sicv8d$1c0r$1@gioia.aioe.org>
<544ddaaa-075d-4f29-9784-778491eacf9dn@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="32605"; posting-host="LTi+xflewmQTbbz7H4hZOQ.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.13.0
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: en-US
 by: Janis Papanagnou - Wed, 22 Sep 2021 11:14 UTC

On 22.09.21 12:29, Mohsen Owzar wrote:
> After inserting "/dev/tty" instead of "-", it waits for the user input
> and it copies when I type "c" and Enter.
>
> In other words, it means that I can have user inputs from within AWK
> only with:
>
> getline answer < "/dev/tty"
>
> Other than this method, there is no other possibility to get user
> input, not in the BEGIN part for the command arguments?

Well, you can get user-input from standard input as well, but
then you can't also read the filenames from that channel (as
you seem to do in your code).

Standard input can always be redirected (to pipe, files, etc.).
With /dev/tty you are directly accessing the device; thus this
appears to be the preferred way to get interactive user input.

As I understand your post, it works with that approach. But
your question seems to say that you're uncomfortable with it?

You could of course also use other channels, e.g. passing the
file names as arguments, as in

$ awk 'BEGIN { for (i = 1; i <= ARGC; i++) {
printf "%s> ", ARGV[i]
getline cmd < "-"
printf "Do %s on %s\n", cmd, ARGV[i]
}
}' aaa bbb

aaa> mv
Do mv on aaa
bbb> cp
Do cp on bbb

Janis

Re: Copying files with generated filenames in a directory from inside teh AWKprogram

<5baa2bca-97b0-48aa-93fa-590e093680c8n@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=914&group=comp.lang.awk#914

  copy link   Newsgroups: comp.lang.awk
X-Received: by 2002:a37:ea0c:: with SMTP id t12mr34247946qkj.509.1632310639991;
Wed, 22 Sep 2021 04:37:19 -0700 (PDT)
X-Received: by 2002:a25:b7d0:: with SMTP id u16mr43349277ybj.342.1632310639777;
Wed, 22 Sep 2021 04:37:19 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.awk
Date: Wed, 22 Sep 2021 04:37:19 -0700 (PDT)
In-Reply-To: <sif36u$vqt$1@gioia.aioe.org>
Injection-Info: google-groups.googlegroups.com; posting-host=31.165.67.106; posting-account=BtTavwoAAACMi3AFrA3U-JBERbxGUGhD
NNTP-Posting-Host: 31.165.67.106
References: <7faa87f4-881f-4b0b-8e79-d25a638bfe75n@googlegroups.com>
<sic49t$1pok$1@gioia.aioe.org> <60f87554-ede5-4c63-b57a-2b81e4b3a19dn@googlegroups.com>
<sicl7p$4bd$1@gioia.aioe.org> <sicv8d$1c0r$1@gioia.aioe.org>
<544ddaaa-075d-4f29-9784-778491eacf9dn@googlegroups.com> <sif36u$vqt$1@gioia.aioe.org>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <5baa2bca-97b0-48aa-93fa-590e093680c8n@googlegroups.com>
Subject: Re: Copying files with generated filenames in a directory from inside
teh AWKprogram
From: mohsen.owzar@gmail.com (Mohsen Owzar)
Injection-Date: Wed, 22 Sep 2021 11:37:19 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 48
 by: Mohsen Owzar - Wed, 22 Sep 2021 11:37 UTC

Janis Papanagnou schrieb am Mittwoch, 22. September 2021 um 13:14:39 UTC+2:
> On 22.09.21 12:29, Mohsen Owzar wrote:
> > After inserting "/dev/tty" instead of "-", it waits for the user input
> > and it copies when I type "c" and Enter.
> >
> > In other words, it means that I can have user inputs from within AWK
> > only with:
> >
> > getline answer < "/dev/tty"
> >
> > Other than this method, there is no other possibility to get user
> > input, not in the BEGIN part for the command arguments?
> Well, you can get user-input from standard input as well, but
> then you can't also read the filenames from that channel (as
> you seem to do in your code).
>
> Standard input can always be redirected (to pipe, files, etc.).
> With /dev/tty you are directly accessing the device; thus this
> appears to be the preferred way to get interactive user input.
>
> As I understand your post, it works with that approach. But
> your question seems to say that you're uncomfortable with it?
>
> You could of course also use other channels, e.g. passing the
> file names as arguments, as in
>
> $ awk 'BEGIN { for (i = 1; i <= ARGC; i++) {
> printf "%s> ", ARGV[i]
> getline cmd < "-"
> printf "Do %s on %s\n", cmd, ARGV[i]
> }
> }' aaa bbb
>
> aaa> mv
> Do mv on aaa
> bbb> cp
> Do cp on bbb
>
>
> Janis

Oh man,
Yes, a bit uncomfortable, therefore your new suggestion sounds very good.
I've learned again something new.
I'll try to go deeper in this approach.

Best regards
Mohsen

Re: Copying files with generated filenames in a directory from inside teh AWKprogram

<80609923-7988-911f-3831-cc517fb93d5e@scorecrow.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=915&group=comp.lang.awk#915

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: 07.013@scorecrow.com (Bruce Horrocks)
Newsgroups: comp.lang.awk
Subject: Re: Copying files with generated filenames in a directory from inside
teh AWKprogram
Date: Wed, 22 Sep 2021 23:02:50 +0100
Lines: 19
Message-ID: <80609923-7988-911f-3831-cc517fb93d5e@scorecrow.com>
References: <7faa87f4-881f-4b0b-8e79-d25a638bfe75n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: individual.net NjWyC0oQPNyi8htOD2ivYAiWj4oioxlA113Jvs+nRLCJUuIU6p
Cancel-Lock: sha1:LTXh2H1sunK2muWVMhoF1jhC2TY=
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0)
Gecko/20100101 Thunderbird/78.14.0
In-Reply-To: <7faa87f4-881f-4b0b-8e79-d25a638bfe75n@googlegroups.com>
Content-Language: en-GB
 by: Bruce Horrocks - Wed, 22 Sep 2021 22:02 UTC

On 21/09/2021 04:33, Mohsen Owzar wrote:
> Hi Guys
> I'm working with Cygwin under Windows 10
> I have written a program in AWK / Sh to copy or rename some selected files, filtered by a string given by the user, from 01 to the length of the selected files by the filter.
> Before you say that this task can be done with another software than AWK, why I used this, OK, I know a bit only AWK and Bash scripting, and if you know another and more effective method than this one, do not hesitate and offer your suggestion.

Something like this in bash:

N=1
for X in "*$1*.png" ; do
echo cp "$X" "$(printf '%02d_%s.png' N "$1" )"
N = $((N+1))
done

Remove the 'echo' when you've finished testing!

--
Bruce Horrocks
Surrey, England

Re: Copying files with generated filenames in a directory from inside teh AWKprogram

<sigctb$oa4$1@gioia.aioe.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=916&group=comp.lang.awk#916

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!rocksolid2!i2pn.org!aioe.org!LTi+xflewmQTbbz7H4hZOQ.user.46.165.242.75.POSTED!not-for-mail
From: janis_papanagnou@hotmail.com (Janis Papanagnou)
Newsgroups: comp.lang.awk
Subject: Re: Copying files with generated filenames in a directory from inside
teh AWKprogram
Date: Thu, 23 Sep 2021 01:06:19 +0200
Organization: Aioe.org NNTP Server
Message-ID: <sigctb$oa4$1@gioia.aioe.org>
References: <7faa87f4-881f-4b0b-8e79-d25a638bfe75n@googlegroups.com>
<80609923-7988-911f-3831-cc517fb93d5e@scorecrow.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="24900"; posting-host="LTi+xflewmQTbbz7H4hZOQ.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.13.0
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: en-US
 by: Janis Papanagnou - Wed, 22 Sep 2021 23:06 UTC

On 23.09.21 00:02, Bruce Horrocks wrote:
> On 21/09/2021 04:33, Mohsen Owzar wrote:
>> Hi Guys
>> I'm working with Cygwin under Windows 10
>> I have written a program in AWK / Sh to copy or rename some selected
>> files, filtered by a string given by the user, from 01 to the length
>> of the selected files by the filter.
>> Before you say that this task can be done with another software than
>> AWK, why I used this, OK, I know a bit only AWK and Bash scripting,
>> and if you know another and more effective method than this one, do
>> not hesitate and offer your suggestion.
>
> Something like this in bash:
>
> N=1
> for X in "*$1*.png" ; do

Isn't it *"$1"*.png

>   echo cp "$X" "$(printf '%02d_%s.png' N "$1" )"
>   N = $((N+1))

and N=$((N+1)) ?

> done
>
> Remove the 'echo' when you've finished testing!
>

Re: Copying files with generated filenames in a directory from inside teh AWKprogram

<5c9ea403-262b-f93e-532a-7fc2453afb87@scorecrow.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=917&group=comp.lang.awk#917

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: 07.013@scorecrow.com (Bruce Horrocks)
Newsgroups: comp.lang.awk
Subject: Re: Copying files with generated filenames in a directory from inside
teh AWKprogram
Date: Thu, 23 Sep 2021 02:33:01 +0100
Lines: 48
Message-ID: <5c9ea403-262b-f93e-532a-7fc2453afb87@scorecrow.com>
References: <7faa87f4-881f-4b0b-8e79-d25a638bfe75n@googlegroups.com>
<80609923-7988-911f-3831-cc517fb93d5e@scorecrow.com>
<sigctb$oa4$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: individual.net fhg1Kjtjzi5yS55P/qFbzgUFmgAEMEYRG/ODk5msHUYYD3FM5I
Cancel-Lock: sha1:algg+Oi5cPKi6C5I5aRsBQr4V+s=
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0)
Gecko/20100101 Thunderbird/78.14.0
In-Reply-To: <sigctb$oa4$1@gioia.aioe.org>
Content-Language: en-GB
 by: Bruce Horrocks - Thu, 23 Sep 2021 01:33 UTC

On 23/09/2021 00:06, Janis Papanagnou wrote:
> On 23.09.21 00:02, Bruce Horrocks wrote:
>> On 21/09/2021 04:33, Mohsen Owzar wrote:
>>> Hi Guys
>>> I'm working with Cygwin under Windows 10
>>> I have written a program in AWK / Sh to copy or rename some selected
>>> files, filtered by a string given by the user, from 01 to the length
>>> of the selected files by the filter.
>>> Before you say that this task can be done with another software than
>>> AWK, why I used this, OK, I know a bit only AWK and Bash scripting,
>>> and if you know another and more effective method than this one, do
>>> not hesitate and offer your suggestion.
>>
>> Something like this in bash:
>>
>> N=1
>> for X in "*$1*.png" ; do
>
> Isn't it *"$1"*.png

Don't know for sure - I think they're both the same or the same under
most conditions, at least.

>
>>   echo cp "$X" "$(printf '%02d_%s.png' N "$1" )"
>>   N = $((N+1))
>
> and N=$((N+1)) ?

Yes, no spaces is correct - I so rarely code in bash I forget and want
to pretty-print it!

My quoting on
echo cp "$X" "$(printf '%02d_%s.png' N "$1" )"
is wrong as well: the double quotes around $1 need escaping. (Which of
course I noticed about 2 seconds after pressing the send button.) :-(

>
>> done
>>
>> Remove the 'echo' when you've finished testing!
>>
>

--
Bruce Horrocks
Surrey, England

Re: Copying files with generated filenames in a directory from inside teh AWKprogram

<sigmq3$1it4$1@gioia.aioe.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=918&group=comp.lang.awk#918

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!i2pn.org!aioe.org!LTi+xflewmQTbbz7H4hZOQ.user.46.165.242.75.POSTED!not-for-mail
From: janis_papanagnou@hotmail.com (Janis Papanagnou)
Newsgroups: comp.lang.awk
Subject: Re: Copying files with generated filenames in a directory from inside
teh AWKprogram
Date: Thu, 23 Sep 2021 03:55:15 +0200
Organization: Aioe.org NNTP Server
Message-ID: <sigmq3$1it4$1@gioia.aioe.org>
References: <7faa87f4-881f-4b0b-8e79-d25a638bfe75n@googlegroups.com>
<80609923-7988-911f-3831-cc517fb93d5e@scorecrow.com>
<sigctb$oa4$1@gioia.aioe.org>
<5c9ea403-262b-f93e-532a-7fc2453afb87@scorecrow.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="52132"; posting-host="LTi+xflewmQTbbz7H4hZOQ.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.13.0
Content-Language: en-US
X-Notice: Filtered by postfilter v. 0.9.2
 by: Janis Papanagnou - Thu, 23 Sep 2021 01:55 UTC

On 23.09.21 03:33, Bruce Horrocks wrote:
> On 23/09/2021 00:06, Janis Papanagnou wrote:
>>>
>>> Something like this in bash:
>>>
>>> N=1
>>> for X in "*$1*.png" ; do
>>
>> Isn't it  *"$1"*.png
>
> Don't know for sure - I think they're both the same or the same under
> most conditions, at least.

I don't think so. The "*" are not expanded while unquoted * is.
You want them file-globbing expanded in this (and most) case(s).

Janis

Re: Copying files with generated filenames in a directory from inside teh AWKprogram

<sih6dj$2ep7h$1@news.xmission.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=919&group=comp.lang.awk#919

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gazelle@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.lang.awk
Subject: Re: Copying files with generated filenames in a directory from inside
teh AWKprogram
Date: Thu, 23 Sep 2021 06:21:39 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <sih6dj$2ep7h$1@news.xmission.com>
References: <7faa87f4-881f-4b0b-8e79-d25a638bfe75n@googlegroups.com> <80609923-7988-911f-3831-cc517fb93d5e@scorecrow.com> <sigctb$oa4$1@gioia.aioe.org> <5c9ea403-262b-f93e-532a-7fc2453afb87@scorecrow.com>
Injection-Date: Thu, 23 Sep 2021 06:21:39 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="2581745"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Thu, 23 Sep 2021 06:21 UTC

In article <5c9ea403-262b-f93e-532a-7fc2453afb87@scorecrow.com>,
Bruce Horrocks <07.013@scorecrow.com> wrote:
....
>> Isn't it *"$1"*.png
>
>Don't know for sure - I think they're both the same or the same under
>most conditions, at least.

I just did a little testing, to confirm my original thoughts.

Generally, no, it's not the same. Double quoting inhibits filename
expandsion, but it works as expected in a "for i in ..." loop, because it
gets scanned twice in that one, specific case.

>>
>>> echo cp "$X" "$(printf '%02d_%s.png' N "$1" )"
>>> N = $((N+1))
>>
>> and N=$((N+1)) ?
>
>Yes, no spaces is correct - I so rarely code in bash I forget and want
>to pretty-print it!

Or you could do:

: $((N++))

and there's probably other ways, as well.

Or just: echo cp "$X" "$(printf '%02d_%s.png' $((N++)) "$1" )"

--
Politics is show business for ugly people.

Sports is politics for stupid people.

Re: Copying files with generated filenames in a directory from inside teh AWKprogram

<sihf7n$1n24$1@gioia.aioe.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=920&group=comp.lang.awk#920

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!i2pn.org!aioe.org!LTi+xflewmQTbbz7H4hZOQ.user.46.165.242.75.POSTED!not-for-mail
From: janis_papanagnou@hotmail.com (Janis Papanagnou)
Newsgroups: comp.lang.awk
Subject: Re: Copying files with generated filenames in a directory from inside
teh AWKprogram
Date: Thu, 23 Sep 2021 10:52:07 +0200
Organization: Aioe.org NNTP Server
Message-ID: <sihf7n$1n24$1@gioia.aioe.org>
References: <7faa87f4-881f-4b0b-8e79-d25a638bfe75n@googlegroups.com>
<80609923-7988-911f-3831-cc517fb93d5e@scorecrow.com>
<sigctb$oa4$1@gioia.aioe.org>
<5c9ea403-262b-f93e-532a-7fc2453afb87@scorecrow.com>
<sih6dj$2ep7h$1@news.xmission.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="56388"; posting-host="LTi+xflewmQTbbz7H4hZOQ.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.13.0
Content-Language: en-US
X-Notice: Filtered by postfilter v. 0.9.2
 by: Janis Papanagnou - Thu, 23 Sep 2021 08:52 UTC

On 23.09.21 08:21, Kenny McCormack wrote:
> In article <5c9ea403-262b-f93e-532a-7fc2453afb87@scorecrow.com>,
> Bruce Horrocks <07.013@scorecrow.com> wrote:
> ...
>>> Isn't it *"$1"*.png
>>
>> Don't know for sure - I think they're both the same or the same under
>> most conditions, at least.
>
> I just did a little testing, to confirm my original thoughts.
>
> Generally, no, it's not the same. Double quoting inhibits filename
> expandsion, but it works as expected in a "for i in ..." loop, because it
> gets scanned twice in that one, specific case.

No, that's not true. A quoted expression with * in it will keep the *
and the filename in the iteration variable X will have it. If you use
(as in the code shown) that variable again in the correct _quoted_
form "$X" then it still persists and a file and literal * will be
looked for to do the operation.

It would (unreliably) work if in the command $X would be unquoted
cp $X ...
where the * would get expanded, but that wouldn't work with filenames
with spaces. So you need to keep the * unquoted at the right place as
shown above.

>
>>>
>>>> echo cp "$X" "$(printf '%02d_%s.png' N "$1" )"
>>>> N = $((N+1))
>>>
>>> and N=$((N+1)) ?
>>
>> Yes, no spaces is correct - I so rarely code in bash I forget and want
>> to pretty-print it!
>
> Or you could do:
>
> : $((N++))

Modern shells (ksh, zsh, and now also bash as I tested) support also
the "arithmetic command" (no ':' and no expansion '$' necessary).

((N++))

Janis

>
> and there's probably other ways, as well.
>
> Or just: echo cp "$X" "$(printf '%02d_%s.png' $((N++)) "$1" )"
>

Re: Copying files with generated filenames in a directory from inside teh AWKprogram

<sihsg0$2sg$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=921&group=comp.lang.awk#921

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: mortonspam@gmail.com (Ed Morton)
Newsgroups: comp.lang.awk
Subject: Re: Copying files with generated filenames in a directory from inside
teh AWKprogram
Date: Thu, 23 Sep 2021 07:38:23 -0500
Organization: A noiseless patient Spider
Lines: 69
Message-ID: <sihsg0$2sg$1@dont-email.me>
References: <7faa87f4-881f-4b0b-8e79-d25a638bfe75n@googlegroups.com>
<80609923-7988-911f-3831-cc517fb93d5e@scorecrow.com>
<sigctb$oa4$1@gioia.aioe.org>
<5c9ea403-262b-f93e-532a-7fc2453afb87@scorecrow.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 23 Sep 2021 12:38:24 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="c207d5737beaa3d6853d1ad8c97b1a49";
logging-data="2960"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19FuEyCjwOtzdaS96PZQ1o0"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.14.0
Cancel-Lock: sha1:AI2I8jzxQAFnI0A4dnH/sdvp1c8=
In-Reply-To: <5c9ea403-262b-f93e-532a-7fc2453afb87@scorecrow.com>
X-Antivirus-Status: Clean
Content-Language: en-US
X-Antivirus: Avast (VPS 210923-0, 9/22/2021), Outbound message
 by: Ed Morton - Thu, 23 Sep 2021 12:38 UTC

On 9/22/2021 8:33 PM, Bruce Horrocks wrote:
> On 23/09/2021 00:06, Janis Papanagnou wrote:
>> On 23.09.21 00:02, Bruce Horrocks wrote:
>>> On 21/09/2021 04:33, Mohsen Owzar wrote:
>>>> Hi Guys
>>>> I'm working with Cygwin under Windows 10
>>>> I have written a program in AWK / Sh to copy or rename some selected
>>>> files, filtered by a string given by the user, from 01 to the length
>>>> of the selected files by the filter.
>>>> Before you say that this task can be done with another software than
>>>> AWK, why I used this, OK, I know a bit only AWK and Bash scripting,
>>>> and if you know another and more effective method than this one, do
>>>> not hesitate and offer your suggestion.
>>>
>>> Something like this in bash:
>>>
>>> N=1
>>> for X in "*$1*.png" ; do
>>
>> Isn't it  *"$1"*.png
>
> Don't know for sure - I think they're both the same or the same under
> most conditions, at least.

No, Janis is correct. The first form will set X to the one literal
string "*$1*.png" while the second will populate it iteratively with the
name of every file in your directory whose name contains whatever value
$1 is and ends in .png.

>
>>
>>>    echo cp "$X" "$(printf '%02d_%s.png' N "$1" )"
>>>    N = $((N+1))
>>
>> and  N=$((N+1))  ?
>
> Yes, no spaces is correct - I so rarely code in bash I forget and want
> to pretty-print it!
>
> My quoting on
>   echo cp "$X" "$(printf '%02d_%s.png' N "$1" )"
> is wrong as well: the double quotes around $1 need escaping.

No, you can (and should in this case) nest double quotes within command
substitution using $(...). If your intent was to get double quotes in
the output of the printf then you'd write it as

printf '%02d_"%s".png' N "$1"

not:

printf '%02d_%s.png' N \"$1\"

Regards,

Ed.

(Which of
> course I noticed about 2 seconds after pressing the send button.) :-(
>
>>
>>> done
>>>
>>> Remove the 'echo' when you've finished testing!
>>>
>>
>
>

Re: Copying files with generated filenames in a directory from inside teh AWKprogram

<311b67ae-ecef-06e6-fefa-205d6a99532a@scorecrow.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=922&group=comp.lang.awk#922

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: 07.013@scorecrow.com (Bruce Horrocks)
Newsgroups: comp.lang.awk
Subject: Re: Copying files with generated filenames in a directory from inside
teh AWKprogram
Date: Thu, 23 Sep 2021 19:50:21 +0100
Lines: 65
Message-ID: <311b67ae-ecef-06e6-fefa-205d6a99532a@scorecrow.com>
References: <7faa87f4-881f-4b0b-8e79-d25a638bfe75n@googlegroups.com>
<80609923-7988-911f-3831-cc517fb93d5e@scorecrow.com>
<sigctb$oa4$1@gioia.aioe.org>
<5c9ea403-262b-f93e-532a-7fc2453afb87@scorecrow.com>
<sihsg0$2sg$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: individual.net eEd1htIWCLqRaB6bQfEKKAnQ7o98KvuPCLIItsdz2YaI5HA5V9
Cancel-Lock: sha1:TnnFX5Go3Zwa4fWPur+T+A7nWLU=
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0)
Gecko/20100101 Thunderbird/78.14.0
In-Reply-To: <sihsg0$2sg$1@dont-email.me>
Content-Language: en-GB
 by: Bruce Horrocks - Thu, 23 Sep 2021 18:50 UTC

On 23/09/2021 13:38, Ed Morton wrote:
> On 9/22/2021 8:33 PM, Bruce Horrocks wrote:
>> On 23/09/2021 00:06, Janis Papanagnou wrote:
>>> On 23.09.21 00:02, Bruce Horrocks wrote:
>>>> On 21/09/2021 04:33, Mohsen Owzar wrote:
>>>>> Hi Guys
>>>>> I'm working with Cygwin under Windows 10
>>>>> I have written a program in AWK / Sh to copy or rename some selected
>>>>> files, filtered by a string given by the user, from 01 to the length
>>>>> of the selected files by the filter.
>>>>> Before you say that this task can be done with another software than
>>>>> AWK, why I used this, OK, I know a bit only AWK and Bash scripting,
>>>>> and if you know another and more effective method than this one, do
>>>>> not hesitate and offer your suggestion.
>>>>
>>>> Something like this in bash:
>>>>
>>>> N=1
>>>> for X in "*$1*.png" ; do
>>>
>>> Isn't it  *"$1"*.png
>>
>> Don't know for sure - I think they're both the same or the same under
>> most conditions, at least.
>
> No, Janis is correct. The first form will set X to the one literal
> string "*$1*.png" while the second will populate it iteratively with the
> name of every file in your directory whose name contains whatever value
> $1 is and ends in .png.

Hm. The perils of testing on the command line instead of in a script.
You're right but it does look a bit counter-intuitive.
[snip]

>> My quoting on
>>    echo cp "$X" "$(printf '%02d_%s.png' N "$1" )"
>> is wrong as well: the double quotes around $1 need escaping.
>
> No, you can (and should in this case) nest double quotes within command
> substitution using $(...). If your intent was to get double quotes in
> the output of the printf then you'd write it as
>
>     printf '%02d_"%s".png' N "$1"
>
> not:
>
>     printf '%02d_%s.png' N \"$1\"

I meant the \"$1\" ... but quotes in the output of the $() turned out to
be simpler than I thought because "$()" does the right thing.

So I think taking it all together we get:

#! /bin/bash
N=1
for X in *"$1"* ; do
echo cp "$X" "$(printf '%02d_%s.png' "$N" "$1")"
N=$((N+1))
done

which works for a limited test set on my desktop!

--
Bruce Horrocks
Surrey, England

Re: Copying files with generated filenames in a directory from inside teh AWKprogram

<ac56111a-3657-4e28-8dc2-0daf7a9865fdn@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=924&group=comp.lang.awk#924

  copy link   Newsgroups: comp.lang.awk
X-Received: by 2002:a05:620a:d8e:: with SMTP id q14mr10171953qkl.409.1632484255859;
Fri, 24 Sep 2021 04:50:55 -0700 (PDT)
X-Received: by 2002:a25:b7d0:: with SMTP id u16mr11460218ybj.342.1632484255668;
Fri, 24 Sep 2021 04:50:55 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.awk
Date: Fri, 24 Sep 2021 04:50:55 -0700 (PDT)
In-Reply-To: <311b67ae-ecef-06e6-fefa-205d6a99532a@scorecrow.com>
Injection-Info: google-groups.googlegroups.com; posting-host=31.165.67.106; posting-account=BtTavwoAAACMi3AFrA3U-JBERbxGUGhD
NNTP-Posting-Host: 31.165.67.106
References: <7faa87f4-881f-4b0b-8e79-d25a638bfe75n@googlegroups.com>
<80609923-7988-911f-3831-cc517fb93d5e@scorecrow.com> <sigctb$oa4$1@gioia.aioe.org>
<5c9ea403-262b-f93e-532a-7fc2453afb87@scorecrow.com> <sihsg0$2sg$1@dont-email.me>
<311b67ae-ecef-06e6-fefa-205d6a99532a@scorecrow.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <ac56111a-3657-4e28-8dc2-0daf7a9865fdn@googlegroups.com>
Subject: Re: Copying files with generated filenames in a directory from inside
teh AWKprogram
From: mohsen.owzar@gmail.com (Mohsen Owzar)
Injection-Date: Fri, 24 Sep 2021 11:50:55 +0000
Content-Type: text/plain; charset="UTF-8"
 by: Mohsen Owzar - Fri, 24 Sep 2021 11:50 UTC

Bruce Horrocks schrieb am Donnerstag, 23. September 2021 um 20:50:23 UTC+2:
> On 23/09/2021 13:38, Ed Morton wrote:
> > On 9/22/2021 8:33 PM, Bruce Horrocks wrote:
> >> On 23/09/2021 00:06, Janis Papanagnou wrote:
> >>> On 23.09.21 00:02, Bruce Horrocks wrote:
> >>>> On 21/09/2021 04:33, Mohsen Owzar wrote:
> >>>>> Hi Guys
> >>>>> I'm working with Cygwin under Windows 10
> >>>>> I have written a program in AWK / Sh to copy or rename some selected
> >>>>> files, filtered by a string given by the user, from 01 to the length
> >>>>> of the selected files by the filter.
> >>>>> Before you say that this task can be done with another software than
> >>>>> AWK, why I used this, OK, I know a bit only AWK and Bash scripting,
> >>>>> and if you know another and more effective method than this one, do
> >>>>> not hesitate and offer your suggestion.
> >>>>
> >>>> Something like this in bash:
> >>>>
> >>>> N=1
> >>>> for X in "*$1*.png" ; do
> >>>
> >>> Isn't it *"$1"*.png
> >>
> >> Don't know for sure - I think they're both the same or the same under
> >> most conditions, at least.
> >
> > No, Janis is correct. The first form will set X to the one literal
> > string "*$1*.png" while the second will populate it iteratively with the
> > name of every file in your directory whose name contains whatever value
> > $1 is and ends in .png.
> Hm. The perils of testing on the command line instead of in a script.
> You're right but it does look a bit counter-intuitive.
> [snip]
> >> My quoting on
> >> echo cp "$X" "$(printf '%02d_%s.png' N "$1" )"
> >> is wrong as well: the double quotes around $1 need escaping.
> >
> > No, you can (and should in this case) nest double quotes within command
> > substitution using $(...). If your intent was to get double quotes in
> > the output of the printf then you'd write it as
> >
> > printf '%02d_"%s".png' N "$1"
> >
> > not:
> >
> > printf '%02d_%s.png' N \"$1\"
> I meant the \"$1\" ... but quotes in the output of the $() turned out to
> be simpler than I thought because "$()" does the right thing.
>
> So I think taking it all together we get:
>
> #! /bin/bash
> N=1
> for X in *"$1"* ; do
> echo cp "$X" "$(printf '%02d_%s.png' "$N" "$1")"
> N=$((N+1))
> done
>
> which works for a limited test set on my desktop!
> --
> Bruce Horrocks
> Surrey, England

I'm somehow confused.

As I understood, I can replace my whole code
including Shell, AWK, Shell
with only this following Shell lines:

:::::::::::::::::::::::::::::::::::::::::::::::::
#! /bin/bash
# Filename: Rename_Test.sh
N=1
for X in *"$1"*
do
echo cp "$X" "$(printf '%02d_%s.png' "$N" "$1")"
N=$((N+1))
done
:::::::::::::::::::::::::::::::::::::::::::::::::

When I run this script, I get the following error message:
:::::::::::::::::::::::::::::::::::::::::::::::::
$> ll
/cygdrive/c/Users/m.owzar/Desktop/GUI_View Screenshots/00_09
insgesamt 208
drwx------+ 1 m.owzar Domain Users 0 24. Sep 13:16 .
drwx------+ 1 m.owzar Domain Users 0 24. Sep 09:47 ..
-rwx------+ 1 m.owzar Domain Users 25272 21. Sep 03:47 '2021-09-21 03_47_09-no title.png'
-rwx------+ 1 m.owzar Domain Users 24687 21. Sep 03:47 '2021-09-21 03_47_18-no title.png'
-rwx------+ 1 m.owzar Domain Users 24774 21. Sep 03:47 '2021-09-21 03_47_23-no title.png'
-rwx------+ 1 m.owzar Domain Users 27528 21. Sep 03:47 '2021-09-21 03_47_41-no title.png'
-rwx------+ 1 m.owzar Domain Users 27937 21. Sep 03:47 '2021-09-21 03_47_45-no title.png'
-rwx------+ 1 m.owzar Domain Users 27559 21. Sep 03:47 '2021-09-21 03_47_50-no title.png'
-rwx------+ 1 m.owzar Domain Users 26617 21. Sep 03:48 '2021-09-21 03_48_01-no title.png'

m.owzar@ /cygdrive/c/Users/m.owzar/Desktop/GUI_View Screenshots/00_09
$> Rename_Test.sh "no title"
/home/m.owzar/BinMo/Rename_Test.sh: Zeile 5: Syntaxfehler beim unerwarteten Wort `$'do\r''
'home/m.owzar/BinMo/Rename_Test.sh: Zeile 5: `do
:::::::::::::::::::::::::::::::::::::::::::::::::

I use under Windows 10 the Cygwin, and in there,
my script at the end of each line has CR|LF.

I think for the given string as argument, it is valid also.
Due to this issue in my AWK part of the code,
I had to subtract one character from the given argument.

Does anyone know what this error message mean? (translated in English)

Line 5: Syntax error at unexpected Word `$'do\r''

Regards
Mohsen

Re: Copying files with generated filenames in a directory from inside teh AWKprogram

<sikfal$1pql$1@gioia.aioe.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=925&group=comp.lang.awk#925

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!i2pn.org!aioe.org!LTi+xflewmQTbbz7H4hZOQ.user.46.165.242.75.POSTED!not-for-mail
From: janis_papanagnou@hotmail.com (Janis Papanagnou)
Newsgroups: comp.lang.awk
Subject: Re: Copying files with generated filenames in a directory from inside
teh AWKprogram
Date: Fri, 24 Sep 2021 14:12:05 +0200
Organization: Aioe.org NNTP Server
Message-ID: <sikfal$1pql$1@gioia.aioe.org>
References: <7faa87f4-881f-4b0b-8e79-d25a638bfe75n@googlegroups.com>
<80609923-7988-911f-3831-cc517fb93d5e@scorecrow.com>
<sigctb$oa4$1@gioia.aioe.org>
<5c9ea403-262b-f93e-532a-7fc2453afb87@scorecrow.com>
<sihsg0$2sg$1@dont-email.me>
<311b67ae-ecef-06e6-fefa-205d6a99532a@scorecrow.com>
<ac56111a-3657-4e28-8dc2-0daf7a9865fdn@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="59221"; posting-host="LTi+xflewmQTbbz7H4hZOQ.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.13.0
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: en-US
 by: Janis Papanagnou - Fri, 24 Sep 2021 12:12 UTC

On 24.09.21 13:50, Mohsen Owzar wrote:
> Bruce Horrocks schrieb am Donnerstag, 23. September 2021 um 20:50:23 UTC+2:
>> On 23/09/2021 13:38, Ed Morton wrote:
>>> On 9/22/2021 8:33 PM, Bruce Horrocks wrote:
>>>> On 23/09/2021 00:06, Janis Papanagnou wrote:
>>>>> On 23.09.21 00:02, Bruce Horrocks wrote:
>>>>>> On 21/09/2021 04:33, Mohsen Owzar wrote:
>>>>>>> Hi Guys
>>>>>>> I'm working with Cygwin under Windows 10
>>>>>>> I have written a program in AWK / Sh to copy or rename some selected
>>>>>>> files, filtered by a string given by the user, from 01 to the length
>>>>>>> of the selected files by the filter.
>>>>>>> Before you say that this task can be done with another software than
>>>>>>> AWK, why I used this, OK, I know a bit only AWK and Bash scripting,
>>>>>>> and if you know another and more effective method than this one, do
>>>>>>> not hesitate and offer your suggestion.
>>>>>>
>>>>>> Something like this in bash:
>>>>>>
>>>>>> N=1
>>>>>> for X in "*$1*.png" ; do
>>>>>
>>>>> Isn't it *"$1"*.png
>>>>
>>>> Don't know for sure - I think they're both the same or the same under
>>>> most conditions, at least.
>>>
>>> No, Janis is correct. The first form will set X to the one literal
>>> string "*$1*.png" while the second will populate it iteratively with the
>>> name of every file in your directory whose name contains whatever value
>>> $1 is and ends in .png.
>> Hm. The perils of testing on the command line instead of in a script.
>> You're right but it does look a bit counter-intuitive.
>> [snip]
>>>> My quoting on
>>>> echo cp "$X" "$(printf '%02d_%s.png' N "$1" )"
>>>> is wrong as well: the double quotes around $1 need escaping.
>>>
>>> No, you can (and should in this case) nest double quotes within command
>>> substitution using $(...). If your intent was to get double quotes in
>>> the output of the printf then you'd write it as
>>>
>>> printf '%02d_"%s".png' N "$1"
>>>
>>> not:
>>>
>>> printf '%02d_%s.png' N \"$1\"
>> I meant the \"$1\" ... but quotes in the output of the $() turned out to
>> be simpler than I thought because "$()" does the right thing.
>>
>> So I think taking it all together we get:
>>
>> #! /bin/bash
>> N=1
>> for X in *"$1"* ; do
>> echo cp "$X" "$(printf '%02d_%s.png' "$N" "$1")"
>> N=$((N+1))
>> done
>>
>> which works for a limited test set on my desktop!
>> --
>> Bruce Horrocks
>> Surrey, England
>
> I'm somehow confused.
>
> As I understood, I can replace my whole code
> including Shell, AWK, Shell
> with only this following Shell lines:
>
> :::::::::::::::::::::::::::::::::::::::::::::::::
> #! /bin/bash
> # Filename: Rename_Test.sh
> N=1
> for X in *"$1"*
> do
> echo cp "$X" "$(printf '%02d_%s.png' "$N" "$1")"
> N=$((N+1))
> done
> :::::::::::::::::::::::::::::::::::::::::::::::::
>
> When I run this script, I get the following error message:
> :::::::::::::::::::::::::::::::::::::::::::::::::
> $> ll
> /cygdrive/c/Users/m.owzar/Desktop/GUI_View Screenshots/00_09
> insgesamt 208
> drwx------+ 1 m.owzar Domain Users 0 24. Sep 13:16 .
> drwx------+ 1 m.owzar Domain Users 0 24. Sep 09:47 ..
> -rwx------+ 1 m.owzar Domain Users 25272 21. Sep 03:47 '2021-09-21 03_47_09-no title.png'
> -rwx------+ 1 m.owzar Domain Users 24687 21. Sep 03:47 '2021-09-21 03_47_18-no title.png'
> -rwx------+ 1 m.owzar Domain Users 24774 21. Sep 03:47 '2021-09-21 03_47_23-no title.png'
> -rwx------+ 1 m.owzar Domain Users 27528 21. Sep 03:47 '2021-09-21 03_47_41-no title.png'
> -rwx------+ 1 m.owzar Domain Users 27937 21. Sep 03:47 '2021-09-21 03_47_45-no title.png'
> -rwx------+ 1 m.owzar Domain Users 27559 21. Sep 03:47 '2021-09-21 03_47_50-no title.png'
> -rwx------+ 1 m.owzar Domain Users 26617 21. Sep 03:48 '2021-09-21 03_48_01-no title.png'
>
> m.owzar@ /cygdrive/c/Users/m.owzar/Desktop/GUI_View Screenshots/00_09
> $> Rename_Test.sh "no title"
> /home/m.owzar/BinMo/Rename_Test.sh: Zeile 5: Syntaxfehler beim unerwarteten Wort `$'do\r''
> 'home/m.owzar/BinMo/Rename_Test.sh: Zeile 5: `do
> :::::::::::::::::::::::::::::::::::::::::::::::::
>
> I use under Windows 10 the Cygwin, and in there,
> my script at the end of each line has CR|LF.
>
> I think for the given string as argument, it is valid also.
> Due to this issue in my AWK part of the code,
> I had to subtract one character from the given argument.
>
> Does anyone know what this error message mean? (translated in English)
>
> Line 5: Syntax error at unexpected Word `$'do\r''

It means that there's a CR that shouldn't be there. Actually saying
what you mentioned above, that it's a DOS file. But that is illegal.
If you cleanup your script (remove the CRs and keep the LFs only)
that error should vanish.

Janis

>
> Regards
> Mohsen
>

Re: Copying files with generated filenames in a directory from inside teh AWKprogram

<d2dfb313-e993-b4a9-9973-0c37acc4586c@scorecrow.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=926&group=comp.lang.awk#926

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: 07.013@scorecrow.com (Bruce Horrocks)
Newsgroups: comp.lang.awk
Subject: Re: Copying files with generated filenames in a directory from inside
teh AWKprogram
Date: Fri, 24 Sep 2021 23:01:37 +0100
Lines: 144
Message-ID: <d2dfb313-e993-b4a9-9973-0c37acc4586c@scorecrow.com>
References: <7faa87f4-881f-4b0b-8e79-d25a638bfe75n@googlegroups.com>
<80609923-7988-911f-3831-cc517fb93d5e@scorecrow.com>
<sigctb$oa4$1@gioia.aioe.org>
<5c9ea403-262b-f93e-532a-7fc2453afb87@scorecrow.com>
<sihsg0$2sg$1@dont-email.me>
<311b67ae-ecef-06e6-fefa-205d6a99532a@scorecrow.com>
<ac56111a-3657-4e28-8dc2-0daf7a9865fdn@googlegroups.com>
<sikfal$1pql$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: individual.net PDbqlGdXNXYZdfyHfsFCAASTr3sFwMk3YbBJ5i9KgxgQR5j4JQ
Cancel-Lock: sha1:YwMZWWPzVUldm74nTzj2uCq+SpM=
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0)
Gecko/20100101 Thunderbird/78.14.0
In-Reply-To: <sikfal$1pql$1@gioia.aioe.org>
Content-Language: en-GB
 by: Bruce Horrocks - Fri, 24 Sep 2021 22:01 UTC

On 24/09/2021 13:12, Janis Papanagnou wrote:
> On 24.09.21 13:50, Mohsen Owzar wrote:
>> Bruce Horrocks schrieb am Donnerstag, 23. September 2021 um 20:50:23 UTC+2:
>>> On 23/09/2021 13:38, Ed Morton wrote:
>>>> On 9/22/2021 8:33 PM, Bruce Horrocks wrote:
>>>>> On 23/09/2021 00:06, Janis Papanagnou wrote:
>>>>>> On 23.09.21 00:02, Bruce Horrocks wrote:
>>>>>>> On 21/09/2021 04:33, Mohsen Owzar wrote:
>>>>>>>> Hi Guys
>>>>>>>> I'm working with Cygwin under Windows 10
>>>>>>>> I have written a program in AWK / Sh to copy or rename some selected
>>>>>>>> files, filtered by a string given by the user, from 01 to the length
>>>>>>>> of the selected files by the filter.
>>>>>>>> Before you say that this task can be done with another software than
>>>>>>>> AWK, why I used this, OK, I know a bit only AWK and Bash scripting,
>>>>>>>> and if you know another and more effective method than this one, do
>>>>>>>> not hesitate and offer your suggestion.
>>>>>>>
>>>>>>> Something like this in bash:
>>>>>>>
>>>>>>> N=1
>>>>>>> for X in "*$1*.png" ; do
>>>>>>
>>>>>> Isn't it *"$1"*.png
>>>>>
>>>>> Don't know for sure - I think they're both the same or the same under
>>>>> most conditions, at least.
>>>>
>>>> No, Janis is correct. The first form will set X to the one literal
>>>> string "*$1*.png" while the second will populate it iteratively with the
>>>> name of every file in your directory whose name contains whatever value
>>>> $1 is and ends in .png.
>>> Hm. The perils of testing on the command line instead of in a script.
>>> You're right but it does look a bit counter-intuitive.
>>> [snip]
>>>>> My quoting on
>>>>> echo cp "$X" "$(printf '%02d_%s.png' N "$1" )"
>>>>> is wrong as well: the double quotes around $1 need escaping.
>>>>
>>>> No, you can (and should in this case) nest double quotes within command
>>>> substitution using $(...). If your intent was to get double quotes in
>>>> the output of the printf then you'd write it as
>>>>
>>>> printf '%02d_"%s".png' N "$1"
>>>>
>>>> not:
>>>>
>>>> printf '%02d_%s.png' N \"$1\"
>>> I meant the \"$1\" ... but quotes in the output of the $() turned out to
>>> be simpler than I thought because "$()" does the right thing.
>>>
>>> So I think taking it all together we get:
>>>
>>> #! /bin/bash
>>> N=1
>>> for X in *"$1"* ; do
>>> echo cp "$X" "$(printf '%02d_%s.png' "$N" "$1")"
>>> N=$((N+1))
>>> done
>>>
>>> which works for a limited test set on my desktop!
>>> --
>>> Bruce Horrocks
>>> Surrey, England
>>
>> I'm somehow confused.
>>
>> As I understood, I can replace my whole code
>> including Shell, AWK, Shell
>> with only this following Shell lines:
>>
>> :::::::::::::::::::::::::::::::::::::::::::::::::
>> #! /bin/bash
>> # Filename: Rename_Test.sh
>> N=1
>> for X in *"$1"*
>> do
>> echo cp "$X" "$(printf '%02d_%s.png' "$N" "$1")"
>> N=$((N+1))
>> done
>> :::::::::::::::::::::::::::::::::::::::::::::::::
>>
>> When I run this script, I get the following error message:
>> :::::::::::::::::::::::::::::::::::::::::::::::::
>> $> ll
>> /cygdrive/c/Users/m.owzar/Desktop/GUI_View Screenshots/00_09
>> insgesamt 208
>> drwx------+ 1 m.owzar Domain Users 0 24. Sep 13:16 .
>> drwx------+ 1 m.owzar Domain Users 0 24. Sep 09:47 ..
>> -rwx------+ 1 m.owzar Domain Users 25272 21. Sep 03:47 '2021-09-21 03_47_09-no title.png'
>> -rwx------+ 1 m.owzar Domain Users 24687 21. Sep 03:47 '2021-09-21 03_47_18-no title.png'
>> -rwx------+ 1 m.owzar Domain Users 24774 21. Sep 03:47 '2021-09-21 03_47_23-no title.png'
>> -rwx------+ 1 m.owzar Domain Users 27528 21. Sep 03:47 '2021-09-21 03_47_41-no title.png'
>> -rwx------+ 1 m.owzar Domain Users 27937 21. Sep 03:47 '2021-09-21 03_47_45-no title.png'
>> -rwx------+ 1 m.owzar Domain Users 27559 21. Sep 03:47 '2021-09-21 03_47_50-no title.png'
>> -rwx------+ 1 m.owzar Domain Users 26617 21. Sep 03:48 '2021-09-21 03_48_01-no title.png'
>>
>> m.owzar@ /cygdrive/c/Users/m.owzar/Desktop/GUI_View Screenshots/00_09
>> $> Rename_Test.sh "no title"
>> /home/m.owzar/BinMo/Rename_Test.sh: Zeile 5: Syntaxfehler beim unerwarteten Wort `$'do\r''
>> 'home/m.owzar/BinMo/Rename_Test.sh: Zeile 5: `do
>> :::::::::::::::::::::::::::::::::::::::::::::::::
>>
>> I use under Windows 10 the Cygwin, and in there,
>> my script at the end of each line has CR|LF.
>>
>> I think for the given string as argument, it is valid also.
>> Due to this issue in my AWK part of the code,
>> I had to subtract one character from the given argument.
>>
>> Does anyone know what this error message mean? (translated in English)
>>
>> Line 5: Syntax error at unexpected Word `$'do\r''
>
> It means that there's a CR that shouldn't be there. Actually saying
> what you mentioned above, that it's a DOS file. But that is illegal.
> If you cleanup your script (remove the CRs and keep the LFs only)
> that error should vanish.

Agreed. You could use dos2unix on the script's source file to do this
cleanup as a one-off activity.

Also the script has a bug: if the parameter text doesn't match any file
then the script tries to copy a non-existent file and fails. The
solution is to test that the file exists first before trying to copy it.

:::::::::::::
#! /bin/bash
# Filename: Rename_Test.sh
N=1
for X in *"$1"*
do
if [ -f "$X" ]
then
echo cp "$X" "$(printf '%02d_%s.png' "$N" "$1")"
N=$((N+1))
fi
done
:::::::::::::

--
Bruce Horrocks
Surrey, England

Re: Copying files with generated filenames in a directory from inside teh AWKprogram

<sili46$f5p$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=927&group=comp.lang.awk#927

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: mortonspam@gmail.com (Ed Morton)
Newsgroups: comp.lang.awk
Subject: Re: Copying files with generated filenames in a directory from inside
teh AWKprogram
Date: Fri, 24 Sep 2021 17:05:57 -0500
Organization: A noiseless patient Spider
Lines: 130
Message-ID: <sili46$f5p$1@dont-email.me>
References: <7faa87f4-881f-4b0b-8e79-d25a638bfe75n@googlegroups.com>
<80609923-7988-911f-3831-cc517fb93d5e@scorecrow.com>
<sigctb$oa4$1@gioia.aioe.org>
<5c9ea403-262b-f93e-532a-7fc2453afb87@scorecrow.com>
<sihsg0$2sg$1@dont-email.me>
<311b67ae-ecef-06e6-fefa-205d6a99532a@scorecrow.com>
<ac56111a-3657-4e28-8dc2-0daf7a9865fdn@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 24 Sep 2021 22:05:58 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="638e6ba33018fdaa9b85936e5d0d68d4";
logging-data="15545"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18EHMumIbEKFGa8pO3ZmNAw"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.14.0
Cancel-Lock: sha1:dcDm2aqQo1TSmvoDKV9N4QFqDGU=
In-Reply-To: <ac56111a-3657-4e28-8dc2-0daf7a9865fdn@googlegroups.com>
X-Antivirus-Status: Clean
Content-Language: en-US
X-Antivirus: Avast (VPS 210924-4, 9/24/2021), Outbound message
 by: Ed Morton - Fri, 24 Sep 2021 22:05 UTC

On 9/24/2021 6:50 AM, Mohsen Owzar wrote:
> Bruce Horrocks schrieb am Donnerstag, 23. September 2021 um 20:50:23 UTC+2:
>> On 23/09/2021 13:38, Ed Morton wrote:
>>> On 9/22/2021 8:33 PM, Bruce Horrocks wrote:
>>>> On 23/09/2021 00:06, Janis Papanagnou wrote:
>>>>> On 23.09.21 00:02, Bruce Horrocks wrote:
>>>>>> On 21/09/2021 04:33, Mohsen Owzar wrote:
>>>>>>> Hi Guys
>>>>>>> I'm working with Cygwin under Windows 10
>>>>>>> I have written a program in AWK / Sh to copy or rename some selected
>>>>>>> files, filtered by a string given by the user, from 01 to the length
>>>>>>> of the selected files by the filter.
>>>>>>> Before you say that this task can be done with another software than
>>>>>>> AWK, why I used this, OK, I know a bit only AWK and Bash scripting,
>>>>>>> and if you know another and more effective method than this one, do
>>>>>>> not hesitate and offer your suggestion.
>>>>>>
>>>>>> Something like this in bash:
>>>>>>
>>>>>> N=1
>>>>>> for X in "*$1*.png" ; do
>>>>>
>>>>> Isn't it *"$1"*.png
>>>>
>>>> Don't know for sure - I think they're both the same or the same under
>>>> most conditions, at least.
>>>
>>> No, Janis is correct. The first form will set X to the one literal
>>> string "*$1*.png" while the second will populate it iteratively with the
>>> name of every file in your directory whose name contains whatever value
>>> $1 is and ends in .png.
>> Hm. The perils of testing on the command line instead of in a script.
>> You're right but it does look a bit counter-intuitive.
>> [snip]
>>>> My quoting on
>>>> echo cp "$X" "$(printf '%02d_%s.png' N "$1" )"
>>>> is wrong as well: the double quotes around $1 need escaping.
>>>
>>> No, you can (and should in this case) nest double quotes within command
>>> substitution using $(...). If your intent was to get double quotes in
>>> the output of the printf then you'd write it as
>>>
>>> printf '%02d_"%s".png' N "$1"
>>>
>>> not:
>>>
>>> printf '%02d_%s.png' N \"$1\"
>> I meant the \"$1\" ... but quotes in the output of the $() turned out to
>> be simpler than I thought because "$()" does the right thing.
>>
>> So I think taking it all together we get:
>>
>> #! /bin/bash
>> N=1
>> for X in *"$1"* ; do
>> echo cp "$X" "$(printf '%02d_%s.png' "$N" "$1")"
>> N=$((N+1))
>> done
>>
>> which works for a limited test set on my desktop!
>> --
>> Bruce Horrocks
>> Surrey, England
>
> I'm somehow confused.
>
> As I understood, I can replace my whole code
> including Shell, AWK, Shell
> with only this following Shell lines:

That is correct. Having shell call awk to call system() to spawn a
subshell to call cp is vastly overcomplicated and inefficient for your
needs as I mentioned in my first comment in this thread.

>
> :::::::::::::::::::::::::::::::::::::::::::::::::
> #! /bin/bash
> # Filename: Rename_Test.sh
> N=1
> for X in *"$1"*
> do
> echo cp "$X" "$(printf '%02d_%s.png' "$N" "$1")"
> N=$((N+1))
> done
> :::::::::::::::::::::::::::::::::::::::::::::::::
>
> When I run this script, I get the following error message:
> :::::::::::::::::::::::::::::::::::::::::::::::::
> $> ll
> /cygdrive/c/Users/m.owzar/Desktop/GUI_View Screenshots/00_09
> insgesamt 208
> drwx------+ 1 m.owzar Domain Users 0 24. Sep 13:16 .
> drwx------+ 1 m.owzar Domain Users 0 24. Sep 09:47 ..
> -rwx------+ 1 m.owzar Domain Users 25272 21. Sep 03:47 '2021-09-21 03_47_09-no title.png'
> -rwx------+ 1 m.owzar Domain Users 24687 21. Sep 03:47 '2021-09-21 03_47_18-no title.png'
> -rwx------+ 1 m.owzar Domain Users 24774 21. Sep 03:47 '2021-09-21 03_47_23-no title.png'
> -rwx------+ 1 m.owzar Domain Users 27528 21. Sep 03:47 '2021-09-21 03_47_41-no title.png'
> -rwx------+ 1 m.owzar Domain Users 27937 21. Sep 03:47 '2021-09-21 03_47_45-no title.png'
> -rwx------+ 1 m.owzar Domain Users 27559 21. Sep 03:47 '2021-09-21 03_47_50-no title.png'
> -rwx------+ 1 m.owzar Domain Users 26617 21. Sep 03:48 '2021-09-21 03_48_01-no title.png'
>
> m.owzar@ /cygdrive/c/Users/m.owzar/Desktop/GUI_View Screenshots/00_09
> $> Rename_Test.sh "no title"
> /home/m.owzar/BinMo/Rename_Test.sh: Zeile 5: Syntaxfehler beim unerwarteten Wort `$'do\r''

As you can see at the end of the above error message your script
contains DOS line endings (CR aka `\r`s). See
https://stackoverflow.com/q/45772525/1745001 for more info and run
`dos2unix` or similar on it to clean it up.

Ed.

> 'home/m.owzar/BinMo/Rename_Test.sh: Zeile 5: `do
> :::::::::::::::::::::::::::::::::::::::::::::::::
>
> I use under Windows 10 the Cygwin, and in there,
> my script at the end of each line has CR|LF.
>
> I think for the given string as argument, it is valid also.
> Due to this issue in my AWK part of the code,
> I had to subtract one character from the given argument.
>
> Does anyone know what this error message mean? (translated in English)
>
> Line 5: Syntax error at unexpected Word `$'do\r''
>
> Regards
> Mohsen
>

Re: Copying files with generated filenames in a directory from inside teh AWKprogram

<silim6$iqg$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=928&group=comp.lang.awk#928

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: mortonspam@gmail.com (Ed Morton)
Newsgroups: comp.lang.awk
Subject: Re: Copying files with generated filenames in a directory from inside
teh AWKprogram
Date: Fri, 24 Sep 2021 17:15:33 -0500
Organization: A noiseless patient Spider
Lines: 35
Message-ID: <silim6$iqg$1@dont-email.me>
References: <7faa87f4-881f-4b0b-8e79-d25a638bfe75n@googlegroups.com>
<80609923-7988-911f-3831-cc517fb93d5e@scorecrow.com>
<sigctb$oa4$1@gioia.aioe.org>
<5c9ea403-262b-f93e-532a-7fc2453afb87@scorecrow.com>
<sihsg0$2sg$1@dont-email.me>
<311b67ae-ecef-06e6-fefa-205d6a99532a@scorecrow.com>
<ac56111a-3657-4e28-8dc2-0daf7a9865fdn@googlegroups.com>
<sikfal$1pql$1@gioia.aioe.org>
<d2dfb313-e993-b4a9-9973-0c37acc4586c@scorecrow.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 24 Sep 2021 22:15:34 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="638e6ba33018fdaa9b85936e5d0d68d4";
logging-data="19280"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/CXk4uMTcVakIexcujugpQ"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.14.0
Cancel-Lock: sha1:zsTpc1YQammH+P3s1x6L0mTzhJc=
In-Reply-To: <d2dfb313-e993-b4a9-9973-0c37acc4586c@scorecrow.com>
X-Antivirus-Status: Clean
Content-Language: en-US
X-Antivirus: Avast (VPS 210924-4, 9/24/2021), Outbound message
 by: Ed Morton - Fri, 24 Sep 2021 22:15 UTC

On 9/24/2021 5:01 PM, Bruce Horrocks wrote:
<snip>
> Also the script has a bug: if the parameter text doesn't match any file
> then the script tries to copy a non-existent file and fails. The
> solution is to test that the file exists first before trying to copy it.

The better solution is to add

shopt -s nullglob

to the top so if the globbing pattern doesn't match anything the result
stored in `X` is null rather than the literal pattern used and so the
loop isn't entered.

But this would all be better discussed over at comp.unix.shell...

Ed.

>
> :::::::::::::
> #! /bin/bash
> # Filename: Rename_Test.sh
> N=1
> for X in *"$1"*
> do
>   if [ -f "$X" ]
>   then
>     echo cp "$X" "$(printf '%02d_%s.png' "$N" "$1")"
>     N=$((N+1))
>   fi
> done
> :::::::::::::
>
>

Re: Copying files with generated filenames in a directory from inside teh AWKprogram

<silprl$2hiig$1@news.xmission.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=929&group=comp.lang.awk#929

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gazelle@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.lang.awk
Subject: Re: Copying files with generated filenames in a directory from inside
teh AWKprogram
Date: Sat, 25 Sep 2021 00:17:57 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <silprl$2hiig$1@news.xmission.com>
References: <7faa87f4-881f-4b0b-8e79-d25a638bfe75n@googlegroups.com> <ac56111a-3657-4e28-8dc2-0daf7a9865fdn@googlegroups.com> <sikfal$1pql$1@gioia.aioe.org> <d2dfb313-e993-b4a9-9973-0c37acc4586c@scorecrow.com>
Injection-Date: Sat, 25 Sep 2021 00:17:57 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="2673232"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Sat, 25 Sep 2021 00:17 UTC

In article <d2dfb313-e993-b4a9-9973-0c37acc4586c@scorecrow.com>,
Bruce Horrocks <07.013@scorecrow.com> wrote:
....
>Also the script has a bug: if the parameter text doesn't match any file
>then the script tries to copy a non-existent file and fails. The
>solution is to test that the file exists first before trying to copy it.

Another way is to use bash's nullglob option.

I've always thought the way sh type shells handle "no file" globbing was
wrong (*). That nullglob should have been the default from the get-go.

Of course, the default can't be changed now, but going forward, most bash
code should set nullglob.

(*) Note that csh changed this - which was (and is) a Good Thing, but it is
different from either classic sh or bash-with-nullglob.

P.S. This thread needs to be moved to the shell group. It has nothing to
do with AWK.

--
People sleep peaceably in their beds at night only because rough
men stand ready to do violence on their behalf.

George Orwell

Re: Copying files with generated filenames in a directory from inside teh AWKprogram

<40ebf0c9-9a7d-4942-8d9b-4bd26a5989dan@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=930&group=comp.lang.awk#930

  copy link   Newsgroups: comp.lang.awk
X-Received: by 2002:ae9:ec0f:: with SMTP id h15mr16564016qkg.224.1632588521644; Sat, 25 Sep 2021 09:48:41 -0700 (PDT)
X-Received: by 2002:a25:8505:: with SMTP id w5mr18816667ybk.185.1632588521469; Sat, 25 Sep 2021 09:48:41 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!feeder1.feed.usenet.farm!feed.usenet.farm!tr3.eu1.usenetexpress.com!feeder.usenetexpress.com!tr1.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.awk
Date: Sat, 25 Sep 2021 09:48:41 -0700 (PDT)
In-Reply-To: <silprl$2hiig$1@news.xmission.com>
Injection-Info: google-groups.googlegroups.com; posting-host=2001:1711:fa54:36a0:68e0:c70b:587d:788c; posting-account=BtTavwoAAACMi3AFrA3U-JBERbxGUGhD
NNTP-Posting-Host: 2001:1711:fa54:36a0:68e0:c70b:587d:788c
References: <7faa87f4-881f-4b0b-8e79-d25a638bfe75n@googlegroups.com> <ac56111a-3657-4e28-8dc2-0daf7a9865fdn@googlegroups.com> <sikfal$1pql$1@gioia.aioe.org> <d2dfb313-e993-b4a9-9973-0c37acc4586c@scorecrow.com> <silprl$2hiig$1@news.xmission.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <40ebf0c9-9a7d-4942-8d9b-4bd26a5989dan@googlegroups.com>
Subject: Re: Copying files with generated filenames in a directory from inside teh AWKprogram
From: mohsen.owzar@gmail.com (Mohsen Owzar)
Injection-Date: Sat, 25 Sep 2021 16:48:41 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 35
 by: Mohsen Owzar - Sat, 25 Sep 2021 16:48 UTC

Kenny McCormack schrieb am Samstag, 25. September 2021 um 02:17:58 UTC+2:
> In article <d2dfb313-e993-b4a9...@scorecrow.com>,
> Bruce Horrocks <07....@scorecrow.com> wrote:
> ...
> >Also the script has a bug: if the parameter text doesn't match any file
> >then the script tries to copy a non-existent file and fails. The
> >solution is to test that the file exists first before trying to copy it.
> Another way is to use bash's nullglob option.
>
> I've always thought the way sh type shells handle "no file" globbing was
> wrong (*). That nullglob should have been the default from the get-go.
>
> Of course, the default can't be changed now, but going forward, most bash
> code should set nullglob.
>
> (*) Note that csh changed this - which was (and is) a Good Thing, but it is
> different from either classic sh or bash-with-nullglob.
>
> P.S. This thread needs to be moved to the shell group. It has nothing to
> do with AWK.
>
> --
> People sleep peaceably in their beds at night only because rough
> men stand ready to do violence on their behalf.
>
> George Orwell
Thanks alot to all for their contributions.

@ED, I know now there is a better way to reach my goal.
But nevertheless, I gathered some new perspectives with my bad concept.

Due to what is said above, I have to convert (dos2unix) all my scripts, written with notepad++
under Windows, to be able to run under Cygwin.

Regards
Mohsen

Pages:12
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor