参考文章:http://blog.csdn.net/chen_jp/article/details/8922582

一 字符替换

origin=原字符串  str=替换后的字符串

替换命令:

str=${origin//目标字符/替换后的字符}

例如:

str=${origin//:/_}

代码示例:

origin='mark:x:0:0:this is a test user:/var/mark:nologin'
str=${origin//:/_}
echo ${origin}
echo ${str}

输出:

mark:x:::this is a test user:/var/mark:nologin
mark_x_0_0_this is a test user_/var/mark_nologin

二 字符串分割

origin='mark:x:0:0:this is a test user:/var/mark:nologin'
i=
while((==))
do
split=`echo $origin|cut -d ":" -f$i`
if [ "$split" != "" ]
then
((i++))
echo $split
else
break
fi
done

输出:

mark
x this is a test user
/var/mark
nologin

Shell 字符串常见操作的更多相关文章

  1. python字符串常见操作

    字符串常见操作 如有字符串mystr = 'hello world itcast and itcastcpp',以下是常见的操作 <1>find 检测 str 是否包含在 mystr中,如 ...

  2. python基础学习-字符串常见操作

    字符串常见操作 索引 s = "abcdefg" # 字符串数据,切片后取出的数据都是字符串类型 # 从左至右取值:从0开始 # 从右向左取值:从-1开始 print(" ...

  3. linux shell 字符串常用操作

    1.shell内置的字符串操作 表达式 含义 ${#string} $string的长度 ${string:position} string中,从位置$position开始提取字符串 ${string ...

  4. Python中字符串常见操作

    (1)find 查找 格式:mystr.find(str, start, end) 例如: mystr.find(str, start=0, end=len(mystr)) 作用:检测str是否包含在 ...

  5. python中字符串常见操作(二)

    # 可迭代对象有:字典,列表,元组,字符串,集合 str1 = '192.168.1.1' str2 = 'as df gh jk' str3 = '小李子' str4 = ['aa','bb','c ...

  6. JAVA中字符串常见操作

    String str1="hello,world";String str2="Hello,World"; 1.字符串的比较:例,System.out.print ...

  7. Go 语言字符串常见操作

    @ 目录 1. 字节数组 2. 头尾处理 3. 位置索引 4. 替换 5. 统计次数 6. 重复 7. 大小写 8. 去除字符 9. 字符串切片处理 10. 数值处理 1. 字节数组 字节与字符的区别 ...

  8. C#字符串常见操作总结

    string类常用的方法和总结小记

  9. Java Script中常见操作

    字符串常见操作:obj.length 长度obj.trim() 移除空白obj.trimLeft()obj.trimRight)obj.charAt(n) 返回字符串中的第n个字符obj.concat ...

随机推荐

  1. 计算1至n中数字X出现的次数

    描述 计算 1 至 n 中数字 X 出现的次数,其中 $n \ge 1,X \in [0,9]$. 解题思路 这是一道比较简单的题目,举个例子先:假设 $n=11, X=1$,那么就是求 1, 2, ...

  2. A Walk Through the Forest[HDU1142]

    A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  3. [leetCode][003] Intersection of Two Linked Lists

    [题目]: Write a program to find the node at which the intersection of two singly linked lists begins. ...

  4. 【BZOJ】1055: [HAOI2008]玩具取名(dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1055 我竟然都没往dp这个方向想.....百度了下看到标题是dp马上就会转移了QAQ... 设d[i ...

  5. POJ 3691 DNA repair(AC自动机+DP)

    题目链接 能AC还是很开心的...此题没有POJ2778那么难,那个题还需要矩阵乘法,两个题有点相似的. 做题之前,把2778代码重新看了一下,回忆一下当时做题的思路,回忆AC自动机是干嘛的... 状 ...

  6. OI优化开关

    #pragma comment(linker,"/STACK:10240000,10240000")#pragma GCC optimize ("O2")

  7. Google Code Jam 2010 Round 1C Problem B. Load Testing

    https://code.google.com/codejam/contest/619102/dashboard#s=p1&a=1 Problem Now that you have won ...

  8. FileStorage Read String Start With Number Need Quotation Mark 读取数字开头的字符串需要加引号

    // Write data FileStorage fs("test.yml", FileStorage::WRITE); fs << "MyString&q ...

  9. 提取数据用strpos函数比较,预期和实际不符问题解决

    在我提取数据时,数据是一串字符串,第一个数据和要比较的字符是相等的可是却是相反的结果 . 测试if(0==false)结果如图 执行结果 说明0和false相等.我的程序开始是这样的 第一个数据是正确 ...

  10. Sublime之旅

    安装 http://www.sublimetext.com/3          常用操作  window版本 CTRL + P 打开文件搜索 Ctrl+K+B 打开目录树 Ctrl+Shift+[ ...