Dear all,
I have two files which contain the same number of lines. Each line
corresponds to file names, e.g.:
file1.txt:
my file 1.txt
my file 2.txt
my file 3.txt
file2.txt:
my_file_1.txt
my_file_2.txt
my_file_3.txt
I would like to read in parallel line by line of both files, since a
line coming from file2.txt is going to be used to change the
corresponding file's name in file1.txt
Best wishes,
Javier
|
|
0
|
|
|
|
Reply
|
Javier
|
5/26/2010 4:09:45 PM |
|
In article <8add1f0d-7a1e-4754-9a5f-72996c30ffa5@i31g2000vbt.googlegroups.com>,
Javier Montoya <jmontoyaz@gmail.com> wrote:
>Dear all,
>
>I have two files which contain the same number of lines. Each line
>corresponds to file names, e.g.:
>file1.txt:
>my file 1.txt
>my file 2.txt
>my file 3.txt
>
>file2.txt:
>my_file_1.txt
>my_file_2.txt
>my_file_3.txt
>
>I would like to read in parallel line by line of both files, since a
>line coming from file2.txt is going to be used to change the
>corresponding file's name in file1.txt
>
>Best wishes,
>
>Javier
The best way to read multiple files in AWK (something I've done a lot of)
is to read each file in turn, saving the stuff you need in arrays. You
arrange it so that the last file is the one that triggers the actions
you want to perform (in database-speak, all but the last file are
"lookup files" and the last file is the "master file").
So, your GAWK script (and if you're not using GAWK, you know how to fix
that problem) will look like:
ARGIND == 1 { do stuff with file 1;next }
ARGIND == 2 { do stuff with file 2;next }
....
# Master file now
/selectlinesinmasterfiletoworkwith/ {
do stuff now, using arrays and data structures built from the other files
}
and then you call it like:
gawk -f foo.awk lfile1 lfile2 ... masterfile
--
> No, I haven't, that's why I'm asking questions. If you won't help me,
> why don't you just go find your lost manhood elsewhere.
CLC in a nutshell.
|
|
0
|
|
|
|
Reply
|
gazelle
|
5/26/2010 4:18:03 PM
|
|
I am guessing that you are trying to rename a list of files in
file1.txt to those names in file2.txt.
1. If you are looking to simply replace spaces with underscore, then
you don't even need file2.txt. Here is how:
awk '{old=$0; gsub(/ /, "_"); cmd=sprintf("mv \"%s\" %s", old,
$0); print cmd}' file1.txt
This will print out a list of mv commands for you to inspect visually.
Once you are satisfied with the result, replace "print cmd" with
"system(cmd)"
----
2. If you really want to rename file names in file1.txt to those in
file2.txt and if your system has the "lam" command:
lam -s 'mv "' file1.txt -s '" "' file2.txt -s '"'
Again, the result is a list of rename commands. Inspect them to make
sure that they are what you want then feed them into the shell to
rename:
lam -s 'mv "' file1.txt -s '" "' file2.txt -s '"' | sh
|
|
0
|
|
|
|
Reply
|
Hai
|
5/27/2010 6:11:50 PM
|
|
If you are into rename, here is another solution:
$ cat rename.awk
# For first file, NR and FNR are the same
NR == FNR {
old[NR] = $0
}
# This is for subsequent files, specifically second file
NR != FNR {
cmd = sprintf("mv \"%s\" \"%s\"", old[FNR], $0)
print cmd
}
$ awk -f rename.awk file1.txt file2.txt
mv "my file 1.txt" "my_file_1.txt"
mv "my file 2.txt" "my_file_2.txt"
mv "my file 3.txt" "my_file_3.txt"
If this is what you want, then change "print cmd" to "system(cmd)"
|
|
0
|
|
|
|
Reply
|
Hai
|
5/27/2010 6:23:13 PM
|
|
In article <d0e13735-8817-4570-831d-e6ee670cd1c5@y18g2000prn.googlegroups.com>,
Hai Vu <haivu2004@gmail.com> wrote:
>I am guessing that you are trying to rename a list of files in
>file1.txt to those names in file2.txt.
>
>1. If you are looking to simply replace spaces with underscore, then
>you don't even need file2.txt. Here is how:
> awk '{old=$0; gsub(/ /, "_"); cmd=sprintf("mv \"%s\" %s", old,
>$0); print cmd}' file1.txt
>
>This will print out a list of mv commands for you to inspect visually.
>Once you are satisfied with the result, replace "print cmd" with
>"system(cmd)"
Correction: Do not change the AWK script to invoke system().
Instead, take the file that you created, and have verified that it looks
right, and execute it as a shell script. Much safer.
>----
>
>2. If you really want to rename file names in file1.txt to those in
>file2.txt and if your system has the "lam" command:
>
> lam -s 'mv "' file1.txt -s '" "' file2.txt -s '"'
>
>Again, the result is a list of rename commands. Inspect them to make
>sure that they are what you want then feed them into the shell to
>rename:
>
> lam -s 'mv "' file1.txt -s '" "' file2.txt -s '"' | sh
Again, don't do this. Instead, take the (verified) file and feed that
to the shell.
BTW, I'm not sure why you are recommending "lam" - a very non-standard
program that I had not even heard of until a few weeks ago - over AWK,
which is both standardized and on-topic.
--
> No, I haven't, that's why I'm asking questions. If you won't help me,
> why don't you just go find your lost manhood elsewhere.
CLC in a nutshell.
|
|
0
|
|
|
|
Reply
|
gazelle
|
5/27/2010 7:14:06 PM
|
|
|
4 Replies
207 Views
(page loaded in 0.069 seconds)
|