Search code examples
bashunixcut

UNIX cut command by tab


Problem: Given a tab delimited file with several columns (tsv format) print the first three fields.

Input Tab seperated file with ASCII text

Output: Contain N lines for each line in the inpu print 1st three fields.

My Code:

while read line; 
do
    echo $line | cut -f-3
done

My Output:

1 New York, New York[10] 8,244,910 1 New York-Northern New Jersey-Long Island, NY-NJ-PA MSA 19,015,900 1 New York-Newark-Bridgeport, NY-NJ-CT-PA CSA 22,214,083
2 Los Angeles, California 3,819,702 2 Los Angeles-Long Beach-Santa Ana, CA MSA 12,944,801 2 Los Angeles-Long Beach-Riverside, CA CSA 18,081,569
3 Chicago, Illinois 2,707,120 3 Chicago-Joliet-Naperville, IL-IN-WI MSA 9,504,753 3 Chicago-Naperville-Michigan City, IL-IN-WI CSA 9,729,825
4 Houston, Texas 2,145,146 4 Dallas-Fort Worth-Arlington, TX MSA 6,526,548 4 Washington-Baltimore-Northern Virginia, DC-MD-VA-WV CSA 8,718,083
5 Philadelphia, Pennsylvania[11] 1,536,471 5 Houston-Sugar Land-Baytown, TX MSA 6,086,538 5 Boston-Worcester-Manchester, MA-RI-NH CSA 7,601,061

Question: I have looked everywhere for a solution for this.


Solution

  • If you are just looking for a solution use the following: cut -d$'\t' -f1-3

    If you are confused on what the flags look here:

    • -d = for delimiter
    • $'\t' = looking for the tab escape character
    • -f1-3 = field number 1 through 3. aka only prints first 3 words.

    For more information look here: Cut Command Linux