Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Linux string replacement command linux string replacement
Linux string replacement command linux string replacement

How to replace a string at a specific position in a specified line of a file in Linux?

You can replace strings through Shell script. If you know which line, the format is as follows: stringid="str_ig_ghjjk">hello/string>The content of the string may be any string. How to replace > with the middle Replace the string with my string such as world. Specific example: For example, replace line 128 with sed-i'128s/>.*/>world/'test.txt like this

How to perform line breaks on Linux character conversion?

In Linux, text files use "n" (0a) to represent carriage return and line feed, while Windows use "rn" (0d0a) to represent carriage return and line feed. Therefore, errors often occur when using Windows text files in Linux. In order to avoid this error, Linux provides two commands for converting text formats to each other: dos2unix and unix2dos. dos2unix converts "rn" into "n", and unixtodos converts "n" into "rn".

The commands dos2unix and unix2dos are very simple to use. The format is: dos2unixfilename

If you want to know more about these two commands, you can check the manual:

mandos2unix

It will tell you the advanced usage of dos2unix

(2)

The dos2unix tool is not installed by default under Ubuntu, and there is no tool with this name ( I have used dos2unix in solaris, but I don’t know why Ubuntu doesn’t have it). But there is an alternative tool - tofrodos. Let's talk about its installation and use.

Of course it is still installed with apt.

(1) Install tofrodos

sudoapt-getinstalltofrodos

In fact, it installs two tools: todos (equivalent to unix2dos), and fromdos (equivalent to dos2unix)

After installation, you can now convert text formats.

For example: todosHello.txt (i.e. unix2dosHello.txt)

fromdosHello.txt (i.e. dos2unixHello.txt)

(2) Do some optimization

Since you are used to the commands of unix2dos and dos2unix, you can link the two tools installed above into unix2dos and dos2unix, or just give them aliases and put them in the startup script.

Steps:

The following is the code snippet:

ln -s/usr/bin/todos/usr/bin/unix2dos

ln-s/usr/bin/fromdos/usr/bin/dos2unix

Or give an alias in ~/.bashrc

vi~/.bashrc

Add aliasunix2dos=todosaliasdos2unix=fromdos

(3)

In addition, if you want to replace a large number of times in the directory, you can use find+xargs

For example:

find./model-name*.sh|xargsfromdos

Linux batch replace folder name, file name, file content command?

1. Use for to be flexible. You can replace characters in file names, add dates, etc. You can first test it with the following statement foriin*aa*;doNN=$(echo$i|sed's/aa/bb/' );echo"$NN";doneTo be on the safe side, only copy foriin*aa*;doNN=$(echo$i|sed's/aa/bb/');cp"$i""$NN";doneThis is a name change , use with caution foriin*aa*;doNN=$(echo$i|sed's/aa/bb/');mv"$i""$NN";done2. Use rename concisely and first use the -n switch to test rename-n's/ aa/bb/'* test is correct. Remove -n to rename rename's/aa/bb/'*

Linux command to overwrite and replace files?

In Linux, you can use the cp command to overwrite and replace files

Enter the alias command and see that the system uses cp internally. When copying, if a file with the same file name is encountered, It will keep asking whether to overwrite and ask you to enter yesorno.

Why? This is because Linux uses aliases on the cp command for security reasons.