1

I have the below file in txt:

"Firstname","Lastname","Company"
"Alex","Johnson","Alex Corp."
"Baran","Atasoy","Gülerler Ltd."

I would like to extract and format data by using awk and/or cut and have the format below:

Firstname Lastname
Alex Johnson
Baran Atasoy

How can I achieve this?

4

Use awk to split the fields on the , (-F,) and substitute out the " (gsub):

awk -F, '{gsub(/"/,""); print $1,$2}' file
Firstname Lastname
Alex Johnson
Baran Atasoy
3

You could assign multiplay field separator by -F

awk -F[\",] '{print $2,$5}

Same with sed

sed 's/"\|,[^,]\+$//g;s/,/ /'

or

sed -E 's/"(\w+)","(\w+).*/\1 \2/'

Same with tr and cut

tr -s ',"' ' ' | cut -d" " -f2-3

At last but not a last: just bash builtin's

while IFS=, read a b c 
do
  echo "${a//\"/} ${b//\"/}"
done

Or may be more complicable

while IFS=[,\"] read a b c d f g
do
  echo $b $f
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.