Summary: The Scripting Wife learns how to use Windows PowerShell to get specific lines from a text file.
Microsoft Scripting Guy, Ed Wilson, here. As the Windows PowerShell 2011 Scripting Games edge forward in the conscious minds of intrepid scripters from around the world, the event continues to grow in size, complexity, and importance in my own mind. Each day brings new opportunities as more people become aware of the event.
I have been asked to do a TechNet Radio podcast—to which I heartily agreed with one stipulation: I wanted it to air on Monday April 4, 2011 because that is the day the 2011 Scripting Games begin. Therefore, the TechNet Radio pod cast will be the opening ceremony for the Games. They actually had to move things around a little bit, but it was an opportunity too good to pass up.
In the meantime, the Energized Tech, former Friday Funny Guy, and Windows PowerShell MVP, Sean Kearney, is busy at work on his own brand of opening ceremonies for the 2011 Scripting Games. When the community becomes involved, great things happen. Interestingly enough, I was speaking at a SQL Saturday event in Orlando, Florida last year, and SQL guru Aaron Nelson (aka SQL Variant) said that one of the strengths of Windows PowerShell is the community because it promotes learning—people are willing to share, and they love to help others be successful using the technology.
Speaking of the 2011 Scripting Games—here comes the Scripting Wife with her laptop. I suspect that she is still studying for the Games and wants some assistance.
“Good morning Scripting Wife,” I said.
“What is so good about it?” she replied.
“Well, for one thing, I have been notified that they want me to do a TechNet Radio podcast. I think they want you too,” I said.
“I see. And I agreed to do that when?”
“Hmmm. I uh…thought you would be happy…”
“I didn’t say I wasn’t happy…but it will cost you. When you pay up, I will be really happy. I think I see dinner and a movie in my future,” she said.
“No problem,” I replied while thinking I was getting off light.
“For my friends and me,” she added, “Yep, a girl’s night out will just about pay for my appearance.”
“Indeed it will,” I relented. “By the way, how many friends are you planning to take on this little soiree?”
“Oh, I don’t know. Probably no more than eight—I am not sure.”
“So what brings you to my office this morning?” I said, anxious to change the subject before she added snacks to the movie excursion.
“Not so fast, you know we will need snacks at the movie.”
“Uh…yeah…of course,” I relented.
“Now, I need you to tell me about processing text files.”
“What do you mean?” I asked.
“I use text files to store my friends’ addresses and phone numbers,” she started to explain.
“You are kidding,” I said inadvertently interrupting her.
“No, I am the Scripting Wife, but that is not important right now, so quit interrupting. I like text files because they are easy to work with, are extremely portable, and do not take up much room. I sync them on my Windows 7 phone, and I always have them. So there.”
“OK. Great idea. Sorry I scoffed. I have just never heard of that before,” I began.
“Well you have not heard of everything, now have you?” she chastised. “Here, let me show you.”
The Scripting Wife opened a directory on her laptop. It was called MyFriends, and it is shown here.
“What do these files look like?” I asked.
The Scripting Wife smiled and said, “Well, take a look.”
The Scripting Wife opened the contact for her friend Lori. It is shown here.
“I want to know how to work with this file,” she said.
“Do they all look like this one?” I asked with mounting interest.
“No silly. They have different information in them. This one has Lori’s information. Lisa’s has information for Lisa, and…”
“No. I did not mean that. I mean, are all of the files structured the same. Because if they have the same number of lines and the information is in the same order, it is relatively easy to work with them. Open Windows PowerShell, and I will show you,” I said.
The Scripting Wife opened the Windows PowerShell console.
“Now what?” she asked.
“Use the Get-Content cmdlet to read and to display the content of Lori’s file,” I said.
The Scripting Wife thought for a minute, and then typed Get-Content and the path to her file. Here is the exact command she typed.
Get-Content -Path C:\MyFriends\Lori.txt
The command and its associated output are shown in the following image.
“Now, suppose you only want to retrieve the name from the file. Assuming that the name is on the first line, you can pipe returned information to the Select-Object cmdlet and use the first parameter to retrieve one line from the file. Why don’t you go ahead and try it?” I suggested.
The Scripting Wife thought for a minute, and then typed the following command.
Get-Content -Path C:\MyFriends\Lori.txt | Select-Object -First 1
The command and its associated output are shown here.
“Now suppose you want to retrieve the last item from the text file. To do this use the last parameter and tell it how many items to retrieve,” I said.
The Scripting Wife thought for a second, and typed the following command:
Get-Content -Path C:\MyFriends\Lori.txt | Select-Object -Last 1
The command and its associated output are shown here.
“What happens if you choose the last two items from your file?” I asked.
“I imagine, it retrieves the last two lines,” she said.
The Scripting Wife changed the last parameter value from 1 to 2. Her revised command and output are shown here.
PS C:\> Get-Content -Path C:\MyFriends\Lori.txt | Select-Object -Last 2
Twitter: LoriK
Facebook: Lori.Kane
“Does this work with the first parameter?” she asked.
“Go ahead and try it,” I said.
She used the Up arrow to retrieve the appropriate command, and changed it to retrieve the first two lines from the text. Her revised command and its associated output are shown here.
PS C:\> Get-Content -Path C:\MyFriends\Lori.txt | Select-Object -First 2
Lori Kane
423 East 2nd Street
“That is pretty cool. But suppose I need to get both Lori’s name and her phone number,” she asked.
“Let’s see. Her name is on the first line, and her phone number is on the fourth line. You can use the index parameter to get one or both of these lines. Go ahead and try it,” I said.
She thought for a few seconds, retrieved her previous command, and edited it until it looked like the one shown here.
Get-Content -Path C:\MyFriends\Lori.txt | Select-Object -Index 1,4
“Well that is stupid,” she said.
“What’s wrong?” I asked.
She showed me the following output.
PS C:\> Get-Content -Path C:\MyFriends\Lori.txt | Select-Object -Index 1,4
423 East 2nd Street
LoriK@hotmail.com
“The index parameter is set up as an array. Therefore it is zero based,” I said.
“Huh? What is that in human talk?” she said.
“It means that the array begins numbering at 0. So when you selected 1 and 4, you actually got items 2 and 5 from the array. You need to subtract one from each number,” I said. “Try it again.”
The Scripting Wife used the Up arrow, and edited her previous command. The resulting command and its output are shown here.
PS C:\> Get-Content -Path C:\MyFriends\Lori.txt | Select-Object -Index 0,3
Lori Kane
555-123-4567
“That is pretty neat. Well, I am outta here. See you later,” she said with a flourish.
And with that she was gone.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy
0 comments