tse --Introduction to Text Stream Editor in Python

Introduction

This article is the 12th day article of Shell Script Advent Calendar 2015.

Here, the text formatting tool "tse" by Python created by Mr. Ishimoto @atsuoishimoto I would like to introduce.

Execution environment

What is tse?

You can understand the details by looking at the slides announced at this year's PyCon JP 2015. tse --Text Formatting Utility with Python

Super simply What is tse? "Python is said to be unsuitable for one-liner, but I still want to do text processing or Python." In response to the request "CLI tools for doing things like ** sed ** and ** awk ** in Python" is.

You can install it with pip.

pip install tse

Good points of tse

After that, I think it's faster to see the actual processing, so I'll cover some of them.

Challenge the shell art study session problem

The 17th Junkudo is a study session that shell art has taken over

https://blog.ueda.asia/?p=6454

Q1

The following data

$ cat data1
a 1
b 4
a 2
a 3
b 5

Let's convert it as follows.

a 1 2 3
b 4 5

If you have the spare capacity, try the following JSON format.

{a:[1,2,3],b:[4,5]}

answer

$cat data1 | tse -b 'd={"a": "", "b": ""}' -s '' 'd[L1] = d[L1] + " " + L2' -e 'for k, v in sorted(d.items()):print(k + v)'
a 1 2 3
b 4 5
$cat data1 | tse -m json -b 'd={"a": [], "b": []}' -s '' 'd[L1].append(int(L2))' -e 'print(json.dumps(sorted(d.items())))'
[["a", [1, 2, 3]], ["b", [4, 5]]]

18th Do you want to add garlic? Shell art study session

https://blog.ueda.asia/?p=6877

Q1 In the following file, the first column is the key and the second column is the value, but look for a key that has both "Oton" and "Okan" values.

$ cat text 
 001 Oton
 001 Oton
 001 Akan
 002 Okan
 003 Oton
 003 Kettle
 003 Okan
 004 Okan
 005 Oton
 005 oranges
 005 Akan

answer

$uniq text | tse -s 'Oton|Okan' 'print L1' | uniq -d
003

19th Shell Art 3rd Anniversary Study Session

https://blog.ueda.asia/?p=7146

Q2 Two natural numbers

$ echo 1 4

After outputting with echo like

4
3
2
1
2
3
4

Try filling in the numbers in between.

answer

$echo 1 4 | tse -s '' 'for s in range(int(L1), int(L2)+1)[::-1]: print(s)' 'for s in range(int(L1)+1, int(L2)+1): print(s)'

in conclusion

I tried to use tse roughly, but it became easy to write one liner with the syntax of Python that I am accustomed to. I feel like I was able to get used to the one-liner field that I was scared to avoid ^^

Tomorrow's turn is @zayarwinttun!

Recommended Posts

tse --Introduction to Text Stream Editor in Python
Speech to speech in python [text to speech]
[Introduction to Python] How to use class in Python?
Introduction to Vectors: Linear Algebra in Python <1>
Introduction to Effectiveness Verification Chapter 1 in Python
Clustering text in Python
Introduction to effectiveness verification Chapter 3 written in Python
I wrote "Introduction to Effect Verification" in Python
Introduction to Python language
Text processing in Python
Introduction to OpenCV (python)-(2)
Introduction to Effectiveness Verification Chapter 2 Written in Python
Introduction to Linear Algebra in Python: A = LU Decomposition
Python OpenCV tried to display the image in text.
UTF8 text processing in python
Introduction to Python Django (2) Win
To flush stdout in Python
Login to website in Python
Introduction to serial communication [Python]
Design Patterns in Python: Introduction
[Introduction to Python] <list> [edit: 2020/02/22]
Introduction to Python (Python version APG4b)
An introduction to Python Programming
How to develop in Python
Introduction to Python For, While
Post to Slack in Python
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
Introduction to Effectiveness Verification Chapters 4 and 5 are written in Python
Convert Excel file to text in Python for diff purposes
[Introduction to Udemy Python 3 + Application] 58. Lambda
[Introduction to Udemy Python 3 + Application] 31. Comments
[Python] How to do PCA in Python
Introduction to Python Numerical Library NumPy
Practice! !! Introduction to Python (Type Hints)
[Introduction to Python3 Day 1] Programming and Python
GOTO in Python with Sublime Text 3
Convert markdown to PDF in Python
How to collect images in Python
[Introduction to Python] <numpy ndarray> [edit: 2020/02/22]
Introduction to Python Hands On Part 1
[Introduction to Python3 Day 13] Chapter 7 Strings (7.1-7.1.1.1)
How to use SQLite in Python
[Introduction to Python] How to parse JSON
[Introduction to Udemy Python 3 + Application] 56. Closure
In the python command python points to python3.8
[Introduction to Python3 Day 14] Chapter 7 Strings (7.1.1.1 to 7.1.1.4)
Introduction to Protobuf-c (C language ⇔ Python)
[Introduction to Udemy Python3 + Application] 59. Generator
Try to calculate Trace in Python
Extract text from images in Python
[Introduction to Python3 Day 15] Chapter 7 Strings (7.1.2-7.1.2.2)
[Introduction to Python] Let's use pandas
Sort large text files in Python
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
6 ways to string objects in Python
How to use PubChem in Python
[Introduction to Python] Let's use pandas
[Introduction to Udemy Python 3 + Application] Summary
Reading and writing text in Python