[ SHELL编程 ] 字符串空格和文件空行删除
1、删除字符串中空格
(1)删除行首空格
echo " 123 45 " | sed -e 's/^[ \t]*//g' #输出
(2)删除行尾空格
echo " 123 45 " | sed 's/[ \t]*$//g' #输出
(3)删除前、后空格,不删除中间空格
echo " 123 45 " | sed -e 's/^[ \t]*//g' -e 's/[ \t]*$//g' #输出
(4) 删除字符串中所有空格
echo " 123 567 " | sed 's/[[:space:]]//g' #输出12345
echo " 123 567 " | tr -d '[ \t]' #输出12345
2、删除文件中的空行
grep -v '^$' filename #打印非空行
sed '/^$/d' filename #打印非空行
sed -i '/^$/d' filename #原地置换,原文件删除空行
awk '!/^$/{print}' filename #打印非空行
3、实例
需求:删除文件中行首、行尾空格和文件中的空行
方法一
sed -i -e 's/^[ \t]*//g' -e 's/[ \t]*$//g' -e '/^$/d' filename
方法二
sed -i 's/^[ \t]*//g;s/[ \t]*$//g;/^$/d' filename
[ SHELL编程 ] 字符串空格和文件空行删除的更多相关文章
- shell编程基础(七): 处理文件命令sed与awk
一.sed(以行为单位处理文件) sed意为流编辑器(Stream Editor),在Shell脚本和Makefile中作为过滤器使用非常普遍,也就是把前一个程序的输出引入sed的输入,经过一系列编辑 ...
- Linux Shell编程第5章——文件的排序、合并和分割
目录 sort命令 sort命令的基本用法 uniq命令 join命令 cut命令 paste命令 split命令 tr命令 tar命令 sort命令 sort命令是Linux系统一种排序工具,它将输 ...
- [ SHELL编程 ] 远程服务器传输文件
在shell编程中经常需要获取远程服务器文件.手工操作中使用scp命令完成.为避免脚本执行scp输入密码进行交互,需先建立本机服务器当前用户和远程服务器指定用户的信任关系.具体代码见操作实例,重点关注 ...
- linux下带有空格的文件怎么删除
如:hello world文件 第一种方式 先用 ls -i 得到 hello world 的inod(就是最前面的数字)假设这个数字是123,然后find . -inum -exec rm {} \ ...
- windows编程之Windows Shell 编程
参考书<VC++ Windows Shell Programming> 这里仅仅是记录下该资源,推荐到下文列出的连接进行查看 用VC++ 进行Windows Shell 扩展编成 ...
- (一)Linux Shell编程——简介、变量、字符串、数组
1. Shell简介 1.1 Shell出现背景 Shell 既是一种脚本编程语言,也是一个连接内核和用户的软件. 对于图形界面,用户点击某个图标就能启动某个程序:对于命令行,用户输入某个程序的名字( ...
- shell编程系列11--文本处理三剑客之sed利用sed删除文本中的内容
shell编程系列11--文本处理三剑客之sed利用sed删除文本中的内容 删除命令对照表 命令 含义 1d 删除第一行内容 ,10d 删除1行到10行的内容 ,+5d 删除10行到16行的内容 /p ...
- Linux shell编程02 shell程序的执行 及文件权限
第一个shell脚本 1. shell编程的方式 交互式shell编程 非交互式shell编程:执行的语句存放到一个文件 shell脚本:可以任意文件名,建议扩展名为sh 2. ...
- Linux编程 24 shell编程(结构化 if [ condition ] 数值比较,字符串比较)
一.概述 接着上篇讲的结构化命令,最后讲到了test命令的另一种写法 if [ condition ],它的语法格式如下: --格式如下: if [ condition ] then commands ...
随机推荐
- 【Nim游戏】高僧斗法
先来看看Nim定理: // 若干堆硬币,二人轮流取,从一堆硬币中取几个 直到某个人不能取硬币 那这个人就输了 // 3 4 5 // 3 3 把硬币变成相同的 那么你就赢了 因为你可以跟着另一个人一样 ...
- &和&&的共同点和区别、Java字符含义和Java创建对象的几种方式
一.&和&&的共同点和区别 1.&和&&的联系(共同点): &和&&都可以用作逻辑与运算符,但是要看使用时的具体条件来决定. 操 ...
- 模仿bootstrap做的 js tooltip (添加鼠标跟随功能)
主要思路: 使用jquery hover方法,当进入时显示tooltip,移出时隐藏tooltip当设定为鼠标跟随时,使用mousemove事件显示tooltip根据tooltip显示位置设置,计算t ...
- [Swift]LeetCode57. 插入区间 | Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- [Swift]LeetCode100. 相同的树 | Same Tree
Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...
- [Swift]LeetCode350. 两个数组的交集 II | Intersection of Two Arrays II
Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...
- [Swift]LeetCode409. 最长回文串 | Longest Palindrome
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- [Swift]LeetCode803. 打砖块 | Bricks Falling When Hit
We have a grid of 1s and 0s; the 1s in a cell represent bricks. A brick will not drop if and only i ...
- [Swift]LeetCode1018. 可被 5 整除的二进制前缀 | Binary Prefix Divisible By 5
Given an array A of 0s and 1s, consider N_i: the i-th subarray from A[0] to A[i] interpreted as a bi ...
- Hive篇--搭建Hive集群
一.前述 Hive中搭建分为三中方式 a)内嵌Derby方式 b)Local方式 c)Remote方式 三种方式归根到底就是元数据的存储位置不一样. 二.具体实现 a)内嵌Derby方式 使用derb ...