0

I have those line

Siouxsie Sioux  Siouxsie Sioux  ...     
Sylvia Tobias   Sylvia Tobias   ...     
Sydney Urshan   Sydney Urshan   ... 
Club Drummer (uncredited)   Club Drummer (uncredited)   ...

With awk i print first column tab separated and i add a ,

awk -F "\t" '{print $1","}'

is ok,but i see blank space at end so i use sed

awk -F "\t" '{print $1","}'|sed -r 's/(^[^[:blank:]]+[[:blank:]]+[^[:blank:]]+)[[:blank:]]+/\1/'

With this is ok,but remove also the second white space,so

Club Drummer (uncredited) ,

become

Club Drummer(uncredited),

i want

Club Drummer (uncredited),

So how to remove the last space only? Thanks

3
awk -F ' *\t' '{print $1 ","}'

Or:

sed $'s/ *\t.*/,/'
2

Strip those trailing spaces using awk itself

awk -F'\t' '{sub(/[[:blank:]]+$/, "", $1);print $1","}' file
2

You can use printf instead of using print to get around this problem.

$ awk '{for (i=1;i<NF;i++)printf("%s ", $i);printf("\b%s\n",",")}' file.txt 
Siouxsie Sioux Siouxsie Sioux,
Sylvia Tobias Sylvia Tobias,
Sydney Urshan Sydney Urshan,
Club Drummer (uncredited) Club Drummer (uncredited),

Or if you need it with sed

$ sed -re 's/ +\.\.+/,/g' file.txt 
Siouxsie Sioux  Siouxsie Sioux,     
Sylvia Tobias   Sylvia Tobias,     
Sydney Urshan   Sydney Urshan, 
Club Drummer (uncredited)   Club Drummer (uncredited),
-1

I think the whole operation could be completed using sed 's/^\([^\t]*\).*/\1,/'...

  • Why have I been downvoted? – guihuy Nov 27 '14 at 19:49

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.