One surprisingly easy command for grabbing a portion of every line in a text file on a Linux system is cut. 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. To check on cut, you can ask about its version like this:\n$ cut --version\ncut (GNU coreutils) 8.32\nCopyright (C) 2020 Free Software Foundation, Inc.\nLicense GPLv3+: GNU GPL version 3 or later .\nThis is free software: you are free to change and redistribute it.\nThere is NO WARRANTY, to the extent permitted by law.\n\nWritten by David M. Ihnat, David MacKenzie, and Jim Meyering.\n\nSelecting by field\nTo 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. The lines in this file look something like what is shown below:\n$ head -5 cities\nRank Name State 2021 Pop. 2010 Census Change Density (mi\u00b2) Area (mi\u00b2)\n1 New York City New York 8,230,290 8,190,210 4,083 300\n2 Los Angeles California 3,983,540 3,795,510 1,266 469\n3 Chicago Illinois 2,679,080 2,697,480 1,756 227\n4 Houston Texas 2,323,660 2,100,280 541 640\n\nTo select a particular field from this file, you might use a command like this that shows the 4th field:\n$ cut -f 4 cities | head -11\n2021 Pop.\n8,230,290\n3,983,540\n2,679,080\n2,323,660\n1,733,630\n1,585,010\n1,581,730\n1,427,720\n1,347,120\n1,011,790\n\nTo add the city names to your selection, you would select the 2nd and 4th fields. Since the tab character is the default delimiter for the cut command, it easily extracts these fields.\n$ cut -f2,4 cities | head -11\nName 2021 Pop.\nNew York City 8,230,290\nLos Angeles 3,983,540\nChicago 2,679,080\nHouston 2,323,660\nPhoenix 1,733,630\nPhiladelphia 1,585,010\nSan Antonio 1,581,730\nSan Diego 1,427,720\nDallas 1,347,120\nAustin 1,011,790\n\nThe string -f1-4 would display the first four fields in the file:\n$ cut -f1-4 cities | head -5\nRank Name State 2021 Pop.\n1 New York City New York 8,230,290\n2 Los Angeles California 3,983,540\n3 Chicago Illinois 2,679,080\n4 Houston Texas 2,323,660\n\nTo 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:\n$ cut -d: -f 1 \/etc\/passwd | head -10\nroot\nbin\ndaemon\nadm\nlp\nsync\nshutdown\nhalt\nmail\noperator\n\nTo select to see both login names and assigned shells, try this:\n$ cut -d: -f 1,7 \/etc\/passwd | head -10\nroot:\/bin\/bash\nbin:\/sbin\/nologin\ndaemon:\/sbin\/nologin\nadm:\/sbin\/nologin\nlp:\/sbin\/nologin\nsync:\/bin\/sync\nshutdown:\/sbin\/shutdown\nhalt:\/sbin\/halt\nmail:\/sbin\/nologin\noperator:\/sbin\/nologin\n\nThe command above selects the 1st and 7th fields.\nTo count how many accounts use each of the shells, use a command like this:\n$ cut -d: -f 7 \/etc\/passwd | sort | uniq -c\n 17 \/bin\/bash\n 1 \/bin\/sync\n 1 \/sbin\/halt\n 44 \/sbin\/nologin\n 1 \/sbin\/shutdown\n 1 \/usr\/sbin\/nologin\n\nNotice how many accounts cannot log in because they're assigned the \/sbin\/nologin shell. These are, of course, accounts associated with system services.\nSelecting by words\nYou can also use the cut command to select single and multiple words or strings from a file. Just remember that you need to specify the delimiter if the words or strings are not separated by tabs. The two command below show different amounts of each line. The first (delimited by blanks) displays the first field. The second (delimited by commas) displays all of the text up to the first comma.\n$ cut -d' ' -f1 addresses\n7610\n6803\n1089\n3833\n$ cut -d, -f1 addresses\n7610 West Park Drive\n6803 Gravel Branch Rd\n1089 Plymouth Drive\n3833 Abingdon Circle\n\nIf we asked for the first field without specifying a delimiter, we would see entire lines in any file that is not delimited by tabs.\n$ cut -f1 addresses\n7610 West Park Drive, Hyattsville, MD 20783\n6803 Gravel Branch Rd, Hurlock, MD 21643\n1089 Plymouth Drive, Rahway, NJ 07065\n3833 Abingdon Circle, Norfolk, VA 23513\n\nSelecting by characters\nTo select lines using character ranges, you can do something like this:\n$ cut -c1-3 weekdays\nSun\nMon\nTue\nWed\nThu\nFri\nSat\n\nThis displays the first three letters of each line of a file that lists the days of the week.\nSelecting by bytes\nYou can ask cut to select by bytes. Unless your data file includes characters that occupy more than a single byte, you would not see any differences. In this example, we might see a difference simply because the \u00a3 sign occupies two bytes.\n$ cut -b1-23 cost\nThat biscuit cost me 2\u2592\n$ cut -c1-23 cost\nThat biscuit cost me 2\u00a3\n\nIn the first command above, the response shows show a block of dots because it's looking only at the first byte of the \u00a3 sign. In the second, we select by character, so it uses both bytes. We could also have just done this and added one more byte:\n$ cut -b1-24 cost\nThat biscuit cost me 2\u00a3\n\nReversing your request\nYou can also select an option to reverse the output from your cut request. This doesn't mean displaying it in reverse order, but means "doing the opposite". Selecting the first four characters from a file is one thing. Select everything but those characters is its "complement". Here's an example:\n$ cut -b1-4 addresses\n7610\n6803\n1089\n3833\n$ cut --complement -b1-4 addresses\n West Park Drive, Hyattsville, MD 20783\n Gravel Branch Rd, Hurlock, MD 21643\n Plymouth Drive, Rahway, NJ 07065\n Abingdon Circle, Norfolk, VA 23513\n\nWrap-up\nThe cut command offers a lot of flexibility for selecting portions of each line in a file. Consult the man page for more information on its many options.