Grabbing the last field of a delimited entry without using "awk" or "sed"
Using a do-while loop, I am processing entries contained in a file, each on a separate line. Each entry is a path to a file. Each file will be encrypted using openssl, and the encrypted file will be saved in directory called "/home/backup". Since I am not replicating the path of each file in the "/home/backup" directory, I only need the last field which contains the file name.
I need a command, or series of commands, that can grab the last field of a delimited entry without using the "awk" or "sed" commands. Below is the file "/root/CONFIGS" containing paths to files (the exception is the "/etc/xinetd.d directory"), as well as the code that I have so far. The openssl code won't work until I give it a valid file output (the last field). Code:
/boot/grub/menu.lst Code:
function1() |
Had a look at basename?
Or bash string substitution? Code:
$ tmp=/boot/grub/menu.lst Cheers, Tink |
Any particular reason you can't use sed or awk? Anyway, here's a creative use of cut and rev:
Code:
$ cat CONFIGS | rev | cut -d'/' -f1 | rev |
Quote:
I am working on this as part of a scripting project and I have been told that I "should use the cut command". Quote:
|
Quote:
|
Quote:
If you can get away w/ out it - here's a version of your original function which I believe I modified in a way that would let you use the original file unmodified, and w/o use of external commands: Code:
function1() Cheers, Tink |
Quote:
As it so happens, I have a somewhat related question that have run into. In addition to creating encrypted copies of the files, I also need to checksum each file (I'm using sha256sum), creating a list of checksums in the process, and then creating an encrypted file containing that list (using openssl again). The catch is that the list of checksums cannot be stored in a temporary file. This presents two issues. First, appending multiple lines of data to something other than a temp file - I'm thinking a variable would be suitable. Based on what I have Googled, it is possible, but I do not understand how. Second, to encrypt a file with openssl you have to specify an input file. I've tried using a variable instead of an input file, but openssl spits errors. Thoughts? |
You can make openssl read from STDIN, and output to STDOUT.
Cheers, Tink |
Quote:
|
W/o trying to replicate your set-up for testing: you should be able
to do the processing in a loop within a sub-shell, and not have to store anything, not even in RAM (I would have thought). Cheers, Tink |
Quote:
Concatenation, as I have now tried, also works well. Code:
file="abc.txt" |
Maybe I didn't full understand:
Code:
for i in $(cat files ); do sha256sum $i; done If you slapped that in front of a pipe to openssl for encryption you shouldn't need to store anything? Obviously the loop would happen over the lines you read from your files .... Cheers, Tink |
Quote:
Does that make sense? I might be confusing more than I am helping! |
Quote:
You already have that loop working, right? I don't understand why you think you need to store the lines w/ the sha's ... can you give us a larger snippet of your code? If the desired result is to have ALL checksummed file names into an encrypted file I can't see any need for storing any output in a variable (or any other way, for that matter) - just pipe it to openssl, encrypt it from stdin, and write the results to your (then encrypted) file - voila. Cheers, Tink |
Quote:
My code is included below. Bear with me as I have changed some variable names. The variable containing checksums of files is shown in red, but based on what you have told me, this variable is unnecessary. The code that encrypts the checksums in the variable is shown in blue, but it produces error messages and does not work. The contents of the CONFIGS file is posted earlier in this thread. Code:
while read filePath #Read file paths from file |
If I haven't overlooked anything major:
Code:
while read filePath #Read file paths from file |
Quote:
Is there anything that Linux gurus can't do? Thank you, again! |
Sweet, and welcome.
|
All times are GMT -5. The time now is 01:16 AM. |