test:
Note: there are 2 spaces between "XX" and "Male"
#! /bin/bash
Name Sex Cell#
XX Male 0000000000
YY Female 0000000101
terminal:
Select the first filed of ./test
-d " ", means we are using "one space" as the delimiter, in this case:
first line of ./test: two fields: "#!" and "/bin/bash"
second line of ./test, three fields: "Name", "Sex" and "Cell#"
third line of ./test, four fields: "XX", null, "Male" and "000000000", because we have two spaces between "XX" and "Male", which generate one "null" as the second field
fourth line of ./test, three fields: "YY", "Female", and "000000101"
-f 1 means: we are going to select the first field of each line.
aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ cut -d " " -f 1 ./test
#!
Name
XX
YY
select other specific field:
First command: third line is empty because 2nd field of third line is empty as explained above.
second command: first line is empty because there are only 2 fields in the first line.
aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ cut -d " " -f 2 ./test
/bin/bash
Sex
Female
aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ cut -d " " -f 3 ./test
Cell#
Male
0000000101
select 4th field: only third line return 4th field, since all other lines have only 3 fields each.
aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ cut -d " " -f 4 ./test
0000000000
select field 1 to field 3 of each line: -f 1-3 means a range.
aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ cut -d " " -f 1,2,3 ./test
#! /bin/bash
Name Sex Cell#
XX Male
YY Femail 0000000101
aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$
aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ cut -d " " -f 1-3 ./test
#! /bin/bash
Name Sex Cell#
XX Male
YY Femail 0000000101
2. cut: use different delimiters
test:
If using ":" as delimiter, then:
1st line: 3 fields: "Name", "Male", "Cell#"
2nd line: 4 fields: "XX",null, "Male", "0000000000"
3rd line: 3 fields " YY"(note the space here), "Female", "0000000101"
Name:Sex:Cell#
XX::Male:0000000000
YY:Female:0000000101
terminal:
1st command: select first 3 fields, note the 2nd line, because we have one "null" object as the second field, so it stopped at "Male"
Note the 3rd line, using ":" as the delimiter, the first field's spaces are counted as part of field
2nd command, select 4th field, only 2nd line has 4 fields, so only 2nd line get outputted.
aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ cut -d ":" -f 1-3 ./test
Name:Sex:Cell#
XX::Male
YY:Female:0000000101
aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ cut -d ":" -f 4 ./test
0000000000
3. cut: select specific count of characters
test is same as above
terminal:
select 1st character of each line: the 3rd line is empty because third line start with a white space
aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ cut -c 1 ./test
N
X
select first 5 characters of each line:
aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ cut -c 1-5 ./test
Name:
XX::M
Y
select more characters than each line has, it will stop at the end of line automatically:
aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ cut -c 1-100 ./test
Name:Sex:Cell#
XX::Male:0000000000
YY:Female:0000000101
Select first 10 characters(permissions) of each line of "ls -lrt"
aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ ls -lrt | cut -c 1-10
total 448
-rwxrwxr-x
-rwxrwxr-x
-rw-rw-r--
-rwxrwxr-x
-rw-rw-r--
drwxrwxr-x
-rw-rw-r--
-rw-rw-r--
-rwxrwxr-x
-rwxrwxr-x
No comments:
Post a Comment