LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-02-2007, 02:54 PM   #1
krazyglue
Member
 
Registered: Oct 2003
Posts: 68

Rep: Reputation: 16
script writing help (grep -r)


Howdy,

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).


The file will always be in the following format

Username1
password
Username2
password
Username3
password
...
username 10
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?
 
Old 11-02-2007, 03:02 PM   #2
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 48
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.
 
Old 11-02-2007, 03:07 PM   #3
brazilnut
Member
 
Registered: Nov 2007
Posts: 113

Rep: Reputation: 16
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
then try invert... since i've not!
 
Old 11-02-2007, 07:59 PM   #4
rsashok
Member
 
Registered: Nov 2006
Location: USA, CA
Distribution: RedHat, Debian
Posts: 202

Rep: Reputation: 31
Try this:
Code:
#!/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.

Last edited by rsashok; 11-02-2007 at 08:04 PM.
 
Old 11-02-2007, 09:04 PM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
u="Username2"
awk -v usr2del=$u '$0==usr2del{ getline;next}1'  "file"
output:
Code:
# ./test.sh
Username1
password
Username3
password

Last edited by ghostdog74; 11-02-2007 at 09:12 PM.
 
Old 11-02-2007, 11:03 PM   #6
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
Another sed solution
Code:
sed '/Username2/,+1d' file
if you want to match multiple user names you could do:
Code:
sed -r '/(Username2|Username3)/,+1d' file
If you don't have the -r option, you'll have to backslash the ()| chars:
Code:
sed '/\(Username2\|Username3\)/,+1d' file
-i option will edit the file in place, but be careful with it so you don't accidentally nuke your data.

Last edited by angrybanana; 11-02-2007 at 11:21 PM.
 
Old 11-05-2007, 03:04 PM   #7
krazyglue
Member
 
Registered: Oct 2003
Posts: 68

Original Poster
Rep: Reputation: 16
Thanks!

Thanks for all the help!

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
 
Old 11-05-2007, 04:05 PM   #8
rsashok
Member
 
Registered: Nov 2006
Location: USA, CA
Distribution: RedHat, Debian
Posts: 202

Rep: Reputation: 31
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:
Code:
rm -f $file > /dev/null 2>&1
 
Old 11-21-2007, 03:20 AM   #9
makefunnow
LQ Newbie
 
Registered: Oct 2007
Posts: 1

Rep: Reputation: 0
Wink how to login with SSH interactive bash Script

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 -



Please Email me to makefunfun@gmail.com
 
Old 11-21-2007, 10:16 AM   #10
rsashok
Member
 
Registered: Nov 2006
Location: USA, CA
Distribution: RedHat, Debian
Posts: 202

Rep: Reputation: 31
makefunnow,

I'd suggest that you put your question in a new thread, rather then appending to existing one with no relevance to your problem.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Find and Grep in Script - almost there chess Programming 8 05-01-2007 09:15 AM
grep script kaiserbeto Linux - Newbie 2 11-15-2006 01:50 PM
grep script/syntax carriehoff Linux - Newbie 6 08-31-2006 01:13 PM
Shell script with grep MicahCarrick Programming 4 08-15-2006 01:08 PM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration