这不是another question的副本。 stackoverflow上发布的所有先前的问题/解决方案都有同样的问题:额外的空间被替换为单个空间。
示例(1.txt)
filename Nospaces
filename One space
filename Two spaces
filename Three spaces
结果:
awk '{$1="";$0=$0;$1=$1}1' 1.txt
One space
Two spaces
Three spaces
awk '{$1=""; print substr($0,2)}' 1.txt
One space
Two spaces
Three spaces
答案 0 :(得分:2)
如果您将字段定义为任意数量的非空格字符,后跟任意数量的 space 字符,那么您可以删除第一个N
之类的字符这样:
$ sed -E 's/([^[:space:]]+[[:space:]]*){1}//' file
Nospaces
One space
Two spaces
Three spaces
将{1}
更改为{N}
,其中N
是要删除的字段数。如果您只想从头开始删除1个字段,则可以完全删除{1}
(以及用于创建组的括号):
sed -E 's/[^[:space:]]+[[:space:]]*//' file
某些版本的sed(例如GNU sed)允许您使用简写:
sed -E 's/(\S+\s*){1}//' file
如果该行的开头可能有一些空格,您可以在该组之外的模式开头添加\s*
(或[[:space:]]*
):
sed -E 's/\s*(\S+\s*){1}//' file
使用awk的问题在于,无论何时触摸给定记录上的任何字段,都会重新格式化整个记录,从而导致每个字段由OFS
(输出字段分隔符)分隔,这是一个单独的默认为空格。如果你愿意,你可以使用awk sub
,但由于这是一个简单的替换,sed是适合这项工作的工具。
答案 1 :(得分:2)
使用IFS
选项指定-F
以避免在awk
awk -F "[ ]" '{$1="";$0=$0;$1=$1}1' 1.txt
awk -F "[ ]" '{$1=""; print substr($0,2)}' 1.txt
答案 2 :(得分:1)
使用cut
:
cut -d' ' -f2- a.txt
打印从秒到最后的所有列并保留空格。
答案 3 :(得分:1)
要保留awk中的空格,您必须使用正则表达式替换或使用子字符串。一旦开始修改单个字段,awk就必须使用定义的(或隐式)OFS重新计算$ 0。
参考汤姆的答案:
awk '{sub(/^([^[:blank:]]+[[:blank:]]+){1}/, "", $0); print}' 1.txt
答案 4 :(得分:-1)
在awk中使用代码,没有前导空格,支持列中的多个空格并从第n列打印:
var today = new Date();
today.setHours(0,0,0,0);
var start_date= new Date();
start_date.setDate(today.getDate() - 5);
start_date.setHours(0,0,0,0);
grid.set('collection', store.filter(
new store.Filter().or(
new store.Filter().gte('datefield1', start_date).lte('datefield1',today),
new store.Filter().gte('datefield2', start_date).lte('datefield2',today)
)
));