cat, grep and cut - translated to python
You need to have better understanding of the python language and its standard library to translate the expression
cat "$filename": Reads the file cat "$filename"
and dumps the content to stdout
|
: pipe redirects the stdout
from previous command and feeds it to the stdin
of the next command
grep "something": Searches the regular expressionsomething
plain text data file (if specified) or in the stdin and returns the matching lines.
cut -d'"' -f2: Splits the string with the specific delimiter and indexes/splices particular fields from the resultant list
Python Equivalent
cat "$filename" | with open("$filename",'r') as fin: | Read the file Sequentially
| for line in fin: |
-----------------------------------------------------------------------------------
grep 'something' | import re | The python version returns
| line = re.findall(r'something', line)[0] | a list of matches. We are only
| | interested in the zero group
-----------------------------------------------------------------------------------
cut -d'"' -f2 | line = line.split('"')[1] | Splits the string and selects
| | the second field (which is
| | index 1 in python)
Combining
import re
with open("filename") as origin_file:
for line in origin_file:
line = re.findall(r'something', line)
if line:
line = line[0].split('"')[1]
print line
What is the code equivalent to grep -B in python?
You'll just have to keep a buffer (a list) of the N last lines, then print them when you encounter a match.
context_lines = []
for line in f:
context_lines.append(line)
if len(context_lines) > 5: # Too many lines?
context_lines.pop(0) # Pop the first one off.
if word in line:
# context_lines already includes the `line` we're looking at
for ctx_line in context_lines:
print(ctx_line)
run linux cat command in python or bash loop
In your python command, you are doing it wrongly , the % count
should be inside the os.system()
call. Example -
os.system('''cat member.php\?%d* | grep -i '<li class="navbit lastnavbit"><span>' | cut -d'>' -f3 | cut -d'<' -f1 >> users.txt''' % count)
Also, you should iterate till the amount you want.
Use cut and grep to separate data while printing multiple fields
I found what I was looking for in some documentation. Anchor matches!
grep "$week$" file
would output this if $week was 0
asset.14548.extension 0
asset.40795.extension 0
I couldn't find my exact question or a closely similar question with a simple answer, so hopefully it helps the next person scratching their head on this.
parsing results in bash
Without a sample file, it is hard to write a working example. But I see, both values are from the same text file and the same line. Therefore I would use awk to do it:
$ cat text
service1;some_other_text;2.3.4
service2;just_some_text;55.66
$awk -F ";" '{printf "%s:%s\n", $1, $3}' test
service1:2.3.4
For a JSON file, it would be easier if you can use jg (e.g. apt-get install jg):
$ cat test.json
[
{
"name": "service1",
"customfield_10090": "1.2.3"
},
{
"name": "service2",
"customfield_10090": "23.3.2"
}
]
$jq '.[] | .name + ":" + .customfield_10090' test.json | sed 's/"//g'
service1:1.2.3
service2:23.3.2
The sed is necessary to eliminate the quotes.
Search for list of keywords in python
For a minimal change to get this working, you can change any(keywords) in string_tags
to the following:
any(keyword in string_tags for keyword in keywords)
Or an alternative using sets:
keywords = set(['diy','decorate', 'craft', 'home decor', 'food'])
def get_tags(blog_soup):
tags_html = blog_soup.find('div', attrs = {'style': 'margin-left: 60px; margin-bottom: 15px;'})
tags = [tag.string for tag in tags_html.findAll('a')]
if keywords.intersection(tags):
print url
how to grep everything between single quotes?
Try something like this: sed -n "s#version:\s*'\(.*\)'#\1#p" myfile.txt
. This avoids the redundant cat
and grep
by finding the "version" line and extracting the contents between the single quotes.
Explanation:
the -n
flag tells sed not to print lines automatically. We then use the p
command at the end of our sed pattern to explicitly print when we've found the version line.
Search for pattern: version:\s*'\(.*\)'
version:\s*
Match "version:" followed by any amount of whitespace'\(.*\)'
Match a single'
, then capture everything until the next'
Replace with: \1
; This is the first (and only) capture group above, containing contents between single quotes.
How to concatenate multiple lines of output to one line?
Use tr '\n' ' '
to translate all newline characters to spaces:
$ grep pattern file | tr '\n' ' '
Note: grep
reads files, cat
concatenates files. Don't cat file | grep
!
Edit:
tr
can only handle single character translations. You could use awk
to change the output record separator like:
$ grep pattern file | awk '{print}' ORS='" '
This would transform:
one
two
three
to:
one" two" three"
How can I grep for a string that begins with a dash/hyphen?
Use:
grep -- -X
Documentation
Related: What does a bare double dash mean? (thanks to nutty about natty).
Related Topics
A Simple Python Deployment Problem - a Whole World of Pain
Kill Python Interpeter in Linux from the Terminal
Differencebetween C.Utf-8 and En_Us.Utf-8 Locales
Capturing Output of Python Script Run Inside a Docker Container
How to Default Python3.8 on My MAC Using Homebrew
How to Delete a Directory Created with Tempfile.Mkdtemp
How to Deal with Linux/Python Dependencies
How to Download Python from Command-Line
How to Kill Zombie Processes Created by Multiprocessing Module
Howto Do Python Command-Line Autocompletion But Not Only at the Beginning of a String
Pyodbc:Can't Open the Driver Even If It Exists
Install Tkinter and Python Locally
Fake Serial Communication Under Linux
Paramiko Error: Error Reading Ssh Protocol Banner
Python: When to Use Pty.Fork() Versus Os.Fork()