How to Extract Employee Names and Salaries from a Text File?

0
4
Asked By CuriousCat93 On

I'm working with a file named emp.txt which contains employee details like ID, name, age, date of joining, department, and salary. I want to extract just the employee names and their corresponding salaries from this file. Here's the content of the emp.txt file:

Empid Empname Age DOJ Dept Salary
101 Sam 45 22-Nov-2008 IT 50000
102 Ram 38 14-Jan-2010 Accounts 40000
103 Tim 46 15-June-2005 IT 60000
104 Kim 25 03-Sep-2020 IT 50000
105 Jane 27 27-Oct-2018 Purchase 30000
106 Dane 36 07-Feb-2014 Accounts 35000
107 Raj 50 17-Mar-2005 Accounts 50000
108 Kiran 28 15-June-2015 IT 55000
109 Varun 40 10-Jul-2011 Purchase 40000
110 Tarun 47 14-Aug-2007 Purchase 50000

Can someone help me with a solution to get just the names and salaries?

2 Answers

Answered By TechyGuru21 On

It looks like you need to show what you've tried first. Take this as a learning opportunity! Try using Get-Content along with a foreach loop to go through each line of the file. You might need to split the strings to get the relevant data. It's more rewarding to figure it out yourself!

Answered By CodeCracker77 On

You really should try to solve this on your own! But if you're looking for a snippet, you can use `Import-Csv` with a delimiter to read the file. Here's a quick command: `Import-Csv -Path "emp.txt" -Delimiter " " | Select-Object Empname, Salary`. Remember, doing it yourself will help you learn more!

CuriousCat93 -

I tried that but I'm getting an error saying employee name and salary aren't being retrieved properly. Any tips?

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.