亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

Chinaunix

標(biāo)題: 這個(gè)shell script 怎么寫? [打印本頁]

作者: shirley    時(shí)間: 2002-01-29 10:55
標(biāo)題: 這個(gè)shell script 怎么寫?
想寫這么一個(gè)shell script, 找出一個(gè)文件比如test中出現(xiàn)hello的次數(shù)。
請(qǐng)大家?guī)蛶兔,我還沒想出來。
作者: mygod    時(shí)間: 2002-01-29 11:13
標(biāo)題: 這個(gè)shell script 怎么寫?
小弟有一計(jì),不知可否:
awk '/hello/{print $0}' test > test1
awk  'BEGIN{FS='\n';OFS=''}{print $0}' test1 > test2
sed 's/$/aaa/' test2 > test3
awk 'BEGIN{FS='hello';OFS='\n';}' test3 > test4
length=`cat test3 | wc -l`
length=`length - 1`

作者: shirley    時(shí)間: 2002-01-29 11:38
標(biāo)題: 這個(gè)shell script 怎么寫?
執(zhí)行了一下,有語法錯(cuò)誤。我再仔細(xì)看看。
看的出來,閣下的shell水平很高。
或許命令awk的使用是關(guān)鍵,這個(gè)我不很熟,需要惡補(bǔ)一下。
作者: mygod    時(shí)間: 2002-01-29 11:46
標(biāo)題: 這個(gè)shell script 怎么寫?
可能是最后一句有錯(cuò),我的意思是將length的值減去1,可能寫法不對(duì)
作者: shirley    時(shí)間: 2002-01-29 11:57
標(biāo)題: 這個(gè)shell script 怎么寫?
好象第一行執(zhí)行就有錯(cuò)。可能是分隔符鬧的吧。
最后一句確實(shí)不對(duì),應(yīng)該是:
length=`expr $length - 1`
作者: mygod    時(shí)間: 2002-01-29 12:31
標(biāo)題: 這個(gè)shell script 怎么寫?
/hello/和{print $0}之間應(yīng)當(dāng)有空格,程序的思路大體是這樣:
首先,將文件中所有包含'hello'的行輸出到test1中,然后將文件的所有行連成一行輸出到test2,這樣test2就變成了只有一行的文件,然后為了避免文件的最后一個(gè)單詞是'hello'造成邊界問題,在test2的最后加上'aaa',輸出到test3,然后以'hello'為分割符,回車為輸出的分割符將內(nèi)容輸出到test4,這樣文件中有多少個(gè)'hello'就會(huì)有多少個(gè)回車,也就是有多少行,當(dāng)然最后別忘了減一
作者: feeling    時(shí)間: 2002-01-29 14:23
標(biāo)題: 這個(gè)shell script 怎么寫?
[這個(gè)貼子最后由feeling在 2002/01/29 03:47pm 編輯]

賦值語句可以直接用 let length=$length-1

作者: shirley    時(shí)間: 2002-01-29 14:43
標(biāo)題: 這個(gè)shell script 怎么寫?
下面引用由feeling2002/01/29 02:23pm 發(fā)表的內(nèi)容:
賦值語句可以直接用 $length=$length-1 或者 let length=$length-1
你這指的是什么shell呀。據(jù)我所知,B SHELL和K SHELL是不能這樣寫的。
作者: feeling    時(shí)間: 2002-01-29 15:43
標(biāo)題: 這個(gè)shell script 怎么寫?
[這個(gè)貼子最后由feeling在 2002/01/29 03:48pm 編輯]

寫錯(cuò)了一點(diǎn),已經(jīng)修改。
在K Shell 下調(diào)試通過
作者: mygod    時(shí)間: 2002-01-29 17:08
標(biāo)題: 這個(gè)shell script 怎么寫?
請(qǐng)大家開動(dòng)腦筋,看看還有沒有其他方法
作者: 夢叮咚    時(shí)間: 2002-01-29 17:18
標(biāo)題: 這個(gè)shell script 怎么寫?
我剛學(xué)shell,不過覺得上例有些煩瑣,用c shell會(huì)不會(huì)簡單一些.

不是很懂,只是提個(gè)想法.
作者: feeling    時(shí)間: 2002-01-29 18:09
標(biāo)題: 這個(gè)shell script 怎么寫?
如果只是想知道個(gè)數(shù)的話,可以簡單地在vi模式用查找、替換命令來得到。
作者: cpss    時(shí)間: 2002-01-29 20:01
標(biāo)題: 這個(gè)shell script 怎么寫?
這樣寫行不行:
number=`more test|tr 'hello'|wc -w`  #計(jì)算以hello為界限拔test分為幾部分
number=`expr $number - 1`  #上面的辦法會(huì)多計(jì)上一個(gè),所以這里減掉一。
作者: mygod    時(shí)間: 2002-01-30 10:13
標(biāo)題: 這個(gè)shell script 怎么寫?
[這個(gè)貼子最后由mygod在 2002/01/30 10:15am 編輯]

呵呵,偶又生一計(jì):
sed 's/hello/\nhello\n/' test > test1
sed '/hello/' test1 > test2
length=`cat test2 | wc -l`

作者: shirley    時(shí)間: 2002-01-30 11:01
標(biāo)題: 這個(gè)shell script 怎么寫?
mygod真是高手啊。我的想法與你不謀而合。就是將hello用一個(gè)包含特殊標(biāo)志的新行代替,然后計(jì)算包含特殊標(biāo)志的行數(shù)。只是不知道在sed中怎樣添加一個(gè)新行,好象\n不成。

從今天早上坐車開始,我就在琢磨sed的使用。
作者: mygod    時(shí)間: 2002-01-30 11:35
標(biāo)題: 這個(gè)shell script 怎么寫?
哈哈,I got it,在AIX上調(diào)試通過:
sed 's/$/qqq/' test > test1
cat test1 | tr -d '\n' > test2
cat test2 | tr 'hello' '\n' > test3
length=`cat test3 | wc -l`
length=`expr $length - 1`

作者: michaelds    時(shí)間: 2002-03-13 19:09
標(biāo)題: 這個(gè)shell script 怎么寫?
剛翻出這個(gè)舊帖,覺得可以這樣:
awk '{for (i=1;i<=NF;i++) if ($i == &quot;hello&quot count++} END{print count}' file
作者: mygod    時(shí)間: 2002-03-14 08:36
標(biāo)題: 這個(gè)shell script 怎么寫?
哇,高明!
作者: txlinux    時(shí)間: 2002-03-14 09:17
標(biāo)題: 這個(gè)shell script 怎么寫?
佩服各位高手的SHELL功底!
小弟深受
作者: txlinux    時(shí)間: 2002-03-14 09:19
標(biāo)題: 這個(gè)shell script 怎么寫?
。。。。
深受啟發(fā)!

(剛才一不小心,錯(cuò)按了發(fā)表。:)

作者: 哈密瓜    時(shí)間: 2002-03-14 09:26
標(biāo)題: 這個(gè)shell script 怎么寫?
michaelds 跟 mygod兩位高手太扎實(shí)了,pfpf!
作者: bjchenxu    時(shí)間: 2002-08-16 15:41
標(biāo)題: 這個(gè)shell script 怎么寫?
在valentine的啟發(fā)下,我也提出一種解法
sed 's/hello/hello@/g' test | tr '@' '\n' | grep -c hello

作者: HopeCao    時(shí)間: 2002-08-17 09:52
標(biāo)題: 這個(gè)shell script 怎么寫?
Num=0
awk '/hello/' test | while read Line
do
       Num=`expr $Num + 1`
done
echo $Num
這樣子不知道行不行?
作者: bjchenxu    時(shí)間: 2002-08-19 16:51
標(biāo)題: 這個(gè)shell script 怎么寫?
[這個(gè)貼子最后由bjchenxu在 2002/08/19 06:12pm 編輯]

再來一招
編輯腳本文件sedfile
內(nèi)容為:
s/hello/&amp;\
/g

然后運(yùn)行命令:
sed -f sedfile test | grep -c hello

注意,以上在csh中通過

-------------------
bash中就更加簡單了
不用建立什么文件了
sed 's/hello/&amp;\
/g' test | grep -c hello
作者: wangrujun    時(shí)間: 2003-03-27 19:03
標(biāo)題: 這個(gè)shell script 怎么寫?
這樣為什么不行呢?不在文件里面,在命令行中
echo "/xxx/1/2/3/4/5.sh" | awk '{for (i=1;i<=NF;i++) if ($i == "/" count++} END{print count}'
作者: 紅袖添香    時(shí)間: 2003-03-28 01:58
標(biāo)題: 這個(gè)shell script 怎么寫?
原帖由 "wangrujun" 發(fā)表:
這樣為什么不行呢?不在文件里面,在命令行中
echo "/xxx/1/2/3/4/5.sh" | awk '{for (i=1;i<=NF;i++) if ($i == "/" count++} END{print count}'


我猜你是想數(shù)一下有幾個(gè)"/",是吧?

當(dāng)然這樣是不行的,域分隔符默認(rèn)是空白,而你這里不存在啊。加上空白就行了,你可以試一下,

# echo "/ xxx / 1 / 2 / 3 / 4 / 5.sh" | awk '{for (i=1;i<=NF;i++) if ($i == "/" count++} END{print count}'

這樣加空格當(dāng)然只是說明一下怎么回事,想要達(dá)到你的目的,可以這樣做,

# echo "/xxx/1/2/3/4/5.sh" | nawk '{gsub(/[^\/]/,"",$0) ; print length($0) }'
作者: wangrujun    時(shí)間: 2003-03-28 09:09
標(biāo)題: 這個(gè)shell script 怎么寫?
您真厲害。nawk我還是第一次看到。shell的書上只介紹了awk

打心底佩服您的shell功力。

多謝您的指教。
作者: aquino    時(shí)間: 2003-03-28 10:25
標(biāo)題: 這個(gè)shell script 怎么寫?
不用編程啦,
grep -c hello test
作者: wangrujun    時(shí)間: 2003-03-28 11:57
標(biāo)題: 這個(gè)shell script 怎么寫?
原帖由 "aquino" 發(fā)表:
不用編程啦,
grep -c hello test


如果一行中有兩個(gè)以上hello,這樣就錯(cuò)了。
作者: tanny    時(shí)間: 2003-03-28 13:06
標(biāo)題: 這個(gè)shell script 怎么寫?
原帖由 "shirley" 發(fā)表:
mygod真是高手啊。我的想法與你不謀而合。就是將hello用一個(gè)包含特殊標(biāo)志的新行代替,然后計(jì)算包含特殊標(biāo)志的行數(shù)。只是不知道在sed中怎樣添加一個(gè)新行,好象\n不成。

從今天早上坐車開始,我就在琢磨sed的使用。


\n就是\之后敲回車鍵
作者: carr    時(shí)間: 2003-04-17 13:22
標(biāo)題: 這個(gè)shell script 怎么寫?
大家可真是高。FPF
作者: wstommy    時(shí)間: 2003-04-17 17:52
標(biāo)題: 這個(gè)shell script 怎么寫?
原帖由 "michaelds" 發(fā)表:
剛翻出這個(gè)舊帖,覺得可以這樣:
awk '{for (i=1;i<=NF;i++) if ($i == "hello" count++} END{print count}' file


牛,PFPF
作者: woodie    時(shí)間: 2003-04-28 19:17
標(biāo)題: 這個(gè)shell script 怎么寫?
tr -d "\n" < test| perl -e '$_=<STDIN>;print s/hello//g'
作者: rollingpig    時(shí)間: 2003-06-24 17:19
標(biāo)題: 這個(gè)shell script 怎么寫?
cat test |awk '{OFS="\n";print $1}'|grep hello|wc -l
作者: minewhy    時(shí)間: 2003-06-25 13:53
標(biāo)題: 這個(gè)shell script 怎么寫?
前面的shell程序可能沒有考慮到形如khellollsd這種夾在中間的hello詞匯。容易造成錯(cuò)誤。
      我寫了一個(gè)perl的程序,望大家指正:
     #!/usr/bin/perl
      my ($count);
      $count=0;
     open(IN,"test";
     while(<IN>
         {  @words=split(/ /,$_);
            foreach $word (@words)
                 { if($word =~ /hello/i)  { $count++;}}
         }
         print "the hello number is $count\n";

   在solaries 下通過運(yùn)行。
作者: lovesaka    時(shí)間: 2006-10-03 02:27

  1. xargs -n1 <file|grep "hello"|wc -w
復(fù)制代碼

作者: awk就是awp加ak    時(shí)間: 2006-10-03 10:37
喝喝,把陳年老酒都挖出來了!
不用說,最后一個(gè)方法最精簡
作者: 寂寞烈火    時(shí)間: 2006-10-03 11:58
原帖由 awk就是awp加ak 于 2006-10-3 10:37 發(fā)表
喝喝,把陳年老酒都挖出來了!
不用說,最后一個(gè)方法最精簡

grep -o '\<hello\>' tets|wc -l
or
awk '{for(i=1;i<=NF;i++)if($i~/\<hello\>/){n++}}END{print n}' test
BTW:這貼忒old啦~
作者: awk就是awp加ak    時(shí)間: 2006-10-03 12:07
貌似不是統(tǒng)計(jì)包含 "hello" 得有多少行
作者: lei8c8    時(shí)間: 2006-10-04 22:45
原帖由 minewhy 于 2003-6-25 13:53 發(fā)表
前面的shell程序可能沒有考慮到形如khellollsd這種夾在中間的hello詞匯。容易造成錯(cuò)誤。
      我寫了一個(gè)perl的程序,望大家指正:
     #!/usr/bin/perl
      my ($count);
      $count=0;
     open(IN ...

hi,你這種情況似乎也有漏洞的
比如有一行: abc hellohello cba
就會(huì)少統(tǒng)計(jì)一次
作者: easthh    時(shí)間: 2006-10-07 11:25
換個(gè)思路。(K shell@AIX下通過)
for i in `awk -F "hello" '{print NF}' urfile`
do
let x=${x:-0}+$i-1
done
echo $x
作者: antimatter    時(shí)間: 2006-11-29 17:36
個(gè)人覺得這個(gè)問題首先要把問題的條件限定清楚,做一個(gè)較為完整的需求分析。然后我們?cè)俑鶕?jù)不同的情況分析。
作者: zwylinux    時(shí)間: 2006-12-03 00:27
原帖由 mygod 于 2002-1-30 10:13 發(fā)表
[這個(gè)貼子最后由mygod在 2002/01/30 10:15am 編輯]

呵呵,偶又生一計(jì):
sed 's/hello/\nhello\n/' test > test1
sed '/hello/' test1 > test2
length=`cat test2 | wc -l`


有錯(cuò)誤,呵呵。我拿這個(gè)文件來試一下,想找出4的個(gè)數(shù),你的腳本只找出了10個(gè)
Adams Mary     5346      11/4/63   28765
    Adams Mary     5346      11/4/63   28765

        Tom Jones      4423      5/12/66   543354
Sally Chang    1654      7/22/54   650000
Billy Black    1683      9/23/44   336500
foo fjdkl foo  slkdfj sldfj lsjf
作者: moonlily    時(shí)間: 2006-12-03 15:27
提示: 作者被禁止或刪除 內(nèi)容自動(dòng)屏蔽
作者: twf_cc    時(shí)間: 2006-12-03 17:21
原帖由 shirley 于 2002-1-29 10:55 發(fā)表
想寫這么一個(gè)shell script, 找出一個(gè)文件比如test中出現(xiàn)hello的次數(shù)。
請(qǐng)大家?guī)蛶兔Γ疫沒想出來。


以bash3 的方法
#! /bin/bash
pat=$1
file=$2
[ ! -f "$file" ] || [ $# -ne 2 ] && exit
i=0
for howmany in $(< $file)
   do
      [[ "$howmany" =~ "\<$pat\>" ]] && (( i++ ))
done
echo "$pat:     $i times appeared"
作者: siehon    時(shí)間: 2006-12-04 11:40
好貼.好多牛人思路很好,不過不少都沒考慮一些特殊情況.
作者: ora_length    時(shí)間: 2006-12-04 14:47
# awk -F"hello" '{sum+=NF;sum-=1} END{print sum}' test
作者: jlus    時(shí)間: 2006-12-04 15:27
樓上的思路挺牛
不過有空行的時(shí)候有點(diǎn)問題吧
作者: jlus    時(shí)間: 2006-12-04 15:32
原帖由 antimatter 于 2006-11-29 17:36 發(fā)表
個(gè)人覺得這個(gè)問題首先要把問題的條件限定清楚,做一個(gè)較為完整的需求分析。然后我們?cè)俑鶕?jù)不同的情況分析。


就是,需求都不清楚怎么做,難道真的要做個(gè)通用的?
作者: jlus    時(shí)間: 2006-12-04 15:53
原帖由 ora_length 于 2006-12-4 14:47 發(fā)表
# awk -F"hello" '{sum+=NF;sum-=1} END{print sum}' test


借用一下




  1. sed 's/hello/hello\n/g' test |sed -n -e '/hello/p'|awk -F"hello" '{sum+=NF;sum-=1} END{print sum}'
復(fù)制代碼

作者: 26510777    時(shí)間: 2007-08-08 22:34
標(biāo)題: 回復(fù) #1 shirley 的帖子
sed 's/hello/\nhelo\n/g' a.txt|grep -c hello




歡迎光臨 Chinaunix (http://72891.cn/) Powered by Discuz! X3.2