简体   繁体   English

在 bash 脚本中使用 awk 变量

[英]use awk variable in bash script

I have a txt file with blow format:我有一个吹格式的txt文件:

66.57.21 - john
88.43.23 - albert
10.10.11 - smith

I wanna to execute "connect.py 66.57.21 john" for each line and I wrote this bash script:我想为每一行执行“connect.py 66.57.21 john”,我写了这个 bash 脚本:

#!/bin/bash
while read LINE; do
awk -v number = "$LINE" '$1'
awk -v name = "$LINE" '$3'
  connect.py $name $number
done < "$1"

but the bash script didn't work但是 bash 脚本不起作用

What is the problem问题是什么

#!/usr/bin/env bash
while read -r number _ name; do
  connect.py "$name" "$number"
done < "$1"

If you are wanting to use awk, here is one way to do it:如果您想使用 awk,这是一种方法:

#!/bin/bash
while read LINE; do
  number=$(echo $LINE | awk -F" " '{print $1}')
  name=$(echo $LINE | awk -F" " '{print $3}')
  connect.py $name $number
done < "${1:-/dev/stdin}"

here is the usage这是用法

cat input.txt | ./program.sh

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM