[LINUX] How to execute the sed command many times using the for statement


On Linux, use the ** sed command to insert characters into various parts of a line **, which is a method using a for statement. I think it can be applied to other commands. (There seems to be a simpler way ...)

insert.sh (You can also copy and paste on the command line instead of a shell script)

cat sequence.txt > sequence2.txt
for int in 15 12 9 6 3
echo "$(sed -E "s/(^.{$int})/\1-/g" sequence2.txt)" > sequence2.txt

##Every 3 characters"-"Insert
## sequence.txt -> ("-"Insert) -> sequence2.txt

Execution result

$ cat sequence.txt

$ ./insert.sh ; cat sequence2.txt


  1. Using the same file name
    sed -E" s / (^. {$ Int}) / \ 1- / g "sequence2.txt> sequence2.txt Normally, the contents of ** sequence2.txt ** will be empty, but by setting
    ʻecho "$ (command)"> sequence2.txt<br>, ** sequence2 You can successfully overwrite the .txt ** without being empty and output. <br> <br> <font color = gray> (Empty data is output in the first place when> sequence2.txt is executed, and the operation ** empty the contents of ** sequence2.txt before sed** Because it is done (as a result, it issed to empty data). ʻEcho" $ (command) " The contents of are executed before> sequence2.txt`, so this will successfully overwrite ** sequence2.txt **) </ font>
    < br>
  2. In order to be able to use the variable ** $ int ** of the for statement in the sed command, you need to use " " (using '' is a variable in the variable. Because it is recognized as just a character string)

There seem to be various other methods for outputting to the file with the same name, so if you are interested, please refer to the link below. (1st link)

Regarding> 1.
Process the file with bash and overwrite the result with the same file
Redirect to file with the same name

Regarding 2.
[[bash] Difference between "" "," '", and" `--Rather rice school](https://blog.goo.ne.jp/01_mai/e/ a4f9f01fb647066d0c7eb37e8ae0a254)

Recommended Posts

How to execute the sed command many times using the for statement
How to write a GUI using the maya command
How to delete the specified string with the sed command! !! !!
How to execute a command using subprocess in Python
I didn't know how to use the [python] for statement
[Introduction to Python] How to use the in operator in a for statement?
[Linux] How to use the echo command
How to use the Linux grep command
Execute the COPY command using python's Psycopg
How to find out the number of CPUs without using the sar command
How to specify the launch browser for JupyterLab 3.0.0
How to use MkDocs for the first time
How to write a ShellScript Bash for statement
How to create a shortcut command for LINUX
How to find the correlation for categorical variables
A memorandum of how to execute the! Sudo magic command in Jupyter Notebook
[Introduction to Python] How to get the index of data with a for statement
How to create an article from the command line
[For beginners] How to use say command in python!
[For beginners] How to display maps and search boxes using the GoogleMap Javascript API
Linux user addition, how to use the useradd command
How to use the grep command and frequent samples
[Introduction to Python] How to stop the loop using break?
I want to automate ssh using the expect command!
(Remember quickly) How to use the LINUX command line
[Introduction to Python] How to write repetitive statements using for statements
How to turn the for statement when there are multiple values for one key in the dictionary
How to find the cumulative sum / sum for each group using DataFrame in Spark [Python version]
How to return to the previous directory with the Bash cd command
[python] How to use the library Matplotlib for drawing graphs
I want to automate ssh using the expect command! part2
How to define multiple variables in a python for statement
How to generate a query using the IN operator in Django
How to use the generator
Clone using the dd command
How to use the decorator
How to increase the axis
How to start the program
[Python] How to use the for statement. A method of extracting by specifying a range or conditions.
How to set the output resolution for each keyframe in Blender
[sh] How to store the command execution result in a variable
How to change the log level of Azure SDK for Python
Let's do it by dividing numpy without using the for statement
How to get the printer driver for Oki Mac into Linux
How to get followers and followers from python using the Mastodon API
[EC2] How to install chrome and the contents of each command
How to make a command to read the configuration file with pyramid
[EC2] How to install and download chromedriver from the command line
How to pass arguments using an instance with systemd's systemctl command
How to output the output result of the Linux man command to a file
How to monitor the execution status of sqlldr with the pv command
How to use machine learning for work? 01_ Understand the purpose of machine learning
Let's do it by multiplying numpy without using the for statement
■ How to execute "TensorFlow CNN for fast style transfer" which comes from GitHub by using Google Colaboratry
For the time being using FastAPI, I want to display how to use API like that on swagger
How to install python using anaconda
How to calculate the autocorrelation coefficient
How to use the zip function
How to use the optparse module
[For non-programmers] How to walk Kaggle
exec, eval to execute [python] statement