tags:

views:

70

answers:

5
+3  A: 

Can you use awk?

As pointed out by wilhelmtell and Colin, replacing every space by a comma is not the right way because it also replaces the spaces in a string like NT AUTHORITY\NETWORK SERVICE

Use this instead

$ cat temp | awk 'BEGIN{FS = ","}{print $1","$8","$10}' | sed 's/"//g'
wmiprvse.exe,NT AUTHORITY\NETWORK SERVICE,N/A

[Old answer]

$ cat temp | awk 'BEGIN{FS = ","}{print $1, $8, $10}' | sed 's/"//g'|
  sed 's/ /,/g'

wmiprvse.exe,NT,AUTHORITY\NETWORK,SERVICE,N/A

1.

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

print column number 1, 8 and 10

2.

sed 's/"//g'

remove "

3.

sed 's/ /,/g'

replace space by a ,

Lazer
The problem with this pipeline is that it removes all quotes and commas, regardless of their context. For instance, this pipeline will corrupt the line `"commas (,)","quotes (\")"`
wilhelmtell
You can change your print into `print $1","$8","$10}` , the second `sed` won't be necessary.
Colin Hebert
This will not work as there is a comma that works as a thousand delimiter. It may or may not be there.
spbfox
thanks for the answer. hmmm, it seems I should learn awk...
x86shadow
+1  A: 

If you can have (escaped) quotes or commas in values then regular expressions are not the right tool for the job. You will need a designated state-machine for this. Your best bet is to write a small script with Ruby, Python, Perl or the likes.

wilhelmtell
+1  A: 

I think you want to use awk for this:

[jkramer/sgi5k:~]# cat foo
"wmiprvse.exe","3596","Console","0","5,632 K","Running","NT AUTHORITY\NETWORK SERVICE","0:00:00","N/A"
[jkramer/sgi5k:~]# awk -F'","|"' '{print $2 "," $8 "," $10}' foo
wmiprvse.exe,NT AUTHORITY\NETWORK SERVICE,N/A
jkramer
A: 

I think the problem is in the thousand-delimiter comma. I would use cut/paste to split the file:

"wmiprvse.exe","3596","Console","0","5,632 K","Running","NT AUTHORITY\NETWORK SERVICE","0:00:00","N/A"

cat myfile | cut -d'"' -f2,14,18 | paste -d','

It cuts fields 2,14 and 18 taking " as a separator and then pastes them together with comma as a separator

spbfox
A: 

Get your *nix tools from GNU win32 here. They are more updated. The more appropriate tool for this job is gawk, not sed. If you look at that massive regular expression, you will understand what i mean.

gawk "{print $1,$7}" file

Note, windows cmd.exe doesn't like double quotes. So if you are printing inside gawk and needs double quotes, always escape them, eg

gawk "{print \"hello\" }" file
ghostdog74