分页: 1 / 1

菜鸟求助:怎样编辑匹配行的上一行?

发表于 : 2014-05-17 22:02
youzhiyili
匹配以http开头的行:^http
可是我想在它上面那一行的行首插入字符,不知道怎么弄 :em06

Re: 菜鸟求助:怎样编辑匹配行的上一行?

发表于 : 2014-05-17 23:13
nae6taiyie0T
你要干嘛?

可以的话, 把你要匹配的文件帖出来, 看看你需要修改哪些字符?

Re: 菜鸟求助:怎样编辑匹配行的上一行?

发表于 : 2014-05-18 0:10
youzhiyili
nae6taiyie0T 写了:你要干嘛?

可以的话, 把你要匹配的文件帖出来, 看看你需要修改哪些字符?
如图,我希望在每个标题前添加这个符号:

代码: 全选

rss_cnBeta(){ # cnBeta RSS函数

		RSS_cnBeta_tmp1=/tmp/rsscnBeta.tmp1

curl -s "http://cnbeta.feedsportal.com/c/34306/f/624776/index.rss" \
|sed -e 's/<title>/\n\n\n<title>/g' \
  -e 's/<link>/\n<link>/g' \
  -e 's/<description>/\n<description>/g' \
  -e 's/\&lt\;/\n\&lt\;/g'\
|sed -e '/^\&lt\;/d' \
  -e 's/<title>//g' \
  -e 's/<\/title>//g' \
  -e 's/<link>//g' \
  -e 's/<\/link>//g' \
  -e 's/<description>//g'\
|tail -n +8\
|sed -e 's/^http/----http/g' -e 's/&//g' >$RSS_cnBeta_tmp1

whiptail --title "cnBeta_RSS" --textbox $RSS_cnBeta_tmp1 0 0 && rm $RSS_cnBeta_tmp1
}
rss_cnBeta
trap 'rm /tmp/rss*;exit' 2
1.png

Re: 菜鸟求助:怎样编辑匹配行的上一行?

发表于 : 2014-05-18 0:16
astolia
用awk吧

代码: 全选

$ cat a.awk 
#!/usr/bin/gawk -f

BEGIN {
	t = 0
}
{
	if (match($0, /^http/) == 0) {
		if (t != 0) {
			print line
		}
	} else {
		if (t != 0) {
			print "prefix" line
		}
	}
	line = $0
	t = 1
}
END {
	if (t == 1) {
		print line
	}
}

代码: 全选

$ cat text.txt 
1 2 3 5
http://a.com
3     3
http://b.com
1

1
http://c.com
http://d.com
2

代码: 全选

$ ./a.awk text.txt 
prefix1 2 3 5
http://a.com
prefix3     3
http://b.com
1

prefix1
prefixhttp://c.com
http://d.com
2

Re: 菜鸟求助:怎样编辑匹配行的上一行?

发表于 : 2014-05-18 0:17
YeLee
覚得没有必要作后期替换吧,前期就把<title>匹配好的话,这样会简単很多的。 :em01

Re: 菜鸟求助:怎样编辑匹配行的上一行?

发表于 : 2014-05-18 1:20
youzhiyili
YeLee 写了:覚得没有必要作后期替换吧,前期就把<title>匹配好的话,这样会简単很多的。 :em01
这主意好,简单方便,已经搞定 :em11

代码: 全选

rss_cnBeta(){ # cnBeta RSS函数
		RSS_cnBeta_tmp1=/tmp/rsscnBeta.tmp1
curl -s "http://cnbeta.feedsportal.com/c/34306/f/624776/index.rss" |sed -e 's/<title>/\n\n\n●<title>/g' -e 's/<link>/\n<link>/g' -e 's/<description>/\n<description>/g' -e 's/\&lt\;/\n\&lt\;/g'|sed -e '/^\&lt\;/d' -e 's/<title>//g' -e 's/<\/title>//g' -e 's/<link>//g' -e 's/<\/link>//g' -e 's/<description>//g'|tail -n +8|sed -e 's/^http/----http/g' -e 's/&//g' >$RSS_cnBeta_tmp1
whiptail --title "cnBeta_RSS" --textbox $RSS_cnBeta_tmp1 0 0 && rm $RSS_cnBeta_tmp1
}
不过,我还是特别想知道在不可避免的情况下,怎样解决这个问题

Re: 菜鸟求助:怎样编辑匹配行的上一行?

发表于 : 2014-05-18 1:21
youzhiyili
astolia 写了:用awk吧

代码: 全选

