I am trying to read a file using a while loop running as sudo, and then run some awk on each line. File owner is user2 and I am running as user1.
sudo sh -c 'while IFS= read -r line; do
echo $line | awk '{print $NF}'; done < /home/user2/test.txt'
I am having trouble with the single quotes. Double quotes does not work at both the places where I have single quotes. Is there any way to get the same command to work with some adjustments?
NOTE: I got the output using the following methods:
-
sudo cat /home/user2/test.txt | while IFS= read line; do echo $line | awk '{print $NF}' done
-
sudo awk {'print $NF'} /home/user2/test.txt
I am trying to understand if there is any solution for using sudo, while and awk with single quotes all of them in a single line.