6

I have a text file that contains passwords and their respective hashes that is formatted as such:

'plaintext password'
'SHA256 hash'
'SHA3-512 hash'

This format continues for several more passwords. Each password and hash has its own line within the file. Each of the hashes below the 'plaintext password' are its respective hashes. I want to create a CSV file that will end up looking like this:

'plaintext password','SHA256 hash','SHA3-512 hash'

Essentially, I want to combine those three fields into one line for each password, delimited by commas for each field. I have tried searching for similar situations to mine but with no avail. I have tried awk as well as tr -s, but I haven't been able to get exactly what I want. I would appreciate the help!

1 Answers1

10

You can try

paste -d, - - - < file.txt > file.csv

The paste command generally merges lines of files side-by-side (with an optional delimiter supplied by -d). In this case we are telling it to read the standard input stream 3 times - - - instead.

steeldriver
  • 131,985
  • 21
  • 239
  • 326
  • 1
    That did the trick! Would you mind explaining what the hyphenations do? I understand the paste command as well as the -d flag, but why the extra three hyphens? – Ultracrepidarian Mar 01 '20 at 22:18
  • @Ultracrepidarian I have added a brief explanation – steeldriver Mar 01 '20 at 22:26
  • @steeldriver just out of curiosity, is it possible to generates separate columns for each word per line, e.g: `(Line #1) This is line number one. (Line #2) This line have fewer words. (Line #3) This line number three have more words in it than other lines.` Thanks. – Jags Mar 01 '20 at 22:50
  • 1
    @Jags that sounds like a separate (different) question - although the formating limitations of the comment box make it hard to know exactly what you're asking – steeldriver Mar 01 '20 at 22:54
  • @steeldriver Okay, I've just asked a separate question. Thanks. `https://askubuntu.com/questions/1214160/create-a-csv-file-from-txt-file` – Jags Mar 01 '20 at 23:12