sed and the \t character
Posted by barnett in Shell Scripting on July 18, 2010
Dear Grymoire,
I am trying to convert a pipe character into a tab. I tried
sed y/\|/\t/ <in>out
but it did not work. Instead of a tab, I got a ‘t’. What did I do wrong?
I thought this was a good lesson, as there are several misunderstandings a beginner might have when they write a shell script.
The first lesson concerns meta-characters.
A meta-character is simply a character that has a special meaning.
The character ‘t’ is a normal character. The character ‘\’ is a special character. It has a special meaning. However, the meaning of this character changes depending on how and where it is used. You have to know the context. ‘|’ is another example. It may or may not be a meta-character.
So what was wrong with ‘\|’ and ‘\t’?
To break down this problem into steps, there are some basic concepts.
In Unix shell scripts. the shell is the interface between you and the computer. When you write a shell script, and that script calls a program like sed, there are two steps
- The shell reads the shell script.
- The shell passes the characters it sees to the program.
Let’s example the above script. How does the shell see the script? The easiest way is to insert echo as the first command on the line.
We also have to remove the <in >out so we can see what happens. So the new command is simply
echo sed y/\|/\t/
And this command echos the following to the screen:
sed y/|/t/
So the pipe character is seen by the shell and echoed. But where is the tab character?. What happened?
Normally, the shell sees the ‘|’ character as a special character. It’s a meta-character because of this special meaning. It tells the shell to start a new process, and you take the output of the previous character, and send it to the new process.
Remember that the shell has three kinds of quotations, a single quote character, a double quote character, and a backslash.
So when you quote a special character,it becomes a special kind of special character.
In other words, it’s normal. Crazy rules, but that’s how it works. Quoting special characters in the shell make them normal and ordinary – just like the letters a-z, etc.
So the ‘\|’ is correct. So is using single or double quotes. So why didn’t ‘\t’ work? Well, on some systems, sed will output either a ‘t’ or a ‘\t’ instead of a tab. What is happening?
The second lesson is this:
To understand what is a meta-character, you have to be familiar with the program that is looking at the character.
The shell sees ‘\t’ to be the same as ‘\’ followed by ‘t’, so it treats them the same way.
What does sed do with ‘\t’? Well, it depends.
Some versions of sed treat ‘t’ to be the same as ‘\t’. Others treat ‘\t’ as a tab.
The person who send me this problem was trying to get a tab character into this part of the script. Well, there are two approaches:
- Let sed convert ‘\t’ into a tab
- Let the shell convert ‘\t’ into a tab, and pass this into sed.
This causes a problem if neither the shell nor sed interpret ‘\t’ to be a tab character. There are some programs that do treat ‘\t’ is a tab character. These include
- The C programming language in a string
- The print(1) command, which is based on the C language
- perl
- awk
- The tr(1) command
- Some versions of the echo(1) command.
- … and several other computer languages.
But the shell does not. And sed may (or may not). So what’s the answer?
It’s quite simple.
If you want sed to insert a tab, you may have to include the tab character in the script.
In other words, the answer is to use the following
sed ‘y/|/ /’ <in >out
Where the character between the ‘/…/’ is a tab character. Unfortunately, I cannot show a tab character there, because it looks like zero or more spaces, depending upon the tab stops in your terminal. However, you are reading this using a browser, which cannot show tabs either.
So how do you get a tab character in the script? If you are using an editor, you can edit a file that contains the script and insert a tab character.
This seemingly simple task can be tricky. Some editors convert tabs to spaces. This can depend upon the settings in your Unix terminal.
To make sure you have a tab character in the script, use ‘od -c’ to display the file. In other words, if you had the script
#!/bin/sh sed 'y/|/ /'
and typed in od -c script, you would see
0000000 # ! / b i n / s h \n s e d ' y 0000020 / | / \t / ' \n 0000027
If this doesn’t work, some of your terminal settings may be wrong.
A second way to get it to work is to enter the script from the command line. The Control-V character tells the terminal handler to “quote” the next character. If it is converting tabs to spaces. Control-V <Tab> may let you type a real tab character.
The third way is to use another program to create the tab character. tr(1) understands ‘\t’ so let it create it for you. ONe way is to set a variable to be equal to a tab character, and then use this variable inside of double quotes:
#!/bin/sh tab=`echo a | tr a '\t'` sed "y/|/$tab/' <in >out
This format is useful if you want to publish a script on the web. That’s because the script does not contain any non-printing characters. This makes it easy to cut and paste a script. Copying a tab character, and pasting it into another window may not work.
There is one more point
Don’t change characters when you don’t need to
In other words, instead of changeing the pipe character ti a tab, just use the pipe character as the field seperator. Awk allows this
Just use
awk -F’|’ ‘{print $3}’
or whatever.
Kinect and first person shooters
Posted by barnett in Technology on July 1, 2010
I was reading about Microsoft’s Kinect. .
Kinect lets you move your body to interact with a game. No controller is needed. Sounds cool, but apparently most of the demos are really lame.
If I was designing a shooter game, this would be my concept:
Instead of using some keystroke to switch weapons, just place the weapons on your body.
- You have a bandolier with grenades.
- You have a pistol at your left hip, and another tucked in your waist.
- You have a rifle around your left shoulder, swung behind your back.
- There is a knife by your knee.
To switch a weapon, reach for it, and place it in front of you. Your hand positions will help determine what type of weapon it is. A pistol and a rifle are held differently. Move your arms, and point to your target. Holding your right hand steady will increase accuracy. You don’t want to move your hands when you shoot. So how do you fire the weapon without moving your hands? Just say “Fire!”. Or “Bang”, “Pow”, “Zap”, “Go”, “Shoot” or any other one-syllable word.
Need a grenade? Just reach up to your bandolier, and pull off a grenade. You then heave the grenade, and the effort used determines the distance.
Add some ninja stars, throwing knives, etc. And you can always use your fists.
Oh – it would help to have a practice range.
Seems more realistic, doesn’t it?
Is Google.cn evil?
Posted by barnett in Technology on August 23, 2009
So many people seem confused about Google and China. When Google created google.cn, was that an act of evil?
And when they switched to google.hk, what is the impact on the people of China?
It’s quite simple. China has a firewall. Not the firewalls we see in businesses or homes, but a huge friggin’ firewall that isolates the entire country. That’s a billion users, and one big firewall that separates them from the rest of the world.
If you are in China, and search “tank man”, you won’t see any photos of him standing in front of a tank. You won’t find the Wikipedia entry . You get nothing. He does not exist in China. That’s what the Great Firewall of China does.
So what happens if you are in China, and you go to Google.hk, and search for “tank man”? Google responds, and points to those links. Ever helpful, that Google. And when the person inside of China tries to click on those links, what happens? “No such server.”
But what happens if Google moves their server inside of the Chinese Firewall? Suddenly the whole world has a happy face, and all porn disappears. So does Tank Man, and Falun Gong, The Dali Lama, Democracy, Marxism, Freedom of speech, Tienanmen Square Protests of 1989, mention of policy brutality, and anything that the Chinese Government views as negative. And Google.cn won’t find these references, because it can’t see them either.
You can see how it would make browsing inside of China more pleasant and convenient for the “proper” citizen of China. who has no desire to go to such sites. It does improve the experience. The customers are happier, because the search result is more accurate (from their perspective). And if you make 1 billion customers more productive, and happier, it’s a good business decision. So Google decided to place a search engine inside of the Firewall. Was that really evil? Well, I can see the business perspective that it does improve the search experience. And it won’t point to unreachable sites. That’s very good for the users.
Yes, it is working “with” the Chinese Government. And I’m sure there were other dealings going on as well. But for the Chinese masses, it was not an act of evil, but of convenience. And the “subversive” citizen can (and will) use google.hk to do unfiltered searches. They could before google.cn. They can now.
Many Chinese citizens dislike the firewall. but Google can’t really change that. There are legitimate sites that get blocked with the “evil” sites. There is no brush so fine that it can separate good from evil. So many citizens of China find ways around the firewall, using software like Tor.
So when Google.cn directed people to Google.hk, they basically said “Here is a search engine that is unfiltered. Use it, and you may discover that your government does not want you to see some sites. We can’t show them to you, but we can let you know they exist. China doesn’t like it because it’s a big arrow that tells people where to go to see what is blocked. It effectively says “unfiltered search – click here.” So it does increase the visibility of this option. It’s teaching the good people in China about the wall that surrounds the country. It’s letting the good people get a peek at the ugly trust.
While I don’t think putting the search engine inside the firewall was an Extreme Evil Act, as it does offer a convenience for 99% of the citizens of the country, I do like the little “tweak” Google did by redirecting google.cn to google.hk.