Given this data
34 foo
34 bar
34 qux
62 foo1
62 qux
78 qux
I want to replace the string at 2nd column into "" if it is "qux". Resulting:
34 foo
34 bar
34
62 foo1
62
78
How do you do that with sed? In particular the data is very big with ~10^7 lines
From stackoverflow
-
No trailing spaces:
sed 's/qux$//' < fileIf it must be in the second column (of potentially more than three columns):
sed 's/\([0-9][ ]*\)qux\(.*\)/\1\2/'(Note that there is a literal tab and space; sed doesn't match tabs with '\t';
But awk is better for tabular data:
awk '{ if ($2 == "qux") {$2 = ""; print} else { print }; }' < fileojblass : If you want to keep the blank lines sed 's/qux//' < fileojblass : Sorry didn't realize the nubers were a column.... as you were.paxdiablo : That awk solution doesn't change the file (in Cygwin at least).paxdiablo : Never mind, I see the problem, it was the second ==, I've fixed it for ya'.neversaint : @Pax: if I want to do in-place replacement to the file how could I do it? And in AWK how do you print with tab delimited? Now it prints with space delimited.guns : @foolishbrat sed and awk don't do in place edits; you can use perl for that, or super sed (ssed). Otherwise the standard thing to do is use a temporary file.paxdiablo : @foolishbrat, you could pass the output through- that's the fastest way to get a result. paxdiablo : Or there's an OFS variable which is by default a space - set that to a tab. But you shouldn't need to, since $0 preserves the tabs (in my answer - I haven't tested guns'). -
I wouldn't actually do it with
sedsince that's not the best tool for the job. Theawktool is my tool of choice whenever somebody mentions columns.cat file | awk '$2 == "qux" { print $1 } $2 != "qux" { print $0 }'or the simplest form:
cat file | awk '{ if ($2 == "qux") {$2 = ""}; print }'If you must use
sed:cat file | sed 's/ *qux *$//'making sure that you use the correct white space (the above uses only spaces).
neversaint : @Pax: thanks for the reply. BUt yours approach with awk give this instead: 34 bar 34 34 qux 62 foo1 62 62 qux 78 78 quxpaxdiablo : Try again. I had a $s instead of $2.
0 comments:
Post a Comment