sed - using variable values as patterns
This is purely a learning exercise.
I have no real-world application. I have a file of names in the format FirstName LastName. I want to exchange the two fields to produce a new file containing the same information in the format LastName FirstName. This works ... Code:
sed 's/\([[:graph:]]*\) \([[:graph:]]*\)/\2 \1/' Code:
Word=' \([[:graph:]]*\) ' Quote:
Daniel B. Martin |
Quote:
Code:
sed 's/'$Word$Word'/\2 \1/' |
Quote:
Code:
Word='\([[:graph:]]*\)' Daniel B. Martin |
How about just use double quotes:
Code:
sed "s/$Word$Word/\2 \1/" |
Whoa! I took a closer look at the suggested solutions and
they don't work (unlikely), or I've implemented them incorrectly. Here's some simple code. Try it yourself and tell me how I goofed it! Code:
echo; echo The input file is... Code:
The input file is... |
Hi.
5th example should look like this Code:
$ Word='\([[:graph:]]*\)' Code:
$ echo a b | sed "s/$Word$Word/[\2] {\1}/" |
You might find the following program helpful:
Code:
|
Quote:
Daniel B. Martin |
All times are GMT -5. The time now is 12:41 AM. |