分享两道linux基础题

回复 收藏

题目1:请把如下字符串

stu494

e222f

stu495

bedf3

stu496

92236

stu497

49b91

转为如下形式:

stu494=e222f

stu495=bedf3

stu496=92236

stu497=49b91

题目2:echo I am oldboy training,请把这个输出中的每个字符去重后排序,重复次数多的排到前面。

=======

答案:

1.

[root@centos ~/test]# cat old 

stu494

e222f

stu495

bedf3

stu496

92236

stu497

49b91

[root@centos ~/test]# sed 'N;s/\n/=/g' old 

stu494=e222f

stu495=bedf3

stu496=92236

stu497=49b91

[root@centos ~/test]# 

2.

[root@centos ~/test]# echo I am oldboy training|grep -o '.'|sort|uniq|sort -r

[root@centos ~/test]# 

2016-09-26 17:13 举报
已邀请:
0

xjpans

赞同来自:

多谢

0

GaryHuang0113 - 世上无难事,只怕有心人

赞同来自:

awk '{if (NR%2==1){str=$1}else{print str"="$1}}' 1.txt

0

GaryHuang0113 - 世上无难事,只怕有心人

赞同来自:

echo I am oldboy training | grep -o '.' | sort | uniq -c | sort -r

0

ggangelo

赞同来自:

第一题,如果行与行之间有空行的话。用这个。sed 'N;N;s/\n\n/=/;n' a.txt

0

ggangelo

赞同来自:

awk 'BEGIN{info="I am oldboy training";split(info,s,"");for(k in s) {
c[s[k]]++};for(b in c){print c,b}}'|sort -rk1

我只是来试下awk的。。好长。。

0

ggangelo

赞同来自:

{print c【b】,b} 这个居然不出来。。

0

u46306306

赞同来自:

awk '{printf "%s=",$0;getline;print;}' 1.txt

0

Youcan

赞同来自:

踩踩

0

邓先辉

赞同来自:

学习了哈

i_f48.gif

0

Coohx - 小运维

赞同来自:

玩了一下第二个题,贡献两个答案:

echo "I am old boy training." | grep -o '\w' | sort | uniq -c |sort -rn

echo "I am old boy training." | grep -o '[a-zA-Z]' | sort | uniq -c | sort -rn

0

卯亮

赞同来自:

学到了。。。

0

左左

赞同来自:

学到了

0

吼吼

赞同来自:

已经收藏了呢 。多谢

回复帖子,请先登录注册

退出全屏模式 全屏模式 回复
评分
  • 评分区间
  • 学分
  • -30 ~ 30
可选评分理由: