remove specified text from file

Login or Register for Dates, Times and to Reply

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting remove specified text from file
# 1  
remove specified text from file

I am trying to write a script that kills old sessions, I've posted here over the past few days and the script is just about perfect except I want to be given the option to exclude specified PIDs from being killed. this is the entire script:
if [ -e /tmp/idlepids ]
rm /tmp/idlepids
if [ -e /tmp/idlepidss ]
rm /tmp/idlepidss
if [ -e /tmp/unwantedpids ]
rm /tmp/unwantedpids
numusers=$(who -q | grep Total | tr -s " " | cut -d" " -f3)
echo Total number of sessions: ${numusers}
if test $numusers -gt 60; then
diff=$(expr $numusers - 60)
if test $diff -eq 1
echo "There is $diff too many sessions. Killing the oldest session."
echo "There are $diff too many sessions, killing the $diff oldest sessions:"
who -u -H | sort -r -k6.2,6 | grep bhb | tr -s " " | cut -d" " -f7 >> /tmp/idlepids
head -$diff /tmp/idlepids >> /tmp/idlepidss
echo "About to kill the following sessions:"
while read PID; do who -u | grep $PID | sort -k4.2,6; done</tmp/idlepidss
#echo "Press ENTER to continue..."
#read enterKey
echo "Last chance to save PIDs..."
echo "Enter up to 9 PID numbers you want to keep alive: \c"
read pid1 pid2 pid3 pid4 pid5 pid6 pid7 pid8 pid9
cat /tmp/idlepidss | grep -v $pid1 | grep -v $pid2 | grep -v $pid3 | grep -v $pid4 | grep -v $pid5 | grep -v $pid6 | grep -v $pid7 | grep -v $pid8 | grep -v $pid9 > /tmp/unwantedpids

while read PID; do kill $PID; done</tmp/unwantedpids
echo "Sessions killed. Exiting"
echo "There are less than 60 users. Exiting"
exit 1

It gives a bunch of grep usage errors
I tested grep -v on the command line and it works.
# cat /tmp/idlepidss
# echo $pid1 $pid2 $pid3

# read pid1 pid2 pid3
43532 35134
# cat /tmp/idlepidss | grep -v $pid1 $pid2
grep: 0652-033 Cannot open 35134.
# set -o vi
# cat /tmp/idlepidss | grep -v $pid1 |grep -v $pid2

# 2  
Probably a quoting issue; if $pid2 is the empty string then you get a syntax error for grep -v $pid2 but it's easy to fix: grep -v "$pid2" with double quotes around the variable (always when using variables, as a strong recommendation). However, you also need to watch out for grep -v "" because it will remove all lines. As a workaround, you could use "${pid2:-nosuchstringmadam}" to replace an empty variable value with an unlikely default string.

As an aside, you can also simplify the long string of greps with egrep -v "$pid1|$pid2|$pid3" etc. And of course, cat file | grep something is always possible to write as grep something file (the purpose of cat is to catenate multiple files together; using it on a single file is usually misdirected).
# 3  
Thanks a lot, worked great Smilie
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #928
Difficulty: Medium
64-bit versions of the Unix time stamp will cease to work at 15:30:08 UTC on Sunday, 14 December 2922770265960.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove the text between all curly brackets from text file?

Hello experts, I have a text file with lot of curly brackets (both opening { & closing } ). I need to delete them alongwith the text between opening & closing brackets' pair. For ex: Input:- 59. Rh1 Qe4 {(Qf5-e4 Qd8-g8+ Kg6-f5 Qg8-h7+ Kf5-e5 Qh7-e7+ Ke5-f5 Qe7-d7+ Qe4-e6 Qd7-h7+ Qe6-g6... (6 Replies)
Discussion started by: prvnrk
6 Replies

2. Shell Programming and Scripting

sed to remove text from file

Trying to use sed to, in-place, remove specific text from a file. Since there are / in the text I use | to escape that character. Thank you :). sed -i -e 's|xxxx://*/|' /home/cmccabe/list sed: -e expression #1, char 51: unterminated `s' command (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Shell Programming and Scripting

Help with remove backticks in a text file

Input file: 'data_1' 'data_10' 'data1311' '235data_13' Desired output: data_1 data_10 data1311 235data_13 Can I know how to remove back tick"'" in a file? Many thanks for advice. (3 Replies)
Discussion started by: perl_beginner
3 Replies

4. Shell Programming and Scripting

Search and remove in a text file

Need help whit a script where I have to input a name and then remove a line where that name is in a file file ex: 001op;Name;Location;date 002op;Name;Location;date and so on.... can anybody help me??? thanks (4 Replies)
Discussion started by: nogame11
4 Replies

5. Shell Programming and Scripting

Remove filename is text file

Hello, I got files full path in a text file like that /main/k/kdelibs/kdelibs4c2a_3.5.10.dfsg.1-2ubuntu7_i386.deb /main/k/kdelibs-experimental/libknotificationitem-dev_4.3.2-0ubuntu1_i386.deb /main/k/kdemultimedia/dragonplayer_4.3.2-0ubuntu1_i386.deb... (13 Replies)
Discussion started by: davidkhan
13 Replies

6. Shell Programming and Scripting

what command is used to remove the all text of the particular file.

Hi all... I want to delete the entire text of the file and want to make it zero byte.. would you please tell me the command for it. Thanks and regards Vijay sahu (4 Replies)
Discussion started by: vijays3
4 Replies

7. Shell Programming and Scripting

remove chunks of text from file

All, So, I have an ldif file that contains about 6500 users worth of data. Some users have a block of text I'd like to remove, while some don't. Example (block of text in question is the block starting with "authAuthority: ;Kerberosv5"): User with text block: # username, users,... (7 Replies)
Discussion started by: staze
7 Replies

8. Shell Programming and Scripting

able to remove \240 from a text file

I have a file originally provided from a SQL database on a Windows platform. I transfer the file via ftp in binary format, remove the ^M's from the end of all lines. I have attempted to use tr ( cat infile | tr -d '\240' ) and sed (cat infile | sed 's/\240//g' ) to remove the occurences of... (11 Replies)
Discussion started by: pbrowne
11 Replies

9. Shell Programming and Scripting

remove and replace text in a file

Hello all, How would I go to a particular line in a file and remove certain text from it and replace with something that I want it to be there. like: file /etc/abc now look for line HOME="/export/xyz" in /etc/abc and then replace with HOME=/"export/xyz1" thanks in advance guys. (1 Reply)
Discussion started by: solaix14
1 Replies

10. Shell Programming and Scripting

Best way to remove sections of text from a file

Greetings! I found this fourm via a google search on "sed expressions". I have a file that contains notices and they are all the same length in lines. For example the file would contains 15 notices, each being 26 lines each. I need some way to eliminate notices that contain a "S" in a particular... (8 Replies)
Discussion started by: cals64
8 Replies

Featured Tech Videos