Rs:record Separator, Record delimiter
Ors:output record separate, output current record delimiter
Fs:field Separator, Field delimiter
Ofs:out of field Separator, output fields delimiter
Ps:rs, ORS, FS, OFS's English explanation is by no means the case, here just explain clearly. It is recommended to read Awk's English reading, which explains the meaning of abbreviations.
What is field (field) and what is a record (row of records)?
Example:
1.txt
I am a student.
I like to swim
Hello Moto
1 represents the first record line, 2 for the second record line, and 3 for the third record row. By observing we can know that there are 3 record lines in total.
Look at the first line: "I am a student", each word in this line is a field. "I" is a field, "AM" is a field, "a" is a field, "Student" is a field, there is a total of 4 fields in the row.
RS and ORS
RS: Record Line separator
Example:
1.txt
a\n
b\n
c\n
d\n
e\n
The text has a total of 5 lines, and each line has a newline character "\ n". So each row of records is marked with "\ n" as a (newline) flag.
You can use this method to understand:
Find the so-and-so flag, and make each and every content back into a row
Example
1.txt
A|b|c
Code: awk ' begin{rs= ' | ';} {Print $} '
A
B
B
ORS: Can be regarded as the reverse process of RS
Example
1.txt
A
B
C
This can be understood as:
Observe the newline symbol for each line, and replace the newline symbol with the symbol you want.
awk ' begin{ors= '----'} {print $} ' 1.txt
A----b----c----
FS: Field delimiter
The FS default value is "(space)", such as "Hello Moto".
There is a space in "Hello Moto", where the space is the delimiter for Hello and moto (separator), while Hello and Moto are fields (files). Awk is distinguished by a space.
Looking at "I----love----You", if we use the command "awk" {print $} "" You will see the result as:
I----love----
If you want to print out three letters, you can see "----" as a delimiter by observing.
awk ' begin{fs= "----";} {print $1,$2,$3} ' filename
I love You
OFS: The field delimiter for the output.
So to explain, as in the above example "I----love----You", "----" for the delimiter (FS), if we want to change with other symbols to display can do this:
awk ' begin{fs= "----", ofs= "* * * *"} {print $1,$2,$3} ' filename
I*****love*****you
Actually, there's an example of OFS.
echo "ABC" | awk ' {ofs= '. "} {nf=nf; print nf,$0} '
Results
1.abc
Ps:rs and Ors can be said to be an inverse process () can also be seen as a replacement process, but as the process of reciprocal inversion is better understood; FS and OFS are a replacement process.
Has a nice day!!!
Awk RS, ors and FS, OFS