Question

Awk, I am new this this command, I know it can list out the text file with condition, but i have no idea how to list them when there is a "," in between the text, how do you count the "," in as $1.

but if its email, email won't show for some reason, I am thinking maybe I should include the "," ?, i am not sure how to solve the problem, and don't know what the problem is. for example i want to show customerid and customersname, i will use:

awk'{print $1,$2}' 


Customerid, customersname, email
12312322, MIKE, example@gmail.com
51231221, CALVIN, example2@gmail.com 
91234232, LISA, example3@gmail.com
12359432, DICK, example4@gmail.com
94123432, ORAN, example5@gmail.com
63242333, KEVIN, example6@gmail.com
Was it helpful?

Solution

You want to use the comma as separator? Use -F like that:

awk -F, '{print $1,$2}'

If you want comma and spaces as separator you can use a regex:

awk -F',[[:space:]]*' '{print $1,$2}'

OTHER TIPS

I'm not sure whether I got your question properly. You can specifiy the input field separator using the -F command line option:

awk -F, '{print $1, $2}' your.csv

Output:

Customerid  customersname
12312322  MIKE
51231221  CALVIN
91234232  LISA
12359432  DICK
94123432  ORAN
63242333  KEVIN

simply using FS:

awk 'BEGIN { FS="," } {print $1,$2}'

from man awk:

7. Builtin-variables

   The following variables are built-in and initialized before program execution.    
         ...
          FS        splits records into fields as a regular expression.
         ...

Here is the code needed

awk -F "," '{print $1,$2}' input.txt

Output:

Customerid, customersname
12312322, MIKE
51231221, CALVIN
91234232, LISA
12359432, DICK
94123432, ORAN
63242333, KEVIN

Explanation:

-F = Field separator

"," = using comma because columns are separated by ,

'{print $1,$2}' = display first and second column

input.txt = the file you want to pass

Hope its help.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top