LinuxQuestions.org
Review your favorite Linux distribution.
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 03-10-2013, 04:13 PM   #1
andrew44
Member
 
Registered: Apr 2011
Posts: 31

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, 05:09 PM   #2
rayfordj
Member
 
Registered: Feb 2008
Location: Texas
Distribution: Fedora, RHEL, CentOS
Posts: 488

Rep: Reputation: 78
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, 05:42 PM   #3
andrew44
Member
 
Registered: Apr 2011
Posts: 31

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, 05:45 PM   #4
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 572

Rep: Reputation: 120Reputation: 120
You're missing a "done".
 
1 members found this post helpful.
Old 03-10-2013, 05:49 PM   #5
andrew44
Member
 
Registered: Apr 2011
Posts: 31

Original Poster
Rep: Reputation: 0
Thanks again!
 
Old 03-10-2013, 06:11 PM   #6
andrew44
Member
 
Registered: Apr 2011
Posts: 31

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 06:13 PM.
 
Old 03-10-2013, 11:23 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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, 04:43 AM   #8
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
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 04:49 AM.
 
Old 03-17-2013, 07:26 PM   #9
andrew44
Member
 
Registered: Apr 2011
Posts: 31

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


Reply

Tags
awk stdin syntax, bash scripting



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

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

All times are GMT -5. The time now is 02:52 PM.

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