Извлечение строки с помощью sed

Мне было просто интересно, как я извлек определенного ряда с помощью sed? (Моя система установлена, поэтому, к сожалению, не :( awk ). Я получил текстовый файл, подобный следующему:

Business Name,Description,Address,Postal Code,Email,Phone,Fax,Web Site,City,Latitude,Longitude,Keywords,Mailing Address,Indigenous Ownership,Region,Type,Industry Sector,Year Formed,Number of Employees,Primary Contact,Contact Title,Twitter,Facebook,When Updated
Duz Cho Construction LP,"Duz Cho Construction is an earthworks construction company doing site development, road access, and reclamation for the oil and gas, wind energy, mining and other civil works construction within the traditional territory of McLeod Lake Indian Band.",4821 South Access Road,V0C 1J0,albertson@dcclp.com,250-788-3120,250-788-3188,https://duzcho.com/duz-cho-construction/,Chetwynd,55.6963381,-121.6266684,Duz Cho Construction LP  23  Construction,"4821 South Access Road, Chetwynd, BC V0C 1J0",TRUE,Northeast,Community Owned Company,23  Construction,2002,55 to 99,Jacob Albertson,General Manager,,https://www.facebook.com/DuzChoConstruction/,2012-04-15
Gilwa Consulting Inc.,"Gilwa Consulting Inc. (GCI) is an Indigenous owned and operated business that provides advisory services for all participants in the construction industry engaged in developing, implementing, and/or executing on contractual agreements between Indigenous peoples, industry (EPC, EPCM, Contractors), and/or government.",710 Ruston Road,V1X3Y2,gilwaconsultingoffice@gmail.com,778-215-1888,,http://www.gilwaconsulting.com,Kelowna,49.8804877,-119.4070893,"Gilwa Consulting Inc. 54  Professional, scientific and technical services","710 Ruston Road, Kelowna, BC V1X 3Y2",TRUE,Thompson / Okanagan,Private Company,"54  Professional, scientific and technical services",2019,1 to 4,Thomas Nyce,Director,,,2021-04-14
HSI  Hobbs Strategic Insights,"Offering a full suite of strategic planning, business consultation, corporate communications and success & inclusion coaching services, HSI works with you to manifest transformational initiatives from conception to implementation. HSI is a leader in Indigenous reconciliation work, and diversity & inclusion initiatives for corporate entities.",,V3M 0J4,info@strategicinsights.ca,604-626-8296,,https://strategicinsights.ca,,,,"HSI  Hobbs Strategic Insights 54  Professional, scientific and technical services",,TRUE,Lower Mainland / Southwest,Private Company,"54  Professional, scientific and technical services",2021,1 to 4,Sasha Hobbs,Principal Strategist,,,2021-04-14
Bigfoot Donuts,"Bigfoot Donuts is a locally owned and operated bakery and caf in the heart of Downtown Courtenay. Specializing in handcrafted, made from scratch, quality, coffee and donuts, with a side of fun.",463 A 5th Street,V9N 1J7,yum@bigfootdonuts.com,250-871-6603,,https://www.bigfootdonuts.com/,Courtenay,49.6902502,-125.0032691,Bigfoot Donuts 72  Accommodation and food services,"463 A 5th Street, Courtenay BC V9N 1J7",TRUE,Vancouver Island and Coast,Private Company,72  Accommodation and food services,2017,5 to 9,Lyndsey Bell,Owner,,https://www.facebook.com/Bigfoot.Donuts/,2021-04-14
Refocus Communications & Fundraising,"Refocus helps non-profit organizations more deeply engage their community and supporters so that, together, they can do great things in the world.
We work with charities across Canada, helping them build fundraising programs with an engagement and communications focus.","20543 96 Ave., Unit 31",V1M 3W3,aimee@refocuscommunications.com,778.875.3439,,http://www.refocuscommunications.com,Langley ,49.1776159,-122.6564416,Refocus Communications & Fundraising 81  Other services (except public administration),"20543 96 Ave., Unit 31, Langley, BC V1M 3W3",TRUE,Lower Mainland / Southwest,Private Company,81  Other services (except public administration),2017,1 to 4,Aime Lindenberger,"CEO, Chief Engagement Officer",,,2021-03-31
Turtle Island Connections,"Turtle Island Connections curates Subscription boxes filled with Indigenous creations. Our box, the Turtle Shell, focuses on our spiritual, mental, emotional, and physical well-being. Each Shell contains Indigenous art, Cultural teachings, Nourishment, Self-care products and Donation to the Vancouver Aboriginal Health Society.",3139 Brandt Crescent,V9L 6W6,info@turtleislandconnections.ca,250.710.7685,,https://www.turtleislandconnections.ca,Duncan,48.7633914,-123.7137629,Turtle Island Connections 44-45  Retail trade,"3139 Brandt Crescent, Duncan BC V9L 6W6",TRUE,Vancouver Island and Coast,Partnership,44-45  Retail trade,2021,1 to 4,Lindsay Myers,Co-Founder,,https://www.facebook.com/TurtleIslandConnections,2021-03-31
Bulkley Valley Roofing Ltd,"Specializing in Commercial, Industrial and Residential Roofing. Fully certified and insured for hot roofing work. Red-seal Journeymen staffed.","Unit 3-3167 Tatlow rd, ",V0J 2N0,bvroof2@gmail.com,250-846-9446,250-846-9440,,Smithers,54.7698334,-127.143943,Bulkley Valley Roofing Ltd 23  Construction,"Unit 3-3167 Tatlow rd, Smithers BC V0J 2N0",TRUE,North Coast,Development Corporation,23  Construction,2000,5 to 9,Murray Olivier,Owner/operator,,,2021-03-31
North Coast Helicopters,Northcoast Helicopters provides experienced long-line pilots who are seasoned in diamond drill support and mountain flying. Northcoast Helicopters provides wildfire fighting services to various private and government agencies.,2555 Seal Cove Road unit 123,V8J 3P6,craig@northcoasthelicopters.com,250-631-6031,,http://www.northcoasthelicopters.com,Prince Rupert,54.3369741,-130.3125632,"North Coast Helicopters 21  Mining, quarrying, and oil and gas extraction","2555 Seal Cove Road unit 123, Prince Rupert BC V8J 3P6",TRUE,North Coast,Private Company,"21  Mining, quarrying, and oil and gas extraction",2005,5 to 9,Kristen,Owner,,,2021-03-31
Park West Professional Services,Call Center,"Suite 811, 100 Park Royal, Suite 200",V7T 1A2,matt@mattvickers.com,604-210-1483,,,West Vancouver,49.3251879,-123.1358334,"Park West Professional Services 54  Professional, scientific and technical services","100 Park Royal, Suite 200, West Vancouver, BC V7T 1A2",TRUE,Lower Mainland / Southwest,Private Company,"54  Professional, scientific and technical services",2013,10 to 19,Matt Vickers,Owner,,,2021-03-31
Afterglow Hair Lounge,"Get your glow on. We strive to provide our guests the same quality one-on-one service small towns are renowned for, with the style, talent and professionalism of the city. We're proud to have been nominated and featured as some of the ""Best of Powell River"" stylists in 2017, 2018, 2019 and 2020 and have a shared dedication to maintain improve client experiences in years to come.  Look no further for the best wedding stylists of the upper Sunshine Coast  available year-round for events in-salon*, seven days a week. Come experience AfterGlow.",7075 Alberni Street,V8A2C2,info@afterglowhairlounge.com,604.485.2075,, http://www.afterglowhairlounge.com,Powell River,49.8468725,-124.5196439,Afterglow Hair Lounge 81  Other services (except public administration),,TRUE,Vancouver Island and Coast,Private Company,81  Other services (except public administration),2011,5 to 9,Holly Barnes,Owner/operator,https://twitter.com/afterglowhair,https://www.facebook.com/AfterGlowHairLounge,2021-03-10
Dawson Analytics Inc.,"Dawson Analytics is a consulting company specializing in research development and design, data analysis and interpretation, and program/policy evaluation. Our company sources and analyzes data to provide our clients with powerful insights into their processes, outcomes and future endeavours.",22839 Gilbert Drive,V4R 0G4,DawsonAnalytics@outlook.com,604.961.5129,,http://www.dawsonanalytics.com,Maple Ridge,49.2512469,-122.5874728,Dawson Analytics Inc.  51  Information and cultural industries,"22839 Gilbert Drive, Maple Ridge, BC V4R 0G4",TRUE,Lower Mainland / Southwest,Private Company,51  Information and cultural industries,2021,1 to 4,Stephanie Johnston,Vice President,,,2021-03-10

Так что в основном я хочу что-то вроде того, что пользователь даст три информации: $1, что является именем файла $2, что является паттерном $ 3, который является годом, когда он был обновлен

В основном то, что я хочу сделать, это найти все записи, которые имеют название компании: первый столбец, который соответствует шаблону, и из этих записей я хочу только те, которые соответствуют конкретному году, указанному $ 3. Также я хочу следующие столбцы из тех, которые совпадают: Название компании, описание, адрес и при обновлении. Я застрял, потому что когда я попытался извлечь первый столбец с параметром, он не работает??:

sed -E 's/([^,]*),'"$2"'/\1/' "$1"

Ничего не отфильтровывает??

-3
задан 18 June 2021 в 05:28

2 ответа

По умолчанию sed не фильтрует, он редактирует и затем печатает результат с любым действием редактирования или без него.

Если вы хотите, чтобы он был отфильтрован, используйте параметр -n , чтобы он не печатал строки после их редактирования. Затем в строках, которые вы хотите вывести на печать, вы помещаете оператор p. Например:

sed -nE 's/match string/replace string/p'

Тогда будут напечатаны только те элементы, которые совпадают и успешно отредактированы.

Но также имейте в виду, что все в совпадающей строке, не совпадающей со строкой соответствия, будет пропущено без изменений и напечатано. Поэтому, если вы хотите напечатать только некоторые части строки, ваша строка соответствия должна соответствовать всей строке, а затем строка замены возвращает только те части, которые вы хотите напечатать (если, конечно, вы не хотите печатать несогласованные части).

0
ответ дан 28 July 2021 в 11:27

sed или awk - оба неправильные инструменты.


Ваш файл представляет собой файл со значениями, разделенными запятыми (CSV), поэтому лучше всего использовать правильный csv синтаксический анализатор:

csvgrep -c "Business Name" -m "$2" -c "When Updated" -m "$3" "$1"

Установить через:

sudo apt install csvkit

Поскольку ваши поля, которые вы хотите фильтровать, происходят чтобы быть первым и последним, вы могли бы использовать grep , awk или sed :

awk -F, -v name_pattern="$2" -v year="$3" '$1~name_pattern && $NF~year' "$1"

или

sed -nE "/[^,]*$2.*$3-[0-9]{2}-[0-9]{2}\$/p" "$1"

или

grep -P "^[^,]*$2.*$3-[0-9]{2}-[0-9]{2}\$" "$1"

Однако, пожалуйста обратите внимание, что эти решения очень хрупкие и работают только для вашего простого примера. Например. если бы в одном из названий вашей компании была запятая, это не сработало бы:

"Bla, Bla & sons"

То же самое, если вы хотите фильтровать по другим полям.


Правильный парсер csv всегда должен быть предпочтительнее!

4
ответ дан 28 July 2021 в 11:27

Другие вопросы по тегам:

Похожие вопросы: