You are not logged in.
Pages: 1
The last column of the log file shows the full path to a file and some entries CONTAIN spaces. I want to use sed to substitute the space to an underscore, BUT I only wanna do it from a particular character to the end of the line. How can I accomplish this?
Tue Aug 23 20:38:01 2011 /media/data/test file 1.txt
Wed Aug 24 11:30:01 2011 /media/data/test file 2.txt
Fri Aug 26 17:13:01 2011 /media/data/test file 3.txt
Sun Aug 28 17:16:02 2011 /media/data/test file 4.txtThe effect I'm after it convert the about to the following:
Tue Aug 23 20:38:01 2011 /media/data/test_file_1.txt
Wed Aug 24 11:30:01 2011 /media/data/test_file_2.txt
Fri Aug 26 17:13:01 2011 /media/data/test_file_3.txt
Sun Aug 28 17:16:02 2011 /media/data/test_file_4.txtTried:
sed '/\// s, ,_,' logBut of course, this will change the very first space to an underscore, not the space and all subsequent spaces AFTER the first forward slash.
Last edited by graysky (2011-11-13 23:32:12)
Offline
The fact that you're actually interested in specific fields and not the entire text stream would suggest awk to me.
Offline
I solved my problem with awk, but I still want to know how I can use sed to do it.
awk solution (main reason for wanting to do this was to reorder the date to a standard format which is easily done in awk:
awk '{ print $3"-"$2"-"$5" "$6 $7 $8 $9 $10}' logLast edited by graysky (2011-11-13 23:44:10)
Offline
Remove White Space From Specific Columns In A Text File
How Can I Strip White Space From The Start And End Of Fields Using Awk
Edit: sorry, didn't see your last post before posting.
Last edited by /dev/zero (2011-11-13 23:46:48)
Offline
This may be what you are looking for.
sed 'h; s/[^/]*\///; s/ /_/g; x; s/\/.*/\//; G; s/\n//'
Thanks
Offline
This may be what you are looking for.
sed 'h; s/[^/]*\///; s/ /_/g; x; s/\/.*/\//; G; s/\n//'
Thanks
and thats is why i hate sed. care to explain this crazy language?
Offline
sed 'h; s/[^/]*\///; s/ /_/g; x; s/\/.*/\//; G; s/\n//'
h copy line to hold space
s/[^/]*\/// delete start of line up to first / in pattern space
s/ /_/g replace all spaces with underscores
x exchange hold space and pattern space
s/\/.*/\// delete from first / to end of line
G append hold space to pattern
s/\n// remove auto inserted new line
Offline
Here is another one, it's faster when there are few lines with spaces after the slash, but slower when there are many:
sed ':begin;s/\/.* /&/;T;s/\(\/[^ ]*\) /\1_/;bbegin':begin label
s/\/.* /&/ fake substitution, look for a space after a /
T if it didn't work, go to end of the script (print, next line)
s/\(\/[^ ]*\) /\1_/ change one space to an underscore
bbegin move to label begin
Offline
This works in GNU sed but not BSD seds:
s/ /_/6gOr there's awk if you're not into the whole brevity thing:
# adding onto graysky's last post
{ printf "%s-%s-%s %s", $3, $2, $5, $6; for(i = 7; i <= NF; i++){ printf "_" $i } print "" }
# or maybe
{ printf "%s-%s-%s ", $3, $2, $5; f = substr($0, 25); gsub(/ /, "_", f); print f }edit: removed extra brace
Last edited by juster (2011-11-19 01:41:21)
Offline
This is how I'd do it:
sed -r ":loop
s|^([^/]+ /[^ ]+) |\1_|g
t loop"Offline
This is even smaller.
sed -r ':loop; s|(.*/.*) |\1_|;t loop'Offline
And so is this:
sed -r ":loop;s|(/.*) |\1_|;t loop"Offline
Pages: 1