LinuxQuestions.org
Visit Jeremy's Blog.
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 06-20-2007, 01:43 PM   #1
rengo.Java
Member
 
Registered: Apr 2007
Location: Argentina
Distribution: Slackware
Posts: 48

Rep: Reputation: 15
Bash & sed question


Hi All!

I write a script that replace some lines following a pattern, using 'sed'.
The script works fine, but now i need to update it to replace only the last occurrence of that pattern en the file.
For example:

#a user
user= foo
name= John
password= 2312
#another user
user= tiger
name= Will
password= 5887

Im searching by the pattern '^user= '.

There is any way to replace ONLY the last occurrence of the pattern?

Thanks!
 
Old 06-20-2007, 03:36 PM   #2
lakris
Member
 
Registered: Sep 2004
Location: Stockholm, Sweden
Distribution: Ubuntu, RedHat, SuSe, Debian, Slax
Posts: 102

Rep: Reputation: 15
Hm, I'm sure that in any language You would have to loop through the occurences of ^user and save it in a list/array. Sed has that capability too. The last one received is the one You want to replace. But I couldn't help it, here's one in bash/sed that does the same thing. This is not a help with sed. But I think it illustrates what You want to do and the benefits of the *nix toolbox... I assume the file has a list of records where user is unique, and also that You separate the name with space before it, like:

lakris@ubuntu:~$ cat users.txt
#a user
user= foo
name= John
password= 2312
#another user
user= tiger
name= Will
password= 5887

lakris@ubuntu:~$ new=lion;org=`cat users.txt |grep ^user|tail -1|cut -d" " -f2`;cat users.txt|sed -e "s/^user=\ $org/user=\ $new/"
#a user
user= foo
name= John
password= 2312
#another user
user= lion
name= Will
password= 5887

lakris@ubuntu:~$

of course You could redirect it and do some file renaming and stuff to update the actual "database". lion could be Your input to a script, even the file name. Excercise left to reader...

Last edited by lakris; 06-20-2007 at 03:51 PM.
 
Old 06-20-2007, 04:44 PM   #3
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 38
Quote:
Originally Posted by rengo.Java
[...]
The script works fine, but now i need to update it to replace only the last occurrence of that pattern en the file.
For example:

#a user
user= foo
name= John
password= 2312
#another user
user= tiger
name= Will
password= 5887

Im searching by the pattern '^user= '.

There is any way to replace ONLY the last occurrence of the pattern?
Not a sed solution though:

Code:
ex - infile<<!
?^user=.*?s//your_new_entry/
x
!
 
Old 06-20-2007, 09:44 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
in awk
Code:
awk 'FNR==NR{ if ($0 ~ /^user=/ ){
                n=NR                                 
              }
              next
            }
      { 
        FNR==n ? $2="userlast": $2=$2
        print $0      
      }           
' "file"  "file"
output:
Code:
# ./test1.sh
#a user
user= foo
name= John
password= 2312
#another user
user= userlast
name= Will
password= 5887
 
Old 06-21-2007, 06:52 AM   #5
rengo.Java
Member
 
Registered: Apr 2007
Location: Argentina
Distribution: Slackware
Posts: 48

Original Poster
Rep: Reputation: 15
Thanks to AL!!!
specially to lakris, your answer is clear and works ok!

Im a begginer in the bash world and there are several commands that i dont know (i.e: tail, cut, head) where can i found a list (or a reference) of that commands and what each do?

Again, THANKS TO ALL!!!!

PD: Sorry my gad english
 
Old 06-21-2007, 07:16 AM   #6
lakris
Member
 
Registered: Sep 2004
Location: Stockholm, Sweden
Distribution: Ubuntu, RedHat, SuSe, Debian, Slax
Posts: 102

Rep: Reputation: 15
Quote:
Originally Posted by rengo.Java
Thanks to AL!!!
specially to lakris, your answer is clear and works ok!

Im a begginer in the bash world and there are several commands that i dont know (i.e: tail, cut, head) where can i found a list (or a reference) of that commands and what each do?

Again, THANKS TO ALL!!!!

PD: Sorry my gad english
Well, *gad* isn't half bad

I have found this, http://www.tldp.org/LDP/abs/html/ , to be a very good resource on bash (and the site http://www.tldp.org/ for most GNU/Linux tools). And of course, man tail, etc is a good way to start. Your distro may have additional documentation packages with HOWTO's and coding examples. A very good way to start is to look at how others solved a specific or similar problem.
I often find myself whipping up one-liners where others would turn to perl or even a higher level programming language. bash and friends are really powerful, fast and intuitive.
Happy hackin'!

/Lakris
 
Old 06-21-2007, 11:39 AM   #7
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
I recently wrote a pretty complex name parser using cat, echo, cut, tr, rev, head, tail and grep.
 
Old 06-21-2007, 03:55 PM   #8
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
whipping up one-liners where others would turn to perl
Perl is quite happy to run a script given entirely on the commandline.
Code:
perl -e 'print "hello world\n";'
--- rod.
 
Old 06-21-2007, 09:59 PM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by rengo.Java
Thanks to AL!!!
specially to lakris, your answer is clear and works ok!

Im a begginer in the bash world and there are several commands that i dont know (i.e: tail, cut, head) where can i found a list (or a reference) of that commands and what each do?

Again, THANKS TO ALL!!!!

PD: Sorry my gad english
somewhere down the road, as you progress in shell scripting, you will learn not to use cat for useless purposes such as :
Code:
cat file | grep "blah"
You will also learn how to get what you want without having to use too many pipes. have fun
 
  


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
Simple bash/awk/sed scripting question R00ts Programming 4 04-16-2005 02:55 AM
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-03-2005 11:00 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM
bash - sed question pk21 Programming 2 08-30-2003 05:57 AM
bash - sed question pk21 Programming 2 07-11-2003 03:29 AM

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

All times are GMT -5. The time now is 08:55 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