$ cat a.awk 
#!/usr/bin/gawk -f

BEGIN {
	t = 0
}
{
	if (match($0, /^http/) == 0) {
		if (t != 0) {
			print line
		}
	} else {
		if (t != 0) {
			print "prefix" line
		}
	}
	line = $0
	t = 1
}
END {
	if (t == 1) {
		print line
	}
}

代码: 全选

$ cat text.txt 
1 2 3 5
http://a.com
3     3
http://b.com
1

1
http://c.com
http://d.com
2

代码: 全选

$ ./a.awk text.txt 
prefix1 2 3 5
http://a.com
prefix3     3
http://b.com
1

prefix1
prefixhttp://c.com
http://d.com
2
非常感谢前辈的教诲,菜鸟学习了 :em11

Re: 菜鸟求助:怎样编辑匹配行的上一行?

发表于 : 2014-05-18 11:21
ubunbates
sed -n '/^http/{x;s/^/你的符号/;x;};x;p;${x;p;}' 你的文件

Re: 菜鸟求助:怎样编辑匹配行的上一行?

发表于 : 2014-05-18 11:39
youzhiyili
ubunbates 写了:sed -n '/^http/{x;s/^/你的符号/;x;};x;p;${x;p;}' 你的文件
:em11
可是,有一个小问题:文件头会多出一个空行,#!/bin/bash成了第二行

Re: 菜鸟求助:怎样编辑匹配行的上一行?

发表于 : 2014-05-18 12:11
eexpress
youzhiyili 写了:
ubunbates 写了:sed -n '/^http/{x;s/^/你的符号/;x;};x;p;${x;p;}' 你的文件
:em11
可是,有一个小问题:文件头会多出一个空行,#!/bin/bash成了第二行
解析不规则的文档,历来就没固定的方法。不应该用shell的简单方法去实现。

Re: 菜鸟求助:怎样编辑匹配行的上一行?

发表于 : 2014-05-18 12:15
ubunbates
eexpress 写了:
youzhiyili 写了:
ubunbates 写了:sed -n '/^http/{x;s/^/你的符号/;x;};x;p;${x;p;}' 你的文件
:em11
可是,有一个小问题:文件头会多出一个空行,#!/bin/bash成了第二行
解析不规则的文档,历来就没固定的方法。不应该用shell的简单方法去实现。
perl 怎么写?

Re: 菜鸟求助:怎样编辑匹配行的上一行?

发表于 : 2014-05-18 12:17
ubunbates
youzhiyili 写了:
ubunbates 写了:sed -n '/^http/{x;s/^/你的符号/;x;};x;p;${x;p;}' 你的文件
:em11
可是,有一个小问题:文件头会多出一个空行,#!/bin/bash成了第二行
多个空行不影响bash script的

sed -n '/^http/{x;s/^/你的符号/;x;};1h;1!{x;p;};${x;p;}' 你的文件

Re: 菜鸟求助:怎样编辑匹配行的上一行?

发表于 : 2014-05-18 13:39
youzhiyili
ubunbates 写了:
youzhiyili 写了:
ubunbates 写了:sed -n '/^http/{x;s/^/你的符号/;x;};x;p;${x;p;}' 你的文件
:em11
可是,有一个小问题:文件头会多出一个空行,#!/bin/bash成了第二行
多个空行不影响bash script的

sed -n '/^http/{x;s/^/你的符号/;x;};1h;1!{x;p;};${x;p;}' 你的文件
前辈能不能讲解一下那个x和p是什么意思?
小弟是初学者,看不懂,先谢了

Re: 菜鸟求助:怎样编辑匹配行的上一行?

发表于 : 2014-05-18 14:22
ubunbates
youzhiyili 写了:
ubunbates 写了:
youzhiyili 写了:
ubunbates 写了:sed -n '/^http/{x;s/^/你的符号/;x;};x;p;${x;p;}' 你的文件
:em11
可是,有一个小问题:文件头会多出一个空行,#!/bin/bash成了第二行
多个空行不影响bash script的

sed -n '/^http/{x;s/^/你的符号/;x;};1h;1!{x;p;};${x;p;}' 你的文件
前辈能不能讲解一下那个x和p是什么意思?
小弟是初学者,看不懂,先谢了
p和x都是sed的命令, 与s一样.
s是替换
p是打印
x是交换当前行和前一行的内容

初级sed见 http://www.theunixschool.com/2012/06/se ... ne-or.html

较高级sed见 http://www.grymoire.com/Unix/Sed.html

cheat sheet http://www.posteet.com/view/2151