I\'m trying to encrypt a textfile with original text: \"hello world\" using anot
ID: 3750186 • Letter: I
Question
I'm trying to encrypt a textfile with original text: "hello world" using another textfile(called the codebook) which has the content: cijklmpqrstxyzabnodefuvwgh (line1) CDEMNOPQRSTGHUVXYZAIJKLWBF(line2) in linux. I'm an absolute beginner and I think I'm only supposed to write a bash file with around 4-5 lines using tr command to translate "hello world" using the codebook. For example, the small a will be replaced with c and b with i and etc.
One last thing to note is that the program should run with codebook.txt as first argument and original_input.txt as second argument.
The code I've written is the following:
$ cat Q2/test_input1.txt | tr 'a-z' 'head -n 1 Q2/codebook.txt' tr 'A-Z' 'head -n 2 Q2/codebook.txt' > Q2/test_version.txt
But it doesn't work.
Explanation / Answer
Correct command:
cat Q2/test_input1.txt | tr 'a-z' $(head -n 1 Q2/codebook.txt) | tr 'A-Z' $(tail -n 1 Q2/codebook.txt') > Q2/test_version.txt
This will give you the expected answer.
$(head -n 1 filename) command retrieves upto first one line of the file.
$(head -n 2 filename) command retrieves upto first two lines of the file.
and so on.
$(tail -n 1 filename) command retrieves lines from end of file i.e last one line.
$(tail -n 2 filename) command retrieves lines last two lines .and so on.
The cat command first takes the input file and using tr we replace the order of the alphabets as from 'a-z' to first line of the file and "A-Z" will be replced according to the second line of the file.
and finally output is written to another file.