CLI结合了两个不同文件中的两个不平衡列表

发布于 2025-01-27 18:55:18 字数 384 浏览 3 评论 0原文

我一直在寻找一种结合两个不平衡文件的方法,我知道有一个解决方案,但我无法对其进行修改以满足我的需求。 IE

文件1

a
b
c
d

文件2

1
2

解决方案

a:1
b:2
c:1
d:2

如果第一个文件是10个单词,而第二个单词仅为3个单词,请重复3个单词,直到第一个文件eof的末尾,并且在其中确定有一个定界符标志。

我最好的尝试是:

paste -d file1 /dev/null file2 > new_file

但这只会使1,2列入新列表,但没有重复。

I was looking for a way to combine two files that are uneven, I know there is a solution with awk but I am unable to modify it to fit my need. I.E.

File 1

a
b
c
d

File 2

1
2

Solution

a:1
b:2
c:1
d:2

if the first file is 10 words and the second is only 3 words, repeat the 3 words until the end of the first file EOF, and im sure theres a delimiter flag with ':' in there.

My best attempt was:

paste -d file1 /dev/null file2 > new_file

But that only put the 1,2 in the new list but didn't repeat.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

两个我 2025-02-03 18:55:18

对于给定的示例数据,您可以尝试此awk

awk 'FNR == NR{a[++n]=$1; next} {print $1 ":" a[(FNR-1) % n + 1]}' file2 file1

a:1
b:2
c:1
d:2

For the given sample data, you may try this awk:

awk 'FNR == NR{a[++n]=$1; next} {print $1 ":" a[(FNR-1) % n + 1]}' file2 file1

a:1
b:2
c:1
d:2
墨落成白 2025-02-03 18:55:18

使用您显示的样本,请尝试以下AWK代码。

awk '
FNR==NR{
  arr[++count1]=$0
  next
}
{
  print $0":"arr[++count2]
  count2=(count2==count1?0:count2)
}
' file2 file1

说明: 添加上述代码的详细说明。

awk '                         ##Starting awk program from here.
FNR==NR{                      ##Checking condition FNR==NR when file2 is being read.
  arr[++count1]=$0            ##Creating arr array with index of count1(increasing it with 1) and value is current line.
  next                        ##next will skip all further statements from here.
}
{
  print $0":"arr[++count2]    ##Printing current line colon and value of arr with index of ++count2.
  count2=(count2==count1?0:count2)  ##Setting count2 to 0 if its eqal to count1 else keeping it as it is.
}
' file2 file1                 ##Mentioning Input_file names here.

With your shown samples, please try following awk code.

awk '
FNR==NR{
  arr[++count1]=$0
  next
}
{
  print $0":"arr[++count2]
  count2=(count2==count1?0:count2)
}
' file2 file1

Explanation: Adding detailed explanation for above code.

awk '                         ##Starting awk program from here.
FNR==NR{                      ##Checking condition FNR==NR when file2 is being read.
  arr[++count1]=$0            ##Creating arr array with index of count1(increasing it with 1) and value is current line.
  next                        ##next will skip all further statements from here.
}
{
  print $0":"arr[++count2]    ##Printing current line colon and value of arr with index of ++count2.
  count2=(count2==count1?0:count2)  ##Setting count2 to 0 if its eqal to count1 else keeping it as it is.
}
' file2 file1                 ##Mentioning Input_file names here.
别在捏我脸啦 2025-02-03 18:55:18

这可能对您有用(gnu sed):

sed -E '1{x;s/.*/cat file2/e;x};G;s/\n/:/;P;x;s/([^\n]*)\n(.*)/\2\n\1/;x;d' file1

用file2的内容准备保留空间。

将file2附加到当前行,用替换第一个newline,然后仅打印第一行。

回到保持空间并循环第一行。

删除当前行并重复。

使用Yes and粘贴的替代性:

yes "$(cat file2)" | sed 'R /dev/stdin' file1 | paste -sd':\n'

或没有SED:

yes "$(cat file2)" | head -$(wc -l <file1) | paste -sd':\n' file1 - 

NB最终-表示循环文件的内容。

This might work for you (GNU sed):

sed -E '1{x;s/.*/cat file2/e;x};G;s/\n/:/;P;x;s/([^\n]*)\n(.*)/\2\n\1/;x;d' file1

Prep the hold space with the contents of file2.

Append file2 to the current line, replace the first newline with : and print only the first line.

Go back to the hold space and cycle the first line.

Delete the current line(s) and repeat.

Alternative using yes and paste:

yes "$(cat file2)" | sed 'R /dev/stdin' file1 | paste -sd':\n'

or without sed:

yes "$(cat file2)" | head -$(wc -l <file1) | paste -sd':\n' file1 - 

N.B. The final - represents the contents of the cycled file2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文