LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 03-10-2013, 05:13 PM   #1
andrew44
LQ Newbie
 
Registered: Apr 2011
Posts: 29

Rep: Reputation: 0
Possible passwords from one column text file.


Hi,

I have lost the password for an encrypted volume.

I have written a script to input the passwords.

I have written down all the possible passwords into a text file. I know the password I have is a combination of two of these

The file contains something like

password1
password2
password3

I want something to generate a new list of passwords covering all the permutations with a space between each.

password1 password2
password1 password3
password2 password1
password2 password3

you get the idea.

Any help is much appreciated!
 
Old 03-10-2013, 06:09 PM   #2
rayfordj
Member
 
Registered: Feb 2008
Location: Texas
Distribution: Fedora, RHEL, CentOS
Posts: 475

Rep: Reputation: 73
Probably not the best, but this may get you along your way...

Code:
for i in $(cat /tmp/pw-file ); do for j in $(cat /tmp/pw-file ); do echo ${i} ${j}; done; done
where /tmp/pw-file is your file with passwords
 
1 members found this post helpful.
Old 03-10-2013, 06:42 PM   #3
andrew44
LQ Newbie
 
Registered: Apr 2011
Posts: 29

Original Poster
Rep: Reputation: 0
Thanks

Perfect thanks very much.

If I want three sets of passwords how would I do that?

for i in $(cat passwords ); do for j in $(cat passwords ); do for k in $(cat passwords ); do echo ${i} ${j} ${k}; done; done

Doesn't work?
 
Old 03-10-2013, 06:45 PM   #4
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 547

Rep: Reputation: 115Reputation: 115
You're missing a "done".
 
1 members found this post helpful.
Old 03-10-2013, 06:49 PM   #5
andrew44
LQ Newbie
 
Registered: Apr 2011
Posts: 29

Original Poster
Rep: Reputation: 0
Thanks again!
 
Old 03-10-2013, 07:11 PM   #6
andrew44
LQ Newbie
 
Registered: Apr 2011
Posts: 29

Original Poster
Rep: Reputation: 0
What happens if I have spaces in the line?

honi soit qui mal y pense

becomes
i soit qui mal y pe i soit qui mal y pe

if I run

for i in $(cat passwords ); do for j in $(cat passwords ); do echo ${i} ${j}; done; done

Last edited by andrew44; 03-10-2013 at 07:13 PM.
 
Old 03-11-2013, 12:23 AM   #7
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,311

Rep: Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040
First, put the script on multiple lines; much easier to read. Second, set bash env var IFS to newline eg
http://ubuntuforums.org/showthread.php?t=820403
Code:
# set IFS to newline only; default is space or tab or newline
IFS=$'\n'

for i in $(cat passwords )
do 
    for j in $(cat passwords )
    do 
        echo ${i} ${j}
    done
done
Note that if you need to reset the IFS during the script, save the old value away to a var and re-assign as reqd.

When the script finishes, IFS will of course be whatever the calling shell value was; it doesn't get changed unless you source the script or run it directly at the cli.
 
Old 03-11-2013, 05:43 AM   #8
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 651

Rep: Reputation: 269Reputation: 269Reputation: 269
Quote:
Code:
for i in $(cat passwords )
It is much better to read files with while and read (Don't read lines with for):

Code:
while read i; do
    ...
done < passwords
Quote:
Code:
echo ${i} ${j}
The braces here are not necessary. On the other hand, the quotes are:

Code:
echo "${i} ${j}"
Another alternative:

Code:
mapfile -t words<passwords
for i in "${words[@]}"; do
    for j in "${words[@]}"; do
	echo "$i $j"
    done
done

Last edited by millgates; 03-11-2013 at 05:49 AM.
 
Old 03-17-2013, 08:26 PM   #9
andrew44
LQ Newbie
 
Registered: Apr 2011
Posts: 29

Original Poster
Rep: Reputation: 0
Thanks to everyone that helped. Problem solved.
 
  


Reply

Tags
awk stdin syntax, bash scripting


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Text file column editing crowzie Linux - Newbie 5 07-17-2011 08:25 AM
[SOLVED] Insert a column of text to a text file docaia Linux - Newbie 5 06-02-2011 11:55 AM
Read text file column by column RVF16 Programming 11 05-31-2009 08:16 AM
Swap column of text file kushalkoolwal Programming 4 09-05-2008 12:30 PM
How to parse text file to a set text column width and output to new text file? jsstevenson Programming 12 04-23-2008 03:36 PM


All times are GMT -5. The time now is 10:48 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration