LinuxQuestions.org
Review your favorite Linux distribution.
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 03-03-2010, 06:52 PM   #1
tensigh
Member
 
Registered: Mar 2004
Location: Tokyo, Japan
Distribution: Backtrack 5 R3
Posts: 141

Rep: Reputation: 15
Sed qestion - how to use wildcards


I'm finally learning how to use sed and I can't find an answer to this but I'm sure it's basic.

I have a file name of student names and their homerooms. The 3rd year students just graduated so I want to change their homeroom status to 'graduated'. This is a sample:

Yuko Myoji 31
Maiko Nanikashira 31
Ema Shiranai 32
Airi Dosuru 33


If I run sed s/31/graduated/g it will change all students in 31 to graduated, but how do I do that for 3x (i.e., students in homerooms 31, 32, 33 and 34?)

I tried sed s/31/graduate/g name.txt >> updatednames.txt, then did the same thing for s/32, s/33 and s/34, but the file repeats the whole list each time, so I know I'm doing something wrong.
 
Old 03-03-2010, 07:26 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
In utilities like SED, what you call "wildcards" are part of the syntax of Regular Expressions (AKA Regexes).

In your example, you could use:
Code:
sed 's/3[0-9]/graduated/' filename
  ## This matches all occurences of "3", following by one numeral
For good tutorials on SED, Regexes, and more, go here:
http://www.grymoire.com/Unix/Sed.html
 
Old 03-04-2010, 01:22 AM   #3
tensigh
Member
 
Registered: Mar 2004
Location: Tokyo, Japan
Distribution: Backtrack 5 R3
Posts: 141

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by pixellany View Post
In utilities like SED, what you call "wildcards" are part of the syntax of Regular Expressions (AKA Regexes).

In your example, you could use:
Code:
sed 's/3[0-9]/graduated/' filename
  ## This matches all occurences of "3", following by one numeral
For good tutorials on SED, Regexes, and more, go here:
http://www.grymoire.com/Unix/Sed.html

Yeah, I knew I didn't have the lingo quite right. I still tend to think of everything I do at the command line as mere commands with switches and wildcards. I forget that grep, sed, etc are actually using expressions. Thanks for that and for the code! Big help!
 
Old 03-04-2010, 09:15 PM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,225

Rep: Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021
Actually, even the bash 'wildcards' are in fact expressions; there's more to them than you think..
 
Old 03-04-2010, 09:52 PM   #5
tensigh
Member
 
Registered: Mar 2004
Location: Tokyo, Japan
Distribution: Backtrack 5 R3
Posts: 141

Original Poster
Rep: Reputation: 15
Yes, true

Quote:
Originally Posted by chrism01 View Post
Actually, even the bash 'wildcards' are in fact expressions; there's more to them than you think..
You're right, I need to stop being so myopic. Bash is really like a mini-programming environment, anyway, right?
 
Old 03-04-2010, 09:55 PM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Quote:
Originally Posted by kuriharu View Post
You're right, I need to stop being so myopic. Bash is really like a mini-programming environment, anyway, right?
BASH is a very capable scripting language, although possibly not the easiest to learn...
 
  


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
SED script qestion Andrew_OC Linux - Desktop 5 11-25-2006 07:40 AM
news server qestion rex11 Debian 1 06-30-2006 12:38 AM
qestion about h264 to mp4 egad Linux - Software 0 04-30-2006 02:42 PM
qestion about anti-virus conner_f Linux - Security 2 05-30-2004 02:13 PM
Noob Networking Qestion Verbal Kint Linux - Networking 5 04-08-2004 03:32 PM


All times are GMT -5. The time now is 09:53 PM.

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