Search code examples
bashshellcut

Bash: splitting items from a definite column


  • I have a file of tsv format containing the line ->

    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
    
  • It has 3 tab delimited columns. Which are ->

    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

  • I want the first 4 comma (',') seperated elements of the first column, which is ->

    1 New York, New York[10] 8,244,910

  • My approach is cut -d',' -f1-4 and in this case, my approach gives output like ->

    1 New York, New York[10] 8,244,910 1 New York-Northern New Jersey-Long Island

How can I fix this??

Original problem link : https://www.hackerrank.com/challenges/text-processing-cut-5?h_r=next-challenge&h_v=zen


Solution

  • cut -f-2 <- The Answer of my question

    cut -f-3 <- The answer to the HackerRank Problem, as it has some indentation problem

    (I'm just splitting by the tab and taking the first two parts.)