The cut command offers a lot of flexibility for selecting portions of each line in a file. Select everything but those characters is its "complement". Selecting the first four characters from a file is one thing. This doesn't mean displaying it in reverse order, but means "doing the opposite". You can also select an option to reverse the output from your cut request. We could also have just done this and added one more byte: $ cut -b1-24 cost In the second, we select by character, so it uses both bytes. In the first command above, the response shows show a block of dots because it's looking only at the first byte of the £ sign. In this example, we might see a difference simply because the £ sign occupies two bytes. Unless your data file includes characters that occupy more than a single byte, you would not see any differences. This displays the first three letters of each line of a file that lists the days of the week. To select lines using character ranges, you can do something like this: $ cut -c1-3 weekdays $ cut -f1 addressesħ610 West Park Drive, Hyattsville, MD 20783 If we asked for the first field without specifying a delimiter, we would see entire lines in any file that is not delimited by tabs. The second (delimited by commas) displays all of the text up to the first comma. The first (delimited by blanks) displays the first field. The two command below show different amounts of each line. Just remember that you need to specify the delimiter if the words or strings are not separated by tabs. You can also use the cut command to select single and multiple words or strings from a file. These are, of course, accounts associated with system services. Notice how many accounts cannot log in because they're assigned the /sbin/nologin shell. To count how many accounts use each of the shells, use a command like this: $ cut -d: -f 7 /etc/passwd | sort | uniq -c The command above selects the 1st and 7th fields. To select to see both login names and assigned shells, try this: To specify a different delimiter, you could add the -d option and use a command like this one, which pulls usernames from the /etc/passwd file: $ cut -d: -f 1 /etc/passwd | head -10 The string -f1-4 would display the first four fields in the file: $ cut -f1-4 cities | head -5 Since the tab character is the default delimiter for the cut command, it easily extracts these fields. To add the city names to your selection, you would select the 2nd and 4th fields. To select a particular field from this file, you might use a command like this that shows the 4th field: The lines in this file look something like what is shown below: $ head -5 cities To illustrate how the cut command works, we'll first run commands using a sample "cities" file that contains details of the largest cities in the US in a tab-separated format. Ihnat, David MacKenzie, and Jim Meyering. There is NO WARRANTY, to the extent permitted by law. This is free software: you are free to change and redistribute it. License GPLv3+: GNU GPL version 3 or later. To check on cut, you can ask about its version like this: $ cut -versionĬopyright (C) 2020 Free Software Foundation, Inc. It works something like awk in that it allows you to select only what you want to see from files, enabling you to pull fields (regardless of the delimiter used), characters or bytes. One surprisingly easy command for grabbing a portion of every line in a text file on a Linux system is cut.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |