1

好的,所以我有一个包含以下信息 my.txt 的文本文件

user:pass
user:pass
user:pass
user:pass

我想打开文件抓取内容并操作每一行,然后将其输出到一个名为 my2 文件的文本中,所以它看起来像这样

http://mysever.com/2/tasks.php?account=user%3Apass
http://mysever.com/2/tasks.php?account=user%3Apass
http://mysever.com/2/tasks.php?account=user%3Apass
http://mysever.com/2/tasks.php?account=user%3Apass

谁能帮忙

4

5 回答 5

2

使用一点 awk 脚本:

 awk -F: '{printf "http://mysever.com/2/tasks.php?account=%s%%3A%s\n", $1, $2}' < my.txt > my2
于 2013-06-28T12:52:25.367 回答
1

使用sed您可以使用-i选项进行 infile 替换或重定向到新文件

$ sed 's,\(.*\):\(.*\),http://mysever.com/2/tasks.php?account=\1%3A\2,' my.txt > my2.txt
http://mysever.com/2/tasks.php?account=user%3Apass
http://mysever.com/2/tasks.php?account=user%3Apass
http://mysever.com/2/tasks.php?account=user%3Apass
http://mysever.com/2/tasks.php?account=user%3Apass

使用awk

$ awk -F: '{print "http://mysever.com/2/tasks.php?account="$1"%3A"$2}' my.txt > my2.txt
http://mysever.com/2/tasks.php?account=user%3Apass
http://mysever.com/2/tasks.php?account=user%3Apass
http://mysever.com/2/tasks.php?account=user%3Apass
http://mysever.com/2/tasks.php?account=user%3Apass
于 2013-06-28T12:55:22.927 回答
1

非常基本的bash:

while IFS=":" read u p
do
  echo "http://mysever.com/2/tasks.php?account=$u%3A$p"
done < my.txt > my2.txt

测试

$ while IFS=":" read u p; do echo "http://mysever.com/2/tasks.php?account=$u%3A$p"; done < file > my2.txt
$ cat my2.txt
http://mysever.com/2/tasks.php?account=user%3Apass
http://mysever.com/2/tasks.php?account=user%3Apass
http://mysever.com/2/tasks.php?account=user%3Apass
http://mysever.com/2/tasks.php?account=user%3Apass
于 2013-06-28T12:48:49.467 回答
0

Perl 解决方案:

perl -ne 'chomp; ($user, $pass) = split /:/;
  print "http://mysever.com/2/tasks.php?account=$user%3A$pass\n"' my.txt
于 2013-06-28T13:13:25.860 回答
0

做你想要的并丢弃带有空字段的行(例如,空行):

while read l; do
    [[ $l =~ .+:.+ ]] || continue
    printf "http://mysever.com/2/tasks.php?account=%s\n" "${l/:/%3A}"
done < my.txt > my2.txt

警告。确保密码不包含任何有趣的符号。

于 2013-06-28T14:01:04.287 回答