LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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

Reply
 
Search this Thread
Old 04-13-2006, 09:45 PM   #1
paul_mat
Member
 
Registered: Nov 2004
Location: Townsville, Australia
Distribution: Fedora Core 5, CentOS 4, RHEL 4
Posts: 855

Rep: Reputation: 30
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 09:49 PM.
 
Old 04-13-2006, 11:11 PM   #2
allelopath
Member
 
Registered: Jan 2003
Location: New Mexico
Distribution: SuSE 9.2 Professional, OpenSuSE 10.1 64 bit, Open SuSE 10.2, Debian Etch
Posts: 535

Rep: Reputation: 30
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
 
Old 04-13-2006, 11:40 PM   #3
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
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
 
Old 04-14-2006, 10:17 PM   #4
paul_mat
Member
 
Registered: Nov 2004
Location: Townsville, Australia
Distribution: Fedora Core 5, CentOS 4, RHEL 4
Posts: 855

Original Poster
Rep: Reputation: 30
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
 
Old 04-14-2006, 10:24 PM   #5
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
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 ( " )
 
Old 04-14-2006, 10:48 PM   #6
paul_mat
Member
 
Registered: Nov 2004
Location: Townsville, Australia
Distribution: Fedora Core 5, CentOS 4, RHEL 4
Posts: 855

Original Poster
Rep: Reputation: 30
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
 
Old 04-14-2006, 11:07 PM   #7
paul_mat
Member
 
Registered: Nov 2004
Location: Townsville, Australia
Distribution: Fedora Core 5, CentOS 4, RHEL 4
Posts: 855

Original Poster
Rep: Reputation: 30
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?
 
Old 04-14-2006, 11:38 PM   #8
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
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
 
Old 04-15-2006, 12:26 AM   #9
paul_mat
Member
 
Registered: Nov 2004
Location: Townsville, Australia
Distribution: Fedora Core 5, CentOS 4, RHEL 4
Posts: 855

Original Poster
Rep: Reputation: 30
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'
 
Old 04-15-2006, 07:50 AM   #10
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
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
 
Old 04-15-2006, 10:23 AM   #11
paul_mat
Member
 
Registered: Nov 2004
Location: Townsville, Australia
Distribution: Fedora Core 5, CentOS 4, RHEL 4
Posts: 855

Original Poster
Rep: Reputation: 30
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?
 
Old 04-15-2006, 10:30 AM   #12
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
Quote:
$B or $pam_info?
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.
 
Old 04-16-2006, 01:59 AM   #13
paul_mat
Member
 
Registered: Nov 2004
Location: Townsville, Australia
Distribution: Fedora Core 5, CentOS 4, RHEL 4
Posts: 855

Original Poster
Rep: Reputation: 30
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Does anyone know of a bash script can search & replace txt in a file. jimwelc Linux - Newbie 6 09-15-2008 12:13 AM
tcsh script, replace word in file true_atlantis Programming 10 02-17-2006 01:24 PM
search function (bash script) LYK Programming 2 05-27-2004 10:51 AM
How to read ans parse MS word file using a Linux Shell script. Alek Linux - General 2 11-10-2003 02:07 PM
Search for a file using shell script sharathkv Linux - Newbie 2 08-06-2003 05:05 AM


All times are GMT -5. The time now is 10:33 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration