0

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, [email protected]
51231221, CALVIN, [email protected] 
91234232, LISA, [email protected]
12359432, DICK, [email protected]
94123432, ORAN, [email protected]
63242333, KEVIN, [email protected]

4 Answers 4

2

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}'
Sign up to request clarification or add additional context in comments.

2 Comments

HI, Thank you for the help, This works, but i am not sure of this part,-F',[[:space:]]*' What does it mean, can you please explain to me?
@user3488212 Use a separator like , or , or , . So it removes the unneeded blank spaces. It does mean , and 0 or more spaces after it. (read about regex)
1

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

Comments

1

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.
         ...

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.