LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-19-2010, 05:52 PM   #1
NirvanaII
Member
 
Registered: Jun 2005
Posts: 139

Rep: Reputation: 18
Can Diff take arrays as input?


Does diff not like arrays? How do achieve the following?

Code:
stuart@stuart:~/music transfer$ diff file1.txt file2.txt 
1c1
< bonkers_in_phoenix.mp3
---
> bonker's_in_phoenix.mp3
But look:

Code:
stuart@stuart:~/music transfer$ echo $x
2
stuart@stuart:~/music transfer$ echo ${file[x]}
bonker's_in_phoenix.mp3
stuart@stuart:~/music transfer$ echo ${nopunc[x]}
bonkers_in_phoenix.mp3
stuart@stuart:~/music transfer$ diff ${file[x]} ${nopunc[x]} 
stuart@stuart:~/music transfer$
 
Old 06-19-2010, 11:19 PM   #2
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.

Some shells allow process substitution where the result of a command is substituted for a file:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate process substitution.

# Uncomment to run script as external user.
# export PATH="/usr/local/bin:/usr/bin:/bin"
# Infrastructure details, environment, commands for forum posts. 
set +o nounset
pe() { for i;do printf "%s" "$i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe ; pe "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
pe "(Versions displayed with local utility \"version\")"
c=$( ps | grep $$ | awk '{print $NF}' )
version >/dev/null 2>&1 && s=$(_eat $0 $1) || s=""
[ "$c" = "$s" ] && p="$s" || p="$c"
version >/dev/null 2>&1 && version "=o" $p diff
set -o nounset

# Define content of 2 arrays.

a=( now is the time )
b=( this is not then )

j=2
pl " Content of array element $j:"
pe ${a[$j]}
pe ${b[$j]}

pl " Results:"
diff <( echo ${a[$j]} ) <( echo ${b[$j]} )

exit 0
producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0 
GNU bash 3.2.39
diff (GNU diffutils) 2.8.1

-----
 Content of array element 2:
the
not

-----
 Results:
1c1
< the
---
> not
This is a property of the shell, not diff ... cheers, makyo

Last edited by makyo; 06-20-2010 at 07:54 PM. Reason: ( Edit 1: minor typo )
 
Old 06-20-2010, 03:12 PM   #3
NirvanaII
Member
 
Registered: Jun 2005
Posts: 139

Original Poster
Rep: Reputation: 18
Thanks makyo!

My knowledge is too limited to understand everything that is going on here, but I've got the 'bit I wanted' to work on my script. I'll read this again at some stage, once i've a deeper understanding of things.

Here's another problem that's driving me mad:

Code:
awk NR==$x Musiclist.txt | cut -d/ -f$num | basename $1 .mp3
How do I get the piped output to read as an argument for the basename command? I know I can do it with xargs but i'm wanting to avoid this command as it throws a fit when fed punctuation.
 
Old 06-20-2010, 07:50 PM   #4
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
Originally Posted by NirvanaII View Post
Thanks makyo!

My knowledge is too limited to understand everything that is going on here, but I've got the 'bit I wanted' to work on my script. I'll read this again at some stage, once i've a deeper understanding of things.

Here's another problem that's driving me mad:

Code:
awk NR==$x Musiclist.txt | cut -d/ -f$num | basename $1 .mp3
How do I get the piped output to read as an argument for the basename command? I know I can do it with xargs but i'm wanting to avoid this command as it throws a fit when fed punctuation.
Code:
basename $(awk NR==$x Musiclist.txt | cut -d/ -f$num | basename $1 .mp3)
or
Code:
basename `awk NR==$x Musiclist.txt | cut -d/ -f$num | basename $1 .mp3`
 
Old 06-20-2010, 08:29 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
I would add to smeezekitty's reply and say it is probably a waste to pipe all of this when awk more than likely would do all the work.
Just a thought
 
Old 06-20-2010, 09:08 PM   #6
NirvanaII
Member
 
Registered: Jun 2005
Posts: 139

Original Poster
Rep: Reputation: 18
I'll have to read up on awk, grail. (so much to learn! )

Hmm... that code is giving me the same output as before:

Code:
stuart@stuart:~/music transfer$ echo $num
7
stuart@stuart:~/music transfer$ echo $x
1
stuart@stuart:~/music transfer$ basename $(awk NR==$x Musiclist.txt | cut -d/ -f$num | basename $1 .mp3)
.mp3
stuart@stuart:~/music transfer$ basename `awk NR==$x Musiclist.txt | cut -d/ -f$num | basename $1 .mp3`
.mp3
stuart@stuart:~/music transfer$ awk NR==$x Musiclist.txt | cut -d/ -f$num 
Neil Young - Don't Let it Bring You Down.mp3

Last edited by NirvanaII; 06-20-2010 at 09:15 PM.
 
Old 06-21-2010, 12:48 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Maybe you would supply a before and after picture so we can help?

ie. are the names of mp3 files the only thing in Musiclist.txt or is there a path as well?

i also presume the after is just the name of the song without extension?
 
Old 06-21-2010, 05:27 AM   #8
NirvanaII
Member
 
Registered: Jun 2005
Posts: 139

Original Poster
Rep: Reputation: 18
Quote:
Originally Posted by grail View Post
Maybe you would supply a before and after picture so we can help?

ie. are the names of mp3 files the only thing in Musiclist.txt or is there a path as well?

i also presume the after is just the name of the song without extension?
In the above example we are looking at the seventh field, of the absolute path entry for track one:

Code:
stuart@stuart:~/music transfer$ cat Musiclist.txt 
/mnt/sda1/Music/Neil Young/Live/Neil Young - Don't Let it Bring You Down.mp3
/mnt/sda1/Music/Neil Young/Live/Neil Young - A Day i'n the Life (live@Gla.mp3
/mnt/sda1/Music/Neil Young/Live/Neil Young - A Day in the Life (live@Gla.mp3
/mnt/sda1/Music/Neil Young/Live/Neil Young - Out On The Weekend.mp3
/mnt/sda1/Music/Neil Young/Live/Neil Young OLD MAN.mp3
/mnt/sda1/Music/Neil Young/Live/Neil Young - Needle and the Damage Done.mp3
 
Old 06-21-2010, 06:43 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
So I have 2 solutions you can choose from:

One liner:
Code:
awk -F"/" '{sub(/\..*/,"",$NF);print $NF}' Musiclist.txt
Short bash script:
Code:
#!/bin/bash

while read line
do
	tmp=$(basename "${line}" | cut -d. -f1)

	echo "$tmp"
done < Musiclist.txt
You could also change basename for parameter substitution, but either works ok
 
Old 06-21-2010, 07:29 AM   #10
NirvanaII
Member
 
Registered: Jun 2005
Posts: 139

Original Poster
Rep: Reputation: 18
Quote:
Originally Posted by grail View Post
So I have 2 solutions you can choose from:

One liner:
Code:
awk -F"/" '{sub(/\..*/,"",$NF);print $NF}' Musiclist.txt
Short bash script:
Code:
#!/bin/bash

while read line
do
	tmp=$(basename "${line}" | cut -d. -f1)

	echo "$tmp"
done < Musiclist.txt
You could also change basename for parameter substitution, but either works ok
How would I go about combining the first of these commands with 'awk NR==$x Musiclist.txt'? i.e. Output line x of the list?

Why isn't smeezekitty's command working do you think?

(This awk command comes up with some frequency, i'm going to have to read into it as some point.)
 
Old 06-21-2010, 07:36 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Cleanest way is probably:
Code:
awk -F"/" -v line=$x 'NR==line{sub(/\..*/,"",$NF);print $NF}' Musiclist.txt
 
Old 06-21-2010, 07:41 AM   #12
NirvanaII
Member
 
Registered: Jun 2005
Posts: 139

Original Poster
Rep: Reputation: 18
Quote:
Originally Posted by grail View Post
Cleanest way is probably:
Code:
awk -F"/" -v line=$x 'NR==line{sub(/\..*/,"",$NF);print $NF}' Musiclist.txt
Nice work!

My program should work now, i'll test it all later today.
 
Old 06-21-2010, 12:33 PM   #13
NirvanaII
Member
 
Registered: Jun 2005
Posts: 139

Original Poster
Rep: Reputation: 18
If you're still interested grail, here's the close to finished command (it works, just crude!):

Code:
convert () { mplayer -cache 32 -ao pcm:file="${file[x]}".wav "${file[x]}".mp3; lame -V2 "${file[x]}".wav "${file[x]}".flac; } ; find "$My_Music" -iname "*.mp3" > Musiclist.txt ; y=$(sed 's@ @\\ @g' Musiclist.txt | wc -l); ((y=y+1));  typeset -i x; x=1; while ((x < y)) && fieldcut[x]=$(awk NR==$x Musiclist.txt | tr -d '[a-z,A-Z,0-9]',[' ','.','\55','\12','@','_','\50','\51','\47'] | wc -c) &&  path[x]=$(awk NR==$x Musiclist.txt | cut -d/ -f1-"${fieldcut[x]}") &&  file[x]=$(awk -F"/" -v line=$x 'NR==line{sub(/\..*/,"",$NF);print $NF}' Musiclist.txt) &&  ln -s "${path[x]}/${file[x]}".mp3 "${file[x]}".mp3; do ((x=x+1)); done ; x=1;  while (( x < y )) && convert; do (( x=x+1)); done;  rm -vf *.wav *.mp3 ; x=1; while (( x < 30)) && mv -v "${file[x]}".flac "${path[x]}"; do ((x=x+1)); done
I can see some bloat just looking at it:

Code:
fieldcut[x]=$(awk NR==$x Musiclist.txt | tr -d '[a-z,A-Z,0-9]',[' ','.','\55','\12','@','_','\50','\51','\47'] | wc -c) &&  path[x]=$(awk NR==$x Musiclist.txt | cut -d/ -f1-"${fieldcut[x]}")
$path[x] can be given in one awk command, i'm guessing, and fieldcut[x] purged.

Code:
y=$(sed 's@ @\\ @g' Musiclist.txt | wc -l); ((y=y+1))
And this expressed as one command. The 'sed command' was used for a different purpose originally, it just happens to work here.

The rest looks moderately trim to my unitiated eye. Now that it works -- to prune! Any suggestions, including instructions?

(and i'd still like to know what's up with smeezekitty's code???)
 
Old 06-21-2010, 01:13 PM   #14
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Shell bug?
I use that method all the time and it works fine.
 
Old 06-21-2010, 01:18 PM   #15
NirvanaII
Member
 
Registered: Jun 2005
Posts: 139

Original Poster
Rep: Reputation: 18
Quote:
Originally Posted by smeezekitty View Post
Shell bug?
I use that method all the time and it works fine.
I'm running the default shell in Antix (a Mepis fork), which I don't know, but would imagine is bash (?)

I'll boot up Puppy and try the command here...
 
  


Reply



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

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



Similar Threads
Thread Thread Starter Forum Replies Last Post
input output error while running 'diff' on a ntfs disk nass Linux - Software 1 12-14-2008 08:56 AM
Arrays of Structures to Arrays of Classes knobby67 Programming 1 01-01-2008 01:39 PM
Arrays - User input - newbie = [ ohfaney Programming 6 05-01-2006 10:14 PM
Question about outputing arrays with pointers, then just arrays... RHLinuxGUY Programming 1 04-12-2006 05:40 AM
Dual Boot diff Hard Disk diff OS on Suse 9.1 wilhem Linux - Newbie 1 08-13-2004 06:06 PM

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

All times are GMT -5. The time now is 09:34 AM.

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