ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Is there a way to do this to tell grep to return everything except the Username and the following line?
I am writing a script to automatically update users and passwords in a file. So if someone were to run the script it would go in to a file and delete a specific line (username) and then the line following (password).
I want my script to delete the "Username" and always the following line which will be a "password". I was trying to do it by using the following:
"cat filename.txt |grep -v Username1 > newfile.txt" but this only takes out the username, and I can't use it for the password line because some passwords will be the same.
Is there a way to do this to tell grep to return everything except the Username and the following line?
You may be able to shoehorn grep into doing this for you by using the -P switch (perl regular expressions) and then specifying a multi-line match.
In reality, you probably want to use perl or sed to do this anyway, they have control structures in the language which allow for much finer control of the output than grep.
I know the '-C' switch tells it to print a number of lines around the match? e.g.
Code:
grep -C 2 "strategy" text.txt
and now see these also:
Quote:
Context control:
-B, --before-context=NUM print NUM lines of leading context
-A, --after-context=NUM print NUM lines of trailing context
-C, --context=NUM print NUM lines of output context
#!/bin/bash
#grab parameter
user=$1
#line number your want to blank
line=`grep -n $user your_file_name | cut -c1`
#this is line which holds password
((line+=1))
#replace line with specified user and the one below with blanks
sed -e "s/$user//;$line s/\(.*\)//" your_file_name
This will replace two lines with blank, then you might want to run a filter and remove blank lines. Run the script as:
Code:
#chmod +x this_script
#this_script Username1
The output will go on your terminal, if needed you might redirect it to another file.
I went ahead and killed the grep idea and went with the sed -i concept. Here is my code if anyone cares to look. If you see any ideas that you would like to pass along to me that would be cool as I am just a newbie at this type of script. Thanks again!
#Deletes virtual users for FTP
#11/05/07
cd /home/users/ftp
ls
if [ "$1" = "" ]
then
clear
echo " you have used this incorrectly."
echo "usage: $0 <username> is the proper format"
exit
else
#if the file exists, then it is removed.
if [[ -f /etc/vsftpd/vsftpd_users.db ]]
then
rm -f /etc/vsftpd/vsftpd_users.db
fi
#remove lines to vsftpd_users.txt
sed -i "/$1/,+1d" /etc/vsftpd/vsftpd_users.txt
db41_load -T -t hash -f /etc/vsftpd/vsftpd_users.txt /etc/vsftpd/vsftpd_users.db
chmod 600 /etc/vsftpd/vsftpd_users.db /etc/vsftpd/vsftpd_users.txt
#Remove the users directory
rm -r -f "/home/users/ftp/$1"
fi
#finished
exit 0
Last edited by krazyglue; 11-05-2007 at 03:05 PM.
Reason: wrong script
Minor thing, you can remove the file without checking for its existence, аnd redirect error to the /dev/null to avoid possible warnings. Additionaly, '-f' option kills all warning anyway:
how to login with SSH interactive bash Script
I have written Script which do interactive Login with ssh and username "web"
------------------------------------------------------------------------
#!/bin/sh
echo 'my1 :web = webpass root= rootpass'
#ssh -l web my3.domain.co.in
expect -c 'spawn ssh web@my3.domain.co.in; expect password ; send "webpass" ; interact'
------------------------------------------------------------------------
but i want to implement this script with su-
because when i run this Script I can able to login upto
[web@my1 my1 mail]$
[web@my1 my1 mail]$ su -
after this login how can i do interactive login for su- which gived me
[root@my1 my1 mail]#
[web@my1 my1 mail]$---------->from web login to [root@my1 my1 mail]#
please help me to get run script which ask for su -
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.