Home > front end >  use awk variable in bash script
use awk variable in bash script

Time:11-12

I have a txt file with blow format:

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:

#!/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

What is the problem

CodePudding user response:

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

CodePudding user response:

If you are wanting to use awk, here is one way to do it:

#!/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
  • Related