在awk中设置外部变量

时间:2018-01-02 05:46:32

标签: linux bash awk

我编写了一个脚本,我想在其中计算data.txt中的列数。我的问题是我无法在awk脚本中设置x。 任何帮助都将受到高度赞赏。

while read p; do
  x=1;
  echo $p | awk -F' ' '{x=NF}'
  echo $x;
  file="$x"".txt";
  echo $file;
  done <$1

data.txt文件:

4495125 94307025    giovy115p@live.it   94307025.094307025  12443
stazla  deva1a23@gmail.com  1992/.:\1
1447585 gioao_87@hotmail.it h1st@1
saknit  tomboro@seznam.cz   1233    1990

预期产出:

5.txt
3.txt
3.txt
4.txt

我的输出:

1.txt
1.txt
1.txt
1.txt

3 个答案:

答案 0 :(得分:3)

您只是无法import UIKit import FirebaseDatabase import FirebaseAuth import Firebase class RootPageViewController: UIPageViewController, UIPageViewControllerDataSource { let Cards = CardsVC() lazy var viewControllerList:[UIViewController] = { let sb = UIStoryboard(name: "Main", bundle: nil) let vc1 = sb.instantiateViewController(withIdentifier: "MessagesVC") let vc2 = sb.instantiateViewController(withIdentifier: "CardsVC") let vc3 = sb.instantiateViewController(withIdentifier: "ProfileVC") return[vc1, vc2, vc3] }() override func viewDidLoad() { super.viewDidLoad() //if Auth.auth().currentUser?.uid == nil{ //logout() //}else{ self.dataSource = self if let firstViewController = viewControllerList.first{ self.setViewControllers([firstViewController], direction: .forward, animated: true, completion: nil) } //} } func logout(){ let storyboard = UIStoryboard(name: "Main", bundle: nil) let verifyPhoneVC = storyboard.instantiateViewController(withIdentifier: "enterPhoneNumber") present(verifyPhoneVC, animated: true, completion: nil) } func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? { guard let vcIndex = viewControllerList.index(of: viewController) else {return nil} let previousIndex = vcIndex - 1 guard previousIndex >= 0 else {return nil} guard viewControllerList.count > previousIndex else {return nil} return viewControllerList[previousIndex] } func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? { guard let vcIndex = viewControllerList.index(of: viewController) else {return nil} let nextIndex = vcIndex + 1 guard viewControllerList.count != nextIndex else {return nil} guard viewControllerList.count > nextIndex else {return nil} return viewControllerList[nextIndex] } } 中的变量集导入shell上下文。在您的示例中,包含Awk的{​​{1}}中设置的值不会反映在外部。

您需要使用命令替换(x)语法来获取NF的值并稍后使用

$(..)

现在NF将包含每行中的列数。请注意,您不需要使用x=$(echo "$p" | awk '{print NF}') x中的默认取消限制器。

此外,您的要求可以在-F' '本身完成。

awk

此处Awk用于确保awk 'NF{print NF".txt"}' file 内的操作仅应用于非空行。每行打印长度并附加扩展名NF{..}

答案 1 :(得分:1)

Awk一次处理一行 - 处理shell while read循环内单独的Awk脚本中的每一行是极其低效的。另请参阅https://unix.stackexchange.com/questions/169716/why-is-using-a-shell-loop-to-process-text-considered-bad-practice

也许是这样的:

awk '{ print >(NF ".txt") }' data.txt

使用5.txt中的五列行,4.txt中的四列行,2.txt中的三列行等创建包含每个唯一列数的文件

Awk变量NF包含字段数(默认情况下,awk在空格运行时拆分字段 - 使用-F更改为其他分隔符)和表达式(NF ".txt")只生成一个字符串连接,其中包含后缀为.txt的字段数,我们将其作为文件名传递给print重定向。

答案 2 :(得分:1)

使用bash:

while read p; do p=($p); echo "${#p[@]}.txt"; done < file

或更短:

while read -a p; do echo "${#p[@]}.txt"; done < file

输出:

5.txt
3.txt
3.txt
4.txt