David Aldridge
asked on
Perl one-liner to remove the last line in a file
I want to ssh to another server and be able to remove the last line from the /etc/sudoers file. I'm NOT very good at one-liners, but I need one here. Any help would be greatly appreciated!
Thanks!
David
Thanks!
David
ASKER
I tried that, but I can't get sed to work on the remote system. Here's the way I ran the command:
ssh myserver "sed '$d' /etc/sudoers /etc/sudoers.rev" It makes the sudoers.rev, but doesn't remove the last line.
ssh myserver "sed '$d' /etc/sudoers /etc/sudoers.rev" It makes the sudoers.rev, but doesn't remove the last line.
Try this: Run it without the > and the output file. Output should go to terminal. Check to see if it removes the last line.
Note that the last line must be the last line. No trailing blank lines, no extra empty lines, no CRLF lines. sed removes the last line, not the last line with something on it.
Note that the last line must be the last line. No trailing blank lines, no extra empty lines, no CRLF lines. sed removes the last line, not the last line with something on it.
root@www:# cat test.txt
87.87.232.0/24
16.255.0.0/22
192.168.0.1/30
222.30.0.0/15
84.84.22.0/28
0.0.10.0/24
root@www:# sed '$d' test.txt
87.87.232.0/24
16.255.0.0/22
192.168.0.1/30
222.30.0.0/15
84.84.22.0/28
root@www:# sed '$d' test.txt > test.out
root@www:# cat test.out
87.87.232.0/24
16.255.0.0/22
192.168.0.1/30
222.30.0.0/15
84.84.22.0/28
ASKER
It's the last line with something on it. I'm having a little luck with grep -v, but still working on getting it to function through an ssh command.
From home, I will try the command from David Aldridge:
Always use full path for any command you are using.
ssh myserver "sed '$d' /etc/sudoers /etc/sudoers.rev"
Try replacing that command to:/usr/bin/ssh myserver "/bin/sed '$d' /etc/sudoers >/etc/sudoers.rev;/usr/bin/wc -l /etc/sudoers*"
I will try following command at home:/usr/bin/ssh myserver "if test -f /etc/sudoers.rev;then /usr/bin/wc -l /etc/sudoers.rev;else echo Initially /etc/sudoers.rev file is not present;fi;/bin/sed '$d' /etc/sudoers >/etc/sudoers.rev;/usr/bin/wc -l /etc/sudoers*"
Let us know the output for the same.Always use full path for any command you are using.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Excellent!
Open in new window
The original file will be preserved. You can then copy the output file back over the input file, or do whatever else is necessary with it.