d*****y 发帖数: 12 | 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
|
r********s 发帖数: 179 | 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????
|
r********s 发帖数: 179 | 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总是有多解的,大家来想啊~
|
r********s 发帖数: 179 | 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] |
r********s 发帖数: 179 | 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 |
d*****y 发帖数: 12 | 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
|