Script to read a CSV file and delete particular rows

I have got a csv file (records .csv) which has 400,000 records without any header and each row in it looks like below format:

"1","222","",3333","666",777",""
"2","234","","345","234","456",""

Here first column that is “1” and “2” in above example is unique number (lets call it URN ) defining each row

Task: i got a list of 1000 urn numbers in a txt file (urn.txt) and I have been asked to edit above csv file(records .csv) such that to keep only these thousand records and delete all other rows..

Urn.txt files have one column specifying unique number (urn) and looks like below:

1
13
16

Manually it is taking time, is it possible to do it from poweshell script?

I know the logic that i need my script to first read csv file then break the row into object using comma delimiter, than i need to read txt file and i need to use loop to read each row from txt file and then find that number in csv file first column and if it is there than just copy whole row into new csv file.

I am not a programmer and i have no skill set of powershell so i am finding it difficult to write this into script. Can anyone help me

New contributor

user27393232 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

2

While it is normally preferable to perform OO processing, via ConvertFrom-Csv, in your case plain-text processing enables a faster solution, which matters with large input files.

# Create a hash set for all URNs.
$hs = 
  [System.Collections.Generic.HashSet[string]] (Get-Content -ReadCount 0 urn.txt)

# Read all lines lazily from the CSV file and pass only those
# lines through that are in the hash set.
[IO.File]::ReadLines((Convert-Path records.csv)) | 
  ForEach-Object {
    if ($hs.Contains(($_ -split ',')[0].Trim('"'))) {
      $_
    }
  }

The above directly outputs the results.

  • To output to a file, append | Set-Content ..., e.g. | Set-Content filtered.csv; use an -Encoding argument to control the character encoding.

  • If you want to write back to the input file (be sure to make a backup copy first), use [IO.File]::ReadAllLines() rather than [IO.File]::ReadLines() or (Get-Content -ReadCount 0 records.csv) (note the parentheses), but note that doing so reads the entire file into an array of lines into memory at once.
    The alternative is to write to a temporary output file first, and then replace the original file with the temporary one.

Note:

  • [IO.File]::ReadLines((Convert-Path records.csv)) is used purely for performance reasons, because the PowerShell-idiomatic equivalent, Get-Content records.csv, is regrettably slow – see GitHub issue #7537 for a discussion and a possible future remedy.

  • The assumption is that none of the rows in your CSV file span more than one line.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật