-2

Consider the following file

col1 col2 col3 col4 col5
3 5 "Hello World" 2 8
20 NA "Alice" 1 6
1 1 "A B C" 1 154

I would like select the first three columns. The expected output is

col1 col2 col3
3 5 "Hello World"
20 NA "Alice"
1 1 "A B C"

I naively tried

$cut -d" " -f -3 myFile.txt
col1 col2 col3
3 5 "Hello
20 NA "Alice"
1 1 "A

I am having difficulties due to the spaces in between " ". How can I cut ignoring the delimiters found in between " "?

Remi.b
  • 17,389
  • 28
  • 87
  • 168
  • Also see: https://stackoverflow.com/q/29642102/499581, https://stackoverflow.com/q/6619619/499581, https://stackoverflow.com/q/26262265/499581. – l'L'l Nov 14 '17 at 19:36

1 Answers1

-1

Probably not the most elegant solution but here is what I came up with

cat myfile | awk '
{
    split($0, chars, "")
    nbQuotes=0
    nbColumns=1
    for (i=1 ; i <= length($0); i++)
    {
        if (chars[i] == "\"")
        {
            nbQuotes++
        } else
        {
            if (nbQuotes%2 == 0)
            {
                if (chars[i] == " ")
                {
                    nbColumns++
                }
            }
        }
        if (nbColumns > 3)
        {
            break
        }
        printf chars[i]
    }
    print ""
}
'

col1 col2 col3
3 5 "Hello World"
20 NA "Alice"
1 1 "A B C"
Remi.b
  • 17,389
  • 28
  • 87
  • 168