avatar
d*y
1
Hi ,
I want to use sed on Unix to do this:
1. I have a string called tmp="12 23 34"
2. I want to replace all the space in this string with a new line and
dump it to a file
3. finally this file looks like this: 12
23
34
I tried this: print $tmp | sed 's/ /\n/g' it didn't work, so somebody
help????
Thanks
avatar
r*s
2
你没有认识到sed的机制,每次读一行,在行内进行一些替换删除操作,
再读入下一行。我先给个awk的范例吧。UNIX总是有多解的,大家来想啊~

【在 d*****y 的大作中提到】
: Hi ,
: I want to use sed on Unix to do this:
: 1. I have a string called tmp="12 23 34"
: 2. I want to replace all the space in this string with a new line and
: dump it to a file
: 3. finally this file looks like this: 12
: 23
: 34
: I tried this: print $tmp | sed 's/ /\n/g' it didn't work, so somebody
: help????

avatar
r*s
3
[7:06pm] cat t
12 34 56
[7:06pm] cat xixi.awk
BEGIN {FS=" "}
{
x1=$1
x2=$2
x3=$3
printf("%s\n", x1)
printf("%s\n", x2)
printf("%s\n", x3)
}
[7:06pm] awk -f xixi.awk t
12
34
56
[7:06pm]

【在 r********s 的大作中提到】
: 你没有认识到sed的机制,每次读一行,在行内进行一些替换删除操作,
: 再读入下一行。我先给个awk的范例吧。UNIX总是有多解的,大家来想啊~

avatar
r*s
4
下面这个用cut命令:
[8:35pm] cat t
12 34 56
[8:36pm] cut -d " " -f1 t ; cut -d " " -f2 t ; cut -d " " -f3 t
12
34
56
[8:36pm]
可以写一个script来偷懒:
# shell script to replace space with new line
#!/bin/sh
echo -n "Enter the input file name: "
read input
cut -d " " -f1 $input; cut -d " " -f2 $input; cut -d " " -f3 $input
以下为运行结果:
[8:44pm] sh xixi
Enter the input file name: t
12
34
56
[8:46pm]
avatar
r*s
5
cool. by 'man tr', I learn sth about translate lowercase to uppercase.
[11:52pm] cat t
abcd
[11:52pm] cat t | tr -s '[:lower:]' '[:upper:]'
ABCD
avatar
d*y
6
Thanks to rhinoceros and killme, your comments are very helpful.
Actually I also tried this and it works well:
print $tmp | tr -s ' ' '[\012*]'
have a nice day
- danny

【在 r********s 的大作中提到】
: cool. by 'man tr', I learn sth about translate lowercase to uppercase.
: [11:52pm] cat t
: abcd
: [11:52pm] cat t | tr -s '[:lower:]' '[:upper:]'
: ABCD

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。