LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Sed qestion - how to use wildcards (https://www.linuxquestions.org/questions/linux-software-2/sed-qestion-how-to-use-wildcards-793012/)

tensigh 03-03-2010 06:52 PM

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.

pixellany 03-03-2010 07:26 PM

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

tensigh 03-04-2010 01:22 AM

Quote:

Originally Posted by pixellany (Post 3884790)
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!

chrism01 03-04-2010 09:15 PM

Actually, even the bash 'wildcards' are in fact expressions; there's more to them than you think..
;)

tensigh 03-04-2010 09:52 PM

Yes, true
 
Quote:

Originally Posted by chrism01 (Post 3886402)
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?

pixellany 03-04-2010 09:55 PM

Quote:

Originally Posted by kuriharu (Post 3886429)
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...


All times are GMT -5. The time now is 07:31 AM.