小编RAH*_*sen的帖子

Linux - 循环遍历每一行的每个元素

我有一个包含以下信息的文本文件:

cat test.txt
a,e,c,d,e,f,g,h
d,A,e,f,g,h
Run Code Online (Sandbox Code Playgroud)

我希望遍历每一行,然后为每一行打印与 e 不同的所有字符的索引。所以理想的输出要么是制表符分隔符,要么是逗号分隔符

1 3 4 6 7 8
1 2 4 5 6

or

1,3,4,6,7,8
1,2,4,5,6
Run Code Online (Sandbox Code Playgroud)

我设法遍历每一行并打印索引,但结果被打印到同一行而不是分开的。

while read line;do echo "$line" | awk -F, -v ORS=' ' '{for(i=1;i<=NF;i++) if($i!="e") {print i}}' ;done<test.txt
Run Code Online (Sandbox Code Playgroud)

结果是

1 3 4 6 7 8 1 2 4 5 6

如果我只使用 awk awk -F, -v ORS=' ' '{for(i=1;i<=NF;i++) if($i!="e") {print i}}'

我得到相同的输出

任何人都可以通过分隔线来帮助我解决这个特定问题。

awk grep for-loop

2
推荐指数
1
解决办法
46
查看次数

使用 int 而不是 const int 声明 0 的 int 数组

我正在用 C++ 编码。

如果我想声明一个 int 数组,让我们说 100 长,全部由 0 组成,我知道您可以像这样使用 cont int 来做到这一点

const int N = 100;
int array[N] = {0};
Run Code Online (Sandbox Code Playgroud)

哪个当然有效,但我需要使用 int 来完成,并且我在堆栈溢出上看到了多个帖子,说明在将 N 定义为 int 时不能这样做。

我的问题是我想声明由未知大小的 0 组成的数组,大小来自库 htslib faidx_seq_len() 的函数,该函数将 .fasta 格式的 DNA 文件作为输入,该函数返回一个 int,因此,我无法更改此返回格式,因此我试图将其转换为 const it。

因此,对于我正在使用的功能,我将执行以下操作:

// seq_ref & chr_name -> variables i get when loading the sequence file i am using

int chr_len = faidx_seq_len(seq_ref,chr_name); // with chr_len being an integer of the size of a given chromosome, which could be 249250621. …
Run Code Online (Sandbox Code Playgroud)

c++ arrays

0
推荐指数
1
解决办法
61
查看次数

条件跳转或移动取决于 for 循环中 strcat 的未初始化值

我有一个包含 3 条染色体字符串的文件,我想将其连接成一个基因组。然后我必须跨多个线程访问这个串联字符串(我使用 pthread_t)。为此,我必须在提取数据时使用 pthread_mutex_lock,然后使用 strcat 连接使用 const char* 函数 fai_fetch 提取的数据,然后将数据保存为 char* (见下文)。

// genome_size the size of all the chromosomes together
// chr_total the number of chromosomes I wish to concatenate
char* genome = (char*) malloc(sizeof(char) * (genome_size+chr_total));

for (int i = 0; i < chr_total; i++){
    pthread_mutex_lock(&data_mutex);
    const char *data = fai_fetch(seq_ref,chr_names[i],&chr_sizes[i]);
    pthread_mutex_unlock(&data_mutex);
    //sprintf(&genome[strlen(genome)],data);
    strcat(genome,data);  
    //sprintf(genome+strlen(genome),data); //All three gives conditional jump or move error

    //sprintf(genome,data); // THIS SOLVES VALGRIND ISSUE ONE BUT DOES NOT GIVE A CONCATENATED …
Run Code Online (Sandbox Code Playgroud)

c c++ malloc valgrind

0
推荐指数
1
解决办法
5920
查看次数

标签 统计

c++ ×2

arrays ×1

awk ×1

c ×1

for-loop ×1

grep ×1

malloc ×1

valgrind ×1