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

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
youzhiyili
帖子: 2422
注册时间: 2012-03-22 20:42
系统: ubuntu22.04

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

#1

帖子 youzhiyili » 2014-05-17 22:02

匹配以http开头的行:^http
可是我想在它上面那一行的行首插入字符,不知道怎么弄 :em06
@自由建客 @qy117121 @枫叶饭团 @cikekid @YeLee @jtshs256 @eexpress @ljj_jjl2008 @zhw2101024 @TeliuTe @月下叹逍遥 @leeaman @懒蜗牛Gentoo @灰色小狼 @photor @adagio @JiangHui @phoenixlzx @oneleaf
nae6taiyie0T
帖子: 482
注册时间: 2013-09-13 0:42
系统: Debian sid

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

#2

帖子 nae6taiyie0T » 2014-05-17 23:13

你要干嘛?

可以的话, 把你要匹配的文件帖出来, 看看你需要修改哪些字符?
头像
youzhiyili
帖子: 2422
注册时间: 2012-03-22 20:42
系统: ubuntu22.04

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

#3

帖子 youzhiyili » 2014-05-18 0:10

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
@自由建客 @qy117121 @枫叶饭团 @cikekid @YeLee @jtshs256 @eexpress @ljj_jjl2008 @zhw2101024 @TeliuTe @月下叹逍遥 @leeaman @懒蜗牛Gentoo @灰色小狼 @photor @adagio @JiangHui @phoenixlzx @oneleaf
头像
astolia
论坛版主
帖子: 6703
注册时间: 2008-09-18 13:11

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

#4

帖子 astolia » 2014-05-18 0:16

用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
上次由 astolia 在 2014-05-18 0:17,总共编辑 1 次。
头像
YeLee
论坛版主
帖子: 26406
注册时间: 2008-08-13 8:48
系统: Fundu i64
来自: 东海硇州,一双管钥。
联系:

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

#5

帖子 YeLee » 2014-05-18 0:17

覚得没有必要作后期替换吧,前期就把<title>匹配好的话,这样会简単很多的。 :em01
◎当我站在道德的高度上俯视别人的时候,发现自己是多么渺小。
♥执着但不偏激,反对而不排斥,坚决捍卫矛盾体的存在方式。
★★★天气预报★★★
fcitx-yatable一个可以使用的码表输入法
[教程]几个实例攻克软件编译难关
Gentoo Development Guide
字体相关
头像
youzhiyili
帖子: 2422
注册时间: 2012-03-22 20:42
系统: ubuntu22.04

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

#6

帖子 youzhiyili » 2014-05-18 1:20

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
}
不过,我还是特别想知道在不可避免的情况下,怎样解决这个问题
@自由建客 @qy117121 @枫叶饭团 @cikekid @YeLee @jtshs256 @eexpress @ljj_jjl2008 @zhw2101024 @TeliuTe @月下叹逍遥 @leeaman @懒蜗牛Gentoo @灰色小狼 @photor @adagio @JiangHui @phoenixlzx @oneleaf
头像
youzhiyili
帖子: 2422
注册时间: 2012-03-22 20:42
系统: ubuntu22.04

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

#7

帖子 youzhiyili » 2014-05-18 1:21

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
@自由建客 @qy117121 @枫叶饭团 @cikekid @YeLee @jtshs256 @eexpress @ljj_jjl2008 @zhw2101024 @TeliuTe @月下叹逍遥 @leeaman @懒蜗牛Gentoo @灰色小狼 @photor @adagio @JiangHui @phoenixlzx @oneleaf
ubunbates
帖子: 120
注册时间: 2014-04-30 15:44
系统: linux

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

#8

帖子 ubunbates » 2014-05-18 11:21

sed -n '/^http/{x;s/^/你的符号/;x;};x;p;${x;p;}' 你的文件
头像
youzhiyili
帖子: 2422
注册时间: 2012-03-22 20:42
系统: ubuntu22.04

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

#9

帖子 youzhiyili » 2014-05-18 11:39

ubunbates 写了:sed -n '/^http/{x;s/^/你的符号/;x;};x;p;${x;p;}' 你的文件
:em11
可是,有一个小问题:文件头会多出一个空行,#!/bin/bash成了第二行
@自由建客 @qy117121 @枫叶饭团 @cikekid @YeLee @jtshs256 @eexpress @ljj_jjl2008 @zhw2101024 @TeliuTe @月下叹逍遥 @leeaman @懒蜗牛Gentoo @灰色小狼 @photor @adagio @JiangHui @phoenixlzx @oneleaf
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

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

#10

帖子 eexpress » 2014-05-18 12:11

youzhiyili 写了:
ubunbates 写了:sed -n '/^http/{x;s/^/你的符号/;x;};x;p;${x;p;}' 你的文件
:em11
可是,有一个小问题:文件头会多出一个空行,#!/bin/bash成了第二行
解析不规则的文档,历来就没固定的方法。不应该用shell的简单方法去实现。
● 鸣学
ubunbates
帖子: 120
注册时间: 2014-04-30 15:44
系统: linux

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

#11

帖子 ubunbates » 2014-05-18 12:15

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

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

#12

帖子 ubunbates » 2014-05-18 12:17

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;}' 你的文件
头像
youzhiyili
帖子: 2422
注册时间: 2012-03-22 20:42
系统: ubuntu22.04

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

#13

帖子 youzhiyili » 2014-05-18 13:39

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是什么意思?
小弟是初学者,看不懂,先谢了
@自由建客 @qy117121 @枫叶饭团 @cikekid @YeLee @jtshs256 @eexpress @ljj_jjl2008 @zhw2101024 @TeliuTe @月下叹逍遥 @leeaman @懒蜗牛Gentoo @灰色小狼 @photor @adagio @JiangHui @phoenixlzx @oneleaf
ubunbates
帖子: 120
注册时间: 2014-04-30 15:44
系统: linux

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

#14

帖子 ubunbates » 2014-05-18 14:22

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
回复