对于文件中的每一行同步执行命令并保存到另一个文件的换行符 [英] For each line in file execute command synchronously and save to newline of another file

查看:43
本文介绍了对于文件中的每一行同步执行命令并保存到另一个文件的换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 Chktitle.shwget 脚本——这个脚本需要一个像下面这样的命令

I have a wget script named Chktitle.sh -- this script takes a command like below

$ Chktitle.sh "my url"

然后我有一个文件名 url.txt 有超过 100 行的 urls 和 ips 来检查网页标题.然后我将 results.txt 作为一个空白文件.

I then have a file name url.txt with over 100 lines with urls and ips to check for web-page titles. Then i have results.txt as a blank file.

有什么办法可以对文件中的每一行执行如下重复操作:

Is there any way I can perform a repetitive action like below for each line in the file:

 Grab line1 from url.txt
 -----
 then execute Chktitle.sh "line1"
 -----
 Now save the result for line1 in results.txt
 -----
 Now goto Line2 ........


 etc etc etc

我需要确保它只会在上一行完成后才执行下一行.任何人都可以向我展示执行此操作的任何简单方法吗?我很乐意使用 Perl、sh 并考虑其他语言..

I need to make sure that it will only execute the next line after the previous one has finished. Can any one show me any easy way to perform this? I am happy to use Perl, sh, and consider other languages..

chktitle.sh的内容:

#!/bin/bash
string=$1"/search/"
wget --quiet -O - $string \
| sed -n -e 's!.*<title>\(.*\)</title>.*!\1!p'

推荐答案

也许这样的事情会有所帮助(前提是我理解正确):

Maybe something like this could help (provided that I understood correctly) :

while read line; do
    /path/to/Chktitle.sh x"$line" >> results.txt;
done < /path/to/input.txt

对于 /path/to/input.txt 中的每一行,执行您的脚本并将输出 (>>) 附加到 results.txt.

For each line in /path/to/input.txt, execute your script and append the output (>>) to results.txt.

当然,您可以随时在 while 循环中添加其他语句:

Of course you could always add additional statements in your while loop :

while read line; do
    # Initialise var to output of chktitle
    var=$(/path/to/Chktitle.sh x"$line");

    # Add conditions
    if [ "$var" = "google" ]; then
        echo "google" >> result.txt;
    else
        echo "not google" >> result.txt;
    fi
done < /path/to/input.txt

这篇关于对于文件中的每一行同步执行命令并保存到另一个文件的换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