将一个文件的值传递到另一个文件作为输入

时间:2022-08-06 10:03:00

I have a file that contains following information in a tab separated manner:

我有一个文件,以标签分隔的方式包含以下信息:

abscdfr  2   5678
bgbhjgy  7   8756
ptxfgst  5   6783

lets call this file A and it contains 2000 lines

我们调用这个文件A,它包含2000行

and I have another file B written in ruby

我有另一个用红宝石写的文件B.

that takes these values as command line input:

将这些值作为命令行输入:

f_id = ARGV[0]
lane = ARGV[1].to_i 
sample_id = ARGV[2].to_i
puts " #{f_id}_#{lane}_#{sample_id}.bw"

I execute the file B in ruby by providing the information in file A

我通过提供文件A中的信息在ruby中执行文件B.

./fileB.rb abscdfr 2 5678

./fileB.rb abscdfr 2 5678

I want to know how can I pass the values of file A as input to file B in a recursive manner.

我想知道如何以递归方式将文件A的值作为输入传递给文件B.

If it was one value it was easy but I am confused with three values.

如果它是一个值很容易,但我对三个值感到困惑。

Kindly help me in writing a wrapper around these two file either in bash or ruby.

请帮我用bash或ruby编写这两个文件的包装器。

Thank you

谢谢

3 个解决方案

#1


1  

The following command will do the job in bash:

以下命令将在bash中执行以下任务:

while read line; do ./fileB.rb $line; done < fileA

This reads each lines into line. Then it runs ./fileB.rb $line for each line. $line gets replaced before the command line is evaluated, thus each word in every line is passed as its own argument, it is important that there is no quotation like "$line". read reads from STDIN and would usually wait for user input, but with < fileA the content of fileA is redirected to STDIN so that read takes its input from there.

这将每行读入行。然后它为每一行运行./fileB.rb $ line。在评估命令行之前,$ line被替换,因此每行中的每个单词都作为自己的参数传递,重要的是没有像“$ line”这样的引用。读取从STDIN读取并通常等待用户输入,但是使用 将filea的内容重定向到stdin,以便读取从那里获取其输入。

#2


1  

You could use a little bash script to loop through each line in the file and output the contents as arguments to another script.

您可以使用一个小的bash脚本循环遍历文件中的每一行,并将内容作为参数输出到另一个脚本。

while read line; do eval "./fileB.rb $line" done < fileA

读线; eval“./ fileB.rb $ line”done

This will evaluate the line in the quotes as if you typed it into the shall yourself.

这将评估引号中的行,就像您自己键入它一样。

#3


1  

Also you can use one liner ruby :

你也可以使用一个衬里红宝石:

ruby -ne 'system( "./fileB.rb  #{$_}" )' < fileA

Explanation :

说明:

  • -e Which allow us to specifies script from command-line
  • -e允许我们从命令行指定脚本
  • -n The other useful flags are -n (somewhat like sed -n or awk) , the flag tell ruby to read input or input file line by line like while loop.
  • -n其他有用的标志是-n(有点像sed -n或awk),该标志告诉ruby逐行读取输入或输入文件,如while循环。
  • $_ Default ruby save current line stored in $_ variable
  • $ _默认ruby保存存储在$ _变量中的当前行

#1


1  

The following command will do the job in bash:

以下命令将在bash中执行以下任务:

while read line; do ./fileB.rb $line; done < fileA

This reads each lines into line. Then it runs ./fileB.rb $line for each line. $line gets replaced before the command line is evaluated, thus each word in every line is passed as its own argument, it is important that there is no quotation like "$line". read reads from STDIN and would usually wait for user input, but with < fileA the content of fileA is redirected to STDIN so that read takes its input from there.

这将每行读入行。然后它为每一行运行./fileB.rb $ line。在评估命令行之前,$ line被替换,因此每行中的每个单词都作为自己的参数传递,重要的是没有像“$ line”这样的引用。读取从STDIN读取并通常等待用户输入,但是使用 将filea的内容重定向到stdin,以便读取从那里获取其输入。

#2


1  

You could use a little bash script to loop through each line in the file and output the contents as arguments to another script.

您可以使用一个小的bash脚本循环遍历文件中的每一行,并将内容作为参数输出到另一个脚本。

while read line; do eval "./fileB.rb $line" done < fileA

读线; eval“./ fileB.rb $ line”done

This will evaluate the line in the quotes as if you typed it into the shall yourself.

这将评估引号中的行,就像您自己键入它一样。

#3


1  

Also you can use one liner ruby :

你也可以使用一个衬里红宝石:

ruby -ne 'system( "./fileB.rb  #{$_}" )' < fileA

Explanation :

说明:

  • -e Which allow us to specifies script from command-line
  • -e允许我们从命令行指定脚本
  • -n The other useful flags are -n (somewhat like sed -n or awk) , the flag tell ruby to read input or input file line by line like while loop.
  • -n其他有用的标志是-n(有点像sed -n或awk),该标志告诉ruby逐行读取输入或输入文件,如while循环。
  • $_ Default ruby save current line stored in $_ variable
  • $ _默认ruby保存存储在$ _变量中的当前行