Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum. |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
04-13-2006, 10:45 PM
|
#1
|
Member
Registered: Nov 2004
Location: Townsville, Australia
Distribution: Fedora Core 5, CentOS 4, RHEL 4
Posts: 855
Rep:
|
search a file for a word - bash script
hi there,
i'm looking for a command that will search in a file for the first instance of a word and then place another word on top of that word.
for instance, search this file for the word dog and then put cat on top
fish
horse
cow
bull
dog
dog
dog
dog
turns into
fish
horse
cow
bull
cat
dog
dog
dog
dog
Last edited by paul_mat; 04-13-2006 at 10:49 PM.
|
|
|
04-14-2006, 12:11 AM
|
#2
|
Member
Registered: Jan 2003
Location: New Mexico
Distribution: Ubuntu 18.04.3 LTS
Posts: 539
Rep:
|
I don't know how to do this.
This might get you going.
This command will find all instances of "dog" in files:
cat * | grep dog
|
|
|
04-14-2006, 12:40 AM
|
#3
|
Senior Member
Registered: Oct 2003
Posts: 3,057
Rep:
|
Using -m1 with grep tells it to stop looking after the first match. So, this might work for you....
Code:
#!/bin/bash
a=$(grep -m1 -n "dog" < file.txt | cut -d: -f1)
sed -i ''$a's/dog/cat/' file.txt
|
|
|
04-14-2006, 11:17 PM
|
#4
|
Member
Registered: Nov 2004
Location: Townsville, Australia
Distribution: Fedora Core 5, CentOS 4, RHEL 4
Posts: 855
Original Poster
Rep:
|
thanks homey that looks just like want i want, but i need to be able to use multiable lines. this is what i want to do.
a=$(grep -m1 -n "#Recommended minimum configuration:" < $A | cut -d: -f1)
sed -i ''$a's/#Recommended minimum configuration:/
#Recommended minimum configuration:
#Added by Server Setup Script
auth_param basic program
/usr/lib/squid/pam_auth
acl pam proxy_auth REQUIRED
http_access allow pam
#Added by Server Setup Script/' $A
but it wont let me, it gives me this error message
sed: -e expression #1, char 42: unterminated `s' command
|
|
|
04-14-2006, 11:24 PM
|
#5
|
Senior Member
Registered: Oct 2003
Posts: 3,057
Rep:
|
I looks like you have special characters in there. If so, they need to have a \ ( backslash ) in front.
For example:
Code:
sed -i ''$a's/\#Recommended minimum configuration\:/'
Also, note: ''$a is two single quotes ( ' ) and not a double quote ( " )
|
|
|
04-14-2006, 11:48 PM
|
#6
|
Member
Registered: Nov 2004
Location: Townsville, Australia
Distribution: Fedora Core 5, CentOS 4, RHEL 4
Posts: 855
Original Poster
Rep:
|
i figured it out
a=$(grep -m1 -n "#Recommended minimum configuration:" < $A | cut -d: -f1)
sed -i ''$a's/#Recommended minimum configuration:/\
#Recommended minimum configuration:\
#Added by Server Setup Script\
auth_param basic program\
$B\
acl pam proxy_auth REQUIRED\
http_access allow pam\
#Added by Server Setup Script/' $A
|
|
|
04-15-2006, 12:07 AM
|
#7
|
Member
Registered: Nov 2004
Location: Townsville, Australia
Distribution: Fedora Core 5, CentOS 4, RHEL 4
Posts: 855
Original Poster
Rep:
|
well i've kinda got it working ... this is my script so far
/usr/bin/clear
echo "
***********************************************************************************
**************************Setup squid.conf File************************************
***********************************************************************************"
echo ""
A=/etc/squid/squid.conf
echo "Enter the path to the squid.conf file:"
printf "default path is = $A: "
read A
if [ "$A" = "" ]; then
A=/etc/squid/squid.conf
fi
echo ""
B=/usr/lib/squid/pam_auth
echo "Enter the path to the squid.conf file:"
printf "default path is = $B: "
read B
if [ "$B" = "" ]; then
B=/usr/lib/squid/pam_auth
fi
echo ""
a=$(grep -m1 -n "#Recommended minimum configuration:" < $A | cut -d: -f1)
sed -i ''$a's/#Recommended minimum configuration:/\
#Recommended minimum configuration:\
#Added by Server Setup Script\
auth_param basic program\
$B\
acl pam proxy_auth REQUIRED\
http_access allow pam\
#Added by Server Setup Script/' $A
and the problem now is in my /etc/squid/squid.conf file this comes out
#Added by Server Setup Script
auth_param basic program
$B
acl pam proxy_auth REQUIRED
http_access allow pam
#Added by Server Setup Script
it doesn't seam to output the variable, can you tell me how to do that?
|
|
|
04-15-2006, 12:38 AM
|
#8
|
Senior Member
Registered: Oct 2003
Posts: 3,057
Rep:
|
I think I would do something like this...
Code:
echo "Enter the path to the squid.conf file:"
read -p "default path is /etc/squid/squid.conf : " info
if [ "$info" = "" ]; then
A=/etc/squid/squid.conf
#echo A=$A
else
A=$info
#echo A=$A
fi
|
|
|
04-15-2006, 01:26 AM
|
#9
|
Member
Registered: Nov 2004
Location: Townsville, Australia
Distribution: Fedora Core 5, CentOS 4, RHEL 4
Posts: 855
Original Poster
Rep:
|
um, maybe i miss said what i was wanting ... the $A variable is fine, it's the $B variable that ends up in the squid.conf as '$B' instead of '/usr/lib/squid/pam_auth'
|
|
|
04-15-2006, 08:50 AM
|
#10
|
Senior Member
Registered: Oct 2003
Posts: 3,057
Rep:
|
Same idea for B
Code:
echo "Enter the path to the pam_auth file:"
read -p "default path is /usr/lib/squid/pam_auth : " pam_info
if [ "$pam_info" = "" ]; then
B=/usr/lib/squid/pam_auth
#echo B=$B
else
B=$pam_info
#echo B=$B
fi
|
|
|
04-15-2006, 11:23 AM
|
#11
|
Member
Registered: Nov 2004
Location: Townsville, Australia
Distribution: Fedora Core 5, CentOS 4, RHEL 4
Posts: 855
Original Poster
Rep:
|
i'm sorry but i still don't understand, what do i put in this part of the script
a=$(grep -m1 -n "#Recommended minimum configuration:" < $A | cut -d: -f1)
sed -i ''$a's/#Recommended minimum configuration:/\
#Recommended minimum configuration:\
#Added by Server Setup Script\
auth_param basic program\
$B\
acl pam proxy_auth REQUIRED\
http_access allow pam\
#Added by Server Setup Script/' $A
$B or $pam_info?
|
|
|
04-15-2006, 11:30 AM
|
#12
|
Senior Member
Registered: Oct 2003
Posts: 3,057
Rep:
|
Use $B which will be the default /usr/lib/squid/pam_auth if the user presses enter at the prompt....
echo "Enter the path to the pam_auth file:"
read -p "default path is /usr/lib/squid/pam_auth : " pam_info
If the user types in another path then, $B will have that value.
|
|
|
04-16-2006, 02:59 AM
|
#13
|
Member
Registered: Nov 2004
Location: Townsville, Australia
Distribution: Fedora Core 5, CentOS 4, RHEL 4
Posts: 855
Original Poster
Rep:
|
i've changed the script to this
A=/etc/squid/squid.conf
echo "Enter the path to the squid.conf file:"
printf "default path is = $A: "
read A
if [ "$A" = "" ]; then
A=/etc/squid/squid.conf
fi
echo ""
echo "Enter the path to the pam_auth file:"
read -p "default path is /usr/lib/squid/pam_auth : " pam_info
if [ "$pam_info" = "" ]; then
B=/usr/lib/squid/pam_auth
#echo B=$B
else
B=$pam_info
#echo B=$B
fi
a=$(grep -m1 -n "#Recommended minimum configuration:" < $A | cut -d: -f1)
sed -i ''$a's/#Recommended minimum configuration:/\
#Recommended minimum configuration:\
#Added by Server Setup Script\
auth_param basic program\
$B\
acl pam proxy_auth REQUIRED\
http_access allow pam\
#Added by Server Setup Script/' $A
and the /etc/squid/squid.conf file still looks like this ...
#Added by Server Setup Script
auth_param basic program
$B
acl pam proxy_auth REQUIRED
http_access allow pam
#Added by Server Setup Script
|
|
|
All times are GMT -5. The time now is 07:54 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|