English 中文(简体)
我如何利用空壳工具从一个超文本档案中提取一个物件。
原标题:How can I extract an item ID from an HTML file using shell tools?
  • 时间:2011-08-06 17:48:16
  •  标签:
  • bash
  • shell
  • sh

我有一份标语(每行一个)的文件,我需要通过该名单开放和循环。 有时用一个名字,下载相应的超文本页面,并提取页上的“项目_id”。

这个项目在“超文本”上显示:?item_id=55963573”>

这是我迄今为止所做的。

#!/bin/sh

for productID in (catIDs.txt) #I know this part is not correct
do
    wget -q -U Mozilla "http://www.example.com/$productID/" -O - 
     | tr  "   
  | grep "^item_id" | cut -d     -f 4 >> itemIDs.txt
    sleep 15
done
最佳回答

这一工作应当:

#!/bin/sh

while read productID; do
    wget -q -U Mozilla "http://www.example.com/$productID/" -O - |
    sed -n -r  s/.*?item_id=([0-9]+)">.*/1/p 
done <catIDs.txt >itemIDs.txt
问题回答

如果档案小,则使用:

for productID in `cat catIDs.txt`
cat catIDs.txt | while read productID;
do
  wget -q -U Mozilla "http://www.domain.com/$productID/" -O - 
  | tr  "   
  | grep "^item_id" | cut -d     -f 4 >> itemIDs.txt
  sleep 15
done

while read productID;
do
  wget -q -U Mozilla "http://www.domain.com/$productID/" -O - 
  | tr  "   
  | grep "^item_id" | cut -d     -f 4 >> itemIDs.txt
  sleep 15
done < catIDs.txt




相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

Bash usage of vi or emacs

From a programming standpoint, when you set the bash shell to use vi or emacs via set -o vi or set -o emacs What is actually going on here? I ve been reading a book where it claims the bash shell ...

Dynamically building a command in bash

I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...

Perform OR on two hash outputs of sha1sum

I want perform sha1sum file1 and sha1sum file2 and perform bitwise OR operation with them using bash. Output should be printable i.e 53a23bc2e24d039 ... (160 bit) How can I do this? I know echo $(( ...

Set screen-title from shellscript

Is it possible to set the Screen Title using a shell script? I thought about something like sending the key commands ctrl+A shift-A Name enter I searched for about an hour on how to emulate ...

热门标签