Reputation: 1064
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
Upvotes: 0
Views: 94
Reputation: 1064
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.)
Upvotes: 1
Reputation: 920
You can use awk to do this very simply:
echo -e "1 New York, New York[10] 8,244,910\t1 New York-Northern New Jersey-Long Island, NY-NJ-PA MSA 19,015,900\t1 New York-Newark-Bridgeport, NY-NJ-CT-PA CSA 22,214,083" | awk -F '\t' ' { print $1 } '
Output:
1 New York, New York[10] 8,244,910
Upvotes: 1