1、测试数据
[root@centos7 test2]# cat a.txt e d g e s d g w a x d g n d i d [root@centos7 test2]# cat a.txt | sed -n l e d g e$ s d g w$ a x d g$ n d i d$
2、awk
[root@centos7 test2]# cat a.txt e d g e s d g w a x d g n d i d [root@centos7 test2]# awk '{$NF = ""; print $0}' a.txt e d g s d g a x d n d i [root@centos7 test2]# awk '{$NF = ""; print $0}' a.txt | sed -n l e d g $ s d g $ a x d $ n d i $ [root@centos7 test2]# awk '{$NF = ""; print $0}' a.txt | sed 's/.$//' | sed -n l e d g$ s d g$ a x d$ n d i$
3、awk for循环
[root@centos7 test]# cat a.txt i e s m u w e i p q x z u n k e c b v y [root@centos7 test]# awk '{for(i = 1; i < NF; i++) printf("%s ", $i); printf("\n")}' a.txt i e s m w e i p x z u n e c b v [root@centos7 test]# awk '{for(i = 1; i < NF; i++) printf("%s ", $i); printf("\n")}' a.txt | sed -n l i e s m $ w e i p $ x z u n $ e c b v $ [root@centos7 test]# awk '{for(i = 1; i < NF; i++) printf("%s ", $i); printf("\n")}' a.txt | sed 's/.$//' | sed -n l i e s m$ w e i p$ x z u n$ e c b v$