views:

2010

answers:

7

Hi all, I need to edit few txt files (an output from sar) and to convert them into csv files. therefore I need to change every white space (or maybe it's a tab between the numbers in the output) using sed or awk functions (an easy shell script in linux)

can anyone help me? every command I used didn't change the file at all (I tried gsub)

thanks! :)

+3  A: 

without looking at your input file, only a guess

awk '{$1=$1}1' OFS=","

redirect to another file and rename as needed

ghostdog74
I assume the final 1 after the closing curly brace is an always-true pattern that prints the line? I'd go with the more readable `{$1=$1; print}`.
ΤΖΩΤΖΙΟΥ
yes. its an awk idiom for true condition which default prints to stdout.
ghostdog74
+2  A: 

Hi,

What about something like this :

cat texte.txt | sed -e 's/\s/,/g' > texte-new.txt

(Yes, with some useless catting and piping ; could also use < to read from the file directly, I suppose -- used cat first to output the content of the file, and only after, I added sed to my command-line)

EDIT : as @ghostdog74 pointed out in a comment, there's definitly no need for thet cat/pipe ; you can give the name of the file to sed :

sed -e 's/\s/,/g' texte.txt > texte-new.txt

If "texte.txt" is this way :

$ cat texte.txt
this is a text
in which I want to replace
spaces by commas

You'll get a "texte-new.txt" that'll look like this :

$ cat texte-new.txt
this,is,a,text
in,which,I,want,to,replace
spaces,by,commas

I wouldn't go just replacing the old file by the new one (could be done with sed -i, if I remember correctly ; and as @ghostdog74 said, this one would accept creating the backup on the fly) : keeping might be wise, as a security measure (even if it means having to rename it to something like "texte-backup.txt")

Pascal MARTIN
no need to cat. sed -e 's/\s/,/g' teste.txt. Also, if using GNU sed, you can use -i.bak
ghostdog74
Yep, I edited my answer while you where posting your comment, to say about -i (even though I'd recommend not using it, to keep a backup of the file -- that can always be useful) ; didn't think about sed myfile.txt, though ; good point, thanks!
Pascal MARTIN
+1  A: 

This command should work:

sed "s/\s/,/g" < infile.txt > outfile.txt

Note that you have to redirect the output to a new file. The input file is not changed in place.

Dawie Strauss
+1  A: 

sed can do this:

sed 's/[\t ]/,/g' input.file

That will send to the console,

sed -i 's/[\t ]/,/g' input.file

will edit the file in-place

ezpz
+4  A: 

Try something like:

sed 's/[:space:]+/,/g' orig.txt > modified.txt

The character class [:space:] will match all whitespace (spaces, tabs, etc.). If you just want to replace a single character, eg. just space, use that only.

EDIT: Actually [:space:] includes carriage return, so this may not do what you want. The following will replace tabs and spaces.

sed 's/[:blank:]+/,/g' orig.txt > modified.txt

as will

sed 's/[\t ]+/,/g' orig.txt > modified.txt

In all of this, you need to be careful that the items in your file that are separated by whitespace don't contain their own whitespace that you want to keep, eg. two words.

dave
isn't sed a line-oriented tool? If so, it should not matter that \n is included in [:space:]
glenn jackman
GNU sed requires this syntax: sed 's/[[:space:]]\+/,/g' filename
glenn jackman
+3  A: 
tr ' ' ',' <input >output

Substitutes each space with a comma, if you need you can make a pass with the -s flag (squeeze repeats), that replaces each input sequence of a repeated character that is listed in SET1 (the blank space) with a single occurrence of that character.

Use of squeeze repeats used to after substitute tabs:

tr -s '\t' <input | tr '\t' ',' >output
Alberto Zaccagni
+1  A: 

WOW! thanks all, you were very helpful :) have a great weekend...