[LINUX] Process the contents of the file in order with a shell script

[1. What you want to do](#What you want to do) [2. Code introduction](# Code introduction) [3. Execution example](# Execution example)

Thing you want to do

The contents of the file are acquired and processed in order from the top. In this article, I will introduce a shell script of a loop statement that acquires the contents of the name data (name.txt) in the file from above, assigns a serial number, and converts it to csv (name.csv).

Code introduction

** Current folder status **

State before execution


$ cat name.txt
Taro Suzuki
Jiro Suzuki
Noriko Yamada
Junko Nishida

main.sh


#!/bin/sh
cnt=1

cat name.txt | while read data
do
	echo ${cnt},${data} >> name.csv
	#Increment of cnt variable
	cnt=`expr ${cnt} + 1`
done

Execution example

main.run sh


sh main.sh

Post-execution status


$ cat name.csv
1,Taro Suzuki
2,Jiro Suzuki
3,Noriko Yamada
4,Junko Nishida

Recommended Posts

Process the contents of the file in order with a shell script
Process the files in the folder in order with a shell script
Conditional branch due to the existence of a shell script file
[Note] A shell script that checks the CPU usage of a specific process in a while loop.
Template of python script to read the contents of the file
When a file is placed in the shared folder of Raspberry Pi, the process is executed.
Access the file with a relative path from the execution script.
[Note] Import of a file in the parent directory in Python
A Python script that compares the contents of two directories
The story of the "hole" in the file
[OCI] Python script to get the IP address of a compute instance in Cloud Shell
[python, ruby] fetch the contents of a web page with selenium-webdriver
Create a shell script to run the python file multiple times
Output the contents of ~ .xlsx in the folder to HTML with Python
Save the Pydrive authentication file in a different directory from the script
Read a file in Python with a relative path from the program
The idea of feeding the config file with a python file instead of yaml
Create a function to get the contents of the database in Go
Write a script in Shell and Python to notify you in Slack when the process is finished
Implement part of the process in C ++
Check the existence of the file with python
Run the Python interpreter in a script
Manage logrotate generations with a shell script.
Memo of the program to get the date in two digits with javascript, Ruby, Python, shell script
The story of a Parking Sensor in 10 minutes with GrovePi + Starter Kit
Replace the directory name and the file name in the directory together with a Linux command.
A memo organized by renaming the file names in the folder with python
Generate a list packed with the number of days in the current month.
[Introduction to Python] How to sort the contents of a list efficiently with list sort
Test & Debug Tips: Create a file of the specified size in Python
I want to sort a list in the order of other lists
View the full path (absolute path) of a file in a directory in Linux Bash
Receive a list of the results of parallel processing in Python with starmap
I made a GAN with Keras, so I made a video of the learning process.
I made a program to check the size of a file in Python
I made a mistake in fetching the hierarchy with MultiIndex of pandas
Get the return value of an external shell script (ls) with python3
[Python] Get the files in a folder with Python
View today's weather forecast with a shell script
Get the caller of a function in Python
Make a copy of the list in Python
Find the number of days in a month
Save the object to a file with pickle
Dump the contents of redis db with lua
Tucker decomposition of the hay process with HOOI
Output in the form of a python array
Convert the character code of the file with Python3
Create a table of contents with IPython notebook
Various ways to read the last line of a csv file in Python
Feel free to write a test with nose (in the case of + gevent)
To output a value even in the middle of a cell with Jupyter Notebook
Zip-compress any file with the [shell] command to create a file and delete the original file.
Replace the named entity in the read text file with a label (using GiNZA)
How to get a list of files in the same directory with python
What I investigated in the process of expressing (schematicizing) containers in a nested frame with Jupyter and making it
I created a script to check if English is entered in the specified position of the JSON file in Python.
How to display the modification date of a file in C language up to nanoseconds
Get the file name in a folder using glob
Automatically determine and process the encoding of the text file
Get the value of a specific key in a list from the dictionary type in the list with Python
Let's understand the standard input / output of bash together and write a shell script.