مدونة مادة تطبيقات انظمة التشغيل: الملفات الدفعية 2

re: Why Linux Users NEVER SUBSCRIBE to any Youtuber; a primer to grep's -P option

Suraj uploaded a video where he wrote a script to be notified when a specified youtube channel uploads a new video. In the video, he takes advantage of youtube's api in order to scrape for the id of the latest video upload of a channel. He uses several pipes to accomplish this task, and while there's nothing wrong with the one-liner that was used, I just wanted to show off grep's -P option, which is used to enable features from Perl's regex engine.
Just like in the video, we need to start by getting the channel's id. We can very easily scrape for the channel_id of a youtube channel with the following:
$ curl -s 'https://www.youtube.com/@{user-id}' | grep -Po "(?<=channelId\":\")[^\"]+(?=\",\"title\":\")" UCXXXXXXXXXXXXXXXXXXXXXX 
This is where grep's -P command comes in handy. Perl's regex engine has something called "lookarounds". These are zero-width assertions that allow for creating custom anchors in order to narrow down the conditions for a pattern match. If you just read that and you are already familiar with regex, yet you have no idea what I'm talking, allow me to explain further.
If you know a bit of regex, then you are likely familiar with the anchors ^ and $. These too are zero-width assertions used to narrow down the conditions for a pattern match. For something like ^foobar$, the way the regex engine works is it finds the string being processed, in this case foobar, and then "looks around" the string to see if it can assert that it is in fact at the beginning of a line (^) AND at the end of the line ($) in order to successfully match the pattern. ^ and $ are zero-width as they do not match any characters, they simply look around from the string to assert what comes before and after.
Let's take away the lookarounds from the original grep and see exactly what we're matching:
$ curl -s 'https://www.youtube.com/@{user-id}' | grep -Po "channelId\":\"[^\"]+\",\"title\":\"" channelId":"UCXXXXXXXXXXXXXXXXXXXXXX","title":" 
We're looking for a pattern that matches the literal string channelId":", followed by [^\"]+ (one or more NOT quotation mark characters, which will capture the channel id), ending with the literal string ","title":". We have to use the regex for one or more NOT quotation marks characters instead of something more straightforward like .+ because the pattern we're matching just so happens to be on a very long single line, and the specificity will constrain regex's greedy nature from matching something further along that line that we don't mean for it to match, and it will. In this instance, .{24} would also suffice as that is saying to match 24 of any character, the exact length of youtube channel ids.
We only want the channel id string though. You can see that it is "anchored" so to speak by the preceding string channelId":" followed by ","title":". Time to make use of a feature from Perl's regex engine to lookaround.
Back to the original grep command:
grep -Po "(?<=channelId\":\")[^\"]+(?=\",\"title\":\")" 
Note that the parenthetical groups are back, and not only that, they also contain some very specific constructs: ?<= and ?=. This is the syntax for Positive lookbehind and Positive lookahead respectively. Positive because the pattern match within the assertion must succeed(be positively identified). Negative lookahead and lookbehind exist as well(good for when you want to match only if the assertion fails). These are what create custom anchors and turn the strings channelId":" and ","title":" into zero-width assertions. Think back to ^foobar$. When foobar is matched, the regex engine will look behind foobar to see if it's anchored to the start of a line, and look ahead of foobar to see if it's anchored to the end.
With the grep command above, we are processing the string [^\"]+, and we are looking behind (?<=) the string to see if it is anchored to channelId":" and we are looking ahead (?=) of it to see if it's anchored to ","title":". If the assertions are satisfied, the string we are looking for and only that string will be printed. The anchors have served their purpose as they only look around the pattern and do not match any characters themselves.
Hopefully with that introduction to perl's lookaround constructs you have more of an understanding of how they work if you didn't before.
Let's press forward with the final grep command for extracting video-ids from the xml output of the api response:
$ curl -s 'https://www.youtube.com/feeds/videos.xml?channel_id=UCXXXXXXXXXXXXXXXXXXXXXX' | grep -oP "(?<=)[^<]+(?=)" xxxxxxxxxxx xxxxxxxxxxx xxxxxxxxxxx xxxxxxxxxxx xxxxxxxxxxx xxxxxxxxxxx xxxxxxxxxxx xxxxxxxxxxx xxxxxxxxxxx xxxxxxxxxxx xxxxxxxxxxx xxxxxxxxxxx xxxxxxxxxxx xxxxxxxxxxx xxxxxxxxxxx 
This one is more straightforward since the xml is nice and organized. We create custom anchors to lookaround for. From the string [^<]+ , which will be the video id (though .{11} and now .+ would work too), we will lookbehind (?<=) for the literal string and lookahead (?=) for the literal string . If the assertions are satisified, the 15 video ids will be returned.
If you just want the latest video:
$ curl -s 'https://www.youtube.com/feeds/videos.xml?channel_id=UCXXXXXXXXXXXXXXXXXXXXXX' | grep -m1 -oP "(?<=)[^<]+(?=)" xxxxxxxxxxx 
And voila, we got just the output we wanted using grep only.
I just learned about this so I am satisfied with just writing this up as a way to reinforce my own understanding on something I know I will return to frequently, but I'm also happy to share this knowledge with some dank linux users in hopes that they too will benefit from it.
I am not exactly a grep master and I know nothing about perl beyond what I just shared right now. I wrote this up using the following for reference:
GNU GREP and RIPGREP (learnbyexample) - Perl Compatible Regular Expressions
Perl Monks - Using Look-ahead and Look-behind
submitted by windows_sans_borders to danklinuxusers [link] [comments]

Grep Command in Linux/Unix

submitted by bug_1729 to programming [link] [comments]

Grep Command in Linux

Grep Command in Linux submitted by linuxtldr to linuxtldr [link] [comments]

Want to exclude grep from ps results under Linux or Unix?

submitted by DCGMechanics to linux [link] [comments]

Linux Grep

-- grep E-mails grep -EiEio '\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b' emails.txt -- или так найти все e-mail grep -o '[[:alnum:]+\.\_\-]*@[[:alnum:]+\.\_\-]*' emails.txt -- выбрать все IP адреса grep -E -o "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)" /valog/auth.log -- исключить шаблон grep -v string-to-exclude filename -- исключить процессы пользователя root ps -efH | grep -v '^root' -- без процессов root и zabbix ps -efH | grep -v '^root' | grep -v '^zabbix' -- убрать комментарии и пустые строки grep -v '^#|^$' example.conf
submitted by SiTek79 to u/SiTek79 [link] [comments]

Grep command in Linux (With Examples)

Grep command in Linux (With Examples) submitted by motivizer to linuxiscool [link] [comments]

Conditional For Loop for Linux/Shell | grep command ?

Hi All,
I'm currently working on a script that can loop through a list of items and look for a specific keyword, then return true and add the items name.
Structure:
-> List of Entity/Items -> Loop and match a specific keyword using "grep" command. -> If True -> Print the keyword and the entity/item name in the output. -> Else: -> No info. found 
Here's my code (refer to name: Conditional Loops):
--- - name: Shell Example hosts: all gather_facts: False vars_prompt: - name: "shortname_in" prompt: "Input show or shortname" private: no - name: "keyword_in" prompt: "Enter specific keyword" private: no tasks: - name: Check Date with shell command shell: "date" register: datecmd tags: datecmd - debug: msg="{{datecmd.stdout}}" tasks: - name: Find a specific keyword(s) to a singe group/multple devices shell: "cinfo {{ shortname_in }}" register: cinfocmd tags: cinfocmd - debug: msg="{{cinfocmd.stdout.split('\n')}}" - debug: msg: "{{ cinfocmd.stdout | regex_findall('[a-zA-Z0-9]+-[a-zA-Z0-9]+-[a-zA-Z0-9]+') }}" - name: get the list of entity set_fact: entlist="{{ cinfocmd.stdout | regex_findall('[a-zA-Z0-9]+-[a-zA-Z0-9]+-[a-zA-Z0-9]+') }}" - name: Conditional Loops shell: "viewtool -u {{entlist}} | grep -Pi {{keyword_in}}" register: viewcfgresult tags: viewcfgresult - debug: msg="{{viewcfgresult.stdout}}" 

Note: I can match when specifying specific items but not when looping. Any recommendations?
Specific item: shell: "viewtool -u {{entlist.20}} | grep -Pi {{keyword_in}}" Result (working): ok: [206.112.194.139] => { "msg": " description xxxxxx" 
submitted by 1searching to ansible [link] [comments]

i bought a bluetooth dongle that works on windows but not linux. however linux does detect it and this was the output for sudo dmesg | grep Bluetooth:

https://imgur.com/a/93uCsEn
any suggestions? can i make it work?
linux mint 20.3 cinnamon
cinnamon version 5.2.7
linux kernel 5.4.0-113-generic
edit: by the way, i booted from a usb flash drive a fresh installation of mint to test the dongle. the result was the same. it was recognized, but wouldn't locate any devices (i tried 2 different ones)
submitted by aluminium_is_cool to linuxquestions [link] [comments]

ripgrep-all Command in Linux: One grep to Rule Them All

ripgrep-all Command in Linux: One grep to Rule Them All submitted by oaf357 to devopsish [link] [comments]

Uso del comando grep en Linux y UNIX con ejemplos

Uso del comando grep en Linux y UNIX con ejemplos submitted by cronos426 to GNULinuxEsp [link] [comments]

ripgrep-all Command in Linux: One grep to Rule Them All

ripgrep-all Command in Linux: One grep to Rule Them All submitted by minnixtx to LinuxLugcast [link] [comments]

How to Use Grep to Extract Emails from a File in Linux

If you have once crossed paths with data entry and extraction tasks either as a freelancing virtual assistant or for a personal/work project, you have understood the importance of data organization and management. The Linux operating system always transforms its users into data specialists.
Read More: https://www.linuxshelltips.com/grep-extract-emails-file-linux/
submitted by linuxshelltips to u/linuxshelltips [link] [comments]

7 Uses of grep Commands in Linux

submitted by yangzhou1993 to programming [link] [comments]

i bought a bluetooth dongle that works on windows but not linux. however linux does detect it and this was the output for sudo dmesg | grep Bluetooth:

https://imgur.com/a/93uCsEn
any suggestions? can i make it work?
linux mint 20.3 cinnamon
cinnamon version 5.2.7
linux kernel 5.4.0-113-generic
edit: by the way, i booted from a usb flash drive a fresh installation of mint to test the dongle. the result was the same. it was recognized, but wouldn't locate any devices (i tried 2 different ones)
submitted by aluminium_is_cool to linux4noobs [link] [comments]

Linux/BSD command line wizardry: Learn to think in sed, awk, and grep

Linux/BSD command line wizardry: Learn to think in sed, awk, and grep submitted by speckz to commandline [link] [comments]

How To Use the Grep Command in Linux to Search Inside Files

How To Use the Grep Command in Linux to Search Inside Files submitted by theterk to tomshardware [link] [comments]

15 Practical Grep Command Examples In Linux / UNIX

15 Practical Grep Command Examples In Linux / UNIX submitted by u-copycat to DevTo [link] [comments]

7 Uses of grep Commands in Linux

submitted by yangzhou1993 to programming [link] [comments]

Linux text processing reference & recipes. Featuring: vim, tr, cat, tac, sort, shuf, seq, pr, paste, fmt, cut, nl, split, csplit, sed, awk, grep and regex.

submitted by true_adrian_scheff to linux_programming [link] [comments]

Linux/BSD command line wizardry: Learn to think in sed, awk, and grep

Linux/BSD command line wizardry: Learn to think in sed, awk, and grep submitted by NISMO1968 to SysAdminBlogs [link] [comments]

Power of Linux command line: I was needing list of newspaper's email addresses curl https://www.apns.com.pk/member_publication/index.php | grep @ | sort | uniq

Power of Linux command line: I was needing list of newspaper's email addresses curl https://www.apns.com.pk/member_publication/index.php | grep @ | sort | uniq submitted by asakpke to HowToHack [link] [comments]

Root to Linux: Search Files with Grep

Root to Linux: Search Files with Grep submitted by u-copycat to DevTo [link] [comments]

ستحتاج إلى الانتقال إلى صفحة إصدارات Github لـ code-server واختيار أحدث إصدار من Linux (سيحتوي اسم الملف على الكلمة "linux"). كان الإصدار الأخير عند كتابة هذا التقرير هو 2.1692. نزّله باستخدام في ملفات Linux (بالفعل جميع unicies) يتم إنشاؤها عند فتحها وحذفها عندما لا شيء يحمل إشارة إليها. في هذه الحالة ، البرنامج الذي فتحه والدليل فتحه 'في' مراجع المراجع إلى الملف. عندما يريد برنامج cp ... و تمرير اسم الملف كمعامل (طالما ان الملف موجود في الدليل الحالي) . طباعة المخرجات علي الشاشة. يستخدم الامر echo لطباعة سلاسل الحروف و قيم المتغيرات المختلفة علي شاشة الغلاف كالتالي: echo str. echo "Welcome to Bash” من الممكن استخدام او ... و تمرير اسم الملف كمعامل (طالما ان الملف موجود في الدليل الحالي) . طباعة المخرجات علي الشاشة. يستخدم الامر echo لطباعة سلاسل الحروف و قيم المتغيرات المختلفة علي شاشة الغلاف كالتالي: echo str. echo "Welcome to Bash” من الممكن استخدام او ... linux - شرح - what are grep patterns called? كيفية منع نتائج مطابقة الملف الثنائي في grep (2) عند استخدام grep في linux ، فإن النتيجة تحتوي دائمًا على الكثير من "تطابقات ملف XXX الثنائية" ، والتي لا يهمني. كيفية قمع هذا الجزء من النتائج ، أو كيفية ...

[index] [774] [2564] [4407] [11632] [100] [2418] [13618] [6226] [11431] [11926]

#

test2