在Linux中的文件中搜索字符在线中的字符并进行编辑

发布于 2025-02-10 13:10:42 字数 260 浏览 2 评论 0原文

我有一个要加载到Linux中数据库中的分界字段文件。问题是每个行中的分隔字段数并不相同。因此,我需要一个shell脚本来迭代每行,并检查定界线字符的出现数量,我需要每行定界线字符13个出现。因此,如果我有10个,例如,我需要在此行的末尾添加2个额外的分界符字符。

现在我所拥有的就是这样:

#!/usr/bin/bash
while read p; do
  if
  -----------
  fi
done <myDataFile

I have a delimited fields file that I want to load into the database in Linux. The thing is that the number of delimited fields is not the same in every row. So, I need a shell script to iterate over each line and check for the number of occurrences of the delimiter character, I need 13 occurrences of the delimiter character per line. So, if I have 10 for example, I need to add 2 extra delimiter characters at the end of this line.

Now all that I have got is this:

#!/usr/bin/bash
while read p; do
  if
  -----------
  fi
done <myDataFile

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

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

发布评论

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

评论(2

甜心小果奶 2025-02-17 13:10:42

提供的信息相当稀疏。

你的定界符是什么?

定界符(引用)可以在任何字段中发生吗?

对于简单的情况,例如分隔符=“ |”并且在字段中不发生,这是一个快速awk hack。

$ cat myDataFile 
a|b|c|d|e|f|g|h|i|j|k|l|m
a|b|c|d|e|f|g|h|i|m
a|b|c|d|e|f|g|h|i|j|k|l|m
a|b|d|e|f|g|h|i|j|k|l|m
a|b|c|d|e|f|g|h|i|j|k|l|m
a|b|c|d|e|f|g|h|i|j|k|l|m
a|b|c|d|e|f|i|j|k|l|m
a|b|c|d|e|f|g|h|i|j|k|l|m

而且尴尬:

awk -F'|' '{missing=13-NF;if(missing==0){print $0}else{printf "%s",$0;for(i=1;i<=missing-1;i++){printf "|"};print "|"}}' myDataFile 
a|b|c|d|e|f|g|h|i|j|k|l|m
a|b|c|d|e|f|g|h|i|m|||
a|b|c|d|e|f|g|h|i|j|k|l|m
a|b|d|e|f|g|h|i|j|k|l|m|
a|b|c|d|e|f|g|h|i|j|k|l|m
a|b|c|d|e|f|g|h|i|j|k|l|m
a|b|c|d|e|f|i|j|k|l|m||
a|b|c|d|e|f|g|h|i|j|k|l|m

尴尬变得漂亮而解释:

{
        missing = 13 - NF      # store the number of missing fields
        if (missing == 0) {    # if all fields are present
                print $0       # just print the line 
        } else {               # otherwise
                printf "%s", $0        # first print the line
                for (i = 1; i <= missing - 1; i++) {   # then pad the line with delimiters (w/o a newline)
                        printf "|"                     
                }
                print "|"      # followed by a last one WITH a newline                     
        }
}

The information provided is rather sparse.

What's your delimiter?

Can the delimiter (quoted) occur in any of the fields?

For a simple case, e.g. delimiter="|" and doesn't occur inside the fields here's a quick awk hack.

$ cat myDataFile 
a|b|c|d|e|f|g|h|i|j|k|l|m
a|b|c|d|e|f|g|h|i|m
a|b|c|d|e|f|g|h|i|j|k|l|m
a|b|d|e|f|g|h|i|j|k|l|m
a|b|c|d|e|f|g|h|i|j|k|l|m
a|b|c|d|e|f|g|h|i|j|k|l|m
a|b|c|d|e|f|i|j|k|l|m
a|b|c|d|e|f|g|h|i|j|k|l|m

And the awk:

awk -F'|' '{missing=13-NF;if(missing==0){print $0}else{printf "%s",$0;for(i=1;i<=missing-1;i++){printf "|"};print "|"}}' myDataFile 
a|b|c|d|e|f|g|h|i|j|k|l|m
a|b|c|d|e|f|g|h|i|m|||
a|b|c|d|e|f|g|h|i|j|k|l|m
a|b|d|e|f|g|h|i|j|k|l|m|
a|b|c|d|e|f|g|h|i|j|k|l|m
a|b|c|d|e|f|g|h|i|j|k|l|m
a|b|c|d|e|f|i|j|k|l|m||
a|b|c|d|e|f|g|h|i|j|k|l|m

And the awk made pretty and explained:

{
        missing = 13 - NF      # store the number of missing fields
        if (missing == 0) {    # if all fields are present
                print $0       # just print the line 
        } else {               # otherwise
                printf "%s", $0        # first print the line
                for (i = 1; i <= missing - 1; i++) {   # then pad the line with delimiters (w/o a newline)
                        printf "|"                     
                }
                print "|"      # followed by a last one WITH a newline                     
        }
}
岛歌少女 2025-02-17 13:10:42
{m,g}awk NF=13 FS='[|]' OFS='|'

a|b|c|d|e|f|g|h|i|j|k|l|m
a|b|c|d|e|f|g|h|i|m|||
a|b|c|d|e|f|g|h|i|j|k|l|m
a|b|d|e|f|g|h|i|j|k|l|m|
a|b|c|d|e|f|g|h|i|j|k|l|m
a|b|c|d|e|f|g|h|i|j|k|l|m
a|b|c|d|e|f|i|j|k|l|m||
a|b|c|d|e|f|g|h|i|j|k|l|m
{m,g}awk NF=13 FS='[|]' OFS='|'

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