标签: cat

cat 和 fold unix 命令的区别

我想知道以下 2 个命令catfold命令之间的区别?我只是使用fold命令,它的作用就像cat命令一样。

fold file1.txt
cat fold.txt (both are using to display the contents of the file)
Run Code Online (Sandbox Code Playgroud)

unix cat

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

文件名中至少包含 2 个指定组件的 cat 文件

我有看起来像这样的文件名:

act-art-atr-phm-pro-psy
act-art-atr-phm-pro-sta
act-art-atr-pro-psy-rel-sta
act-art-atr-pro-tme
act-art-atr-psy
act-art-atr-psy-qud-sta
act-art-atr-psy-sta
act-art-atr-rel
act-art-atr-sta
act-art-com
Run Code Online (Sandbox Code Playgroud)

我想要cat包含至少两个我手动指示的特定单词(以“-”分隔)的文件。

例如:我想要cat所有至少包含“act”和“psy”的文件,因此生成的文件将包含文件中的所有内容

act-art-atr-phm-pro-psy
act-art-atr-pro-psy-rel-sta
act-art-atr-psy
act-art-atr-psy-qud-sta
act-art-atr-psy-sta
Run Code Online (Sandbox Code Playgroud)

我不确定能够做到这一点的直接正则表达式,因为每个文件的文件名顺序都是唯一的。

是否有特定regex的文件可以搜索包含至少 2 个以“-”分隔的三个字母名称cat的文件,我可以将其用于所有相应的文件?

或者,如果我提出的regex+cat策略不是最好的,那么甚至可以采用更有效的方法来处理此任务。

regex file cat

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

Bash:在角色之间捕捉一个文件

我已经尝试了各种解决方案来找到一个很好的方法来通过一个以特定单词开头的文件,并以特定的单词结束.

假设我有一个名为的文件states.txt包含:

Alabama
Alaska
Arizona
Arkansas
California
Colorado
Connecticut
Delaware
Florida
Georgia
Hawaii
Idaho
Illinois 
Indiana
Iowa
Kansas
Kentucky
Louisiana
Maine
Maryland
Massachusetts
Michigan
Minnesota
Mississippi
Missouri
Montana 
Nebraska
Nevada
New Hampshire
New Jersey
New Mexico
New York
North Carolina
North Dakota
Ohio
Oklahoma
Oregon
Pennsylvania 
Rhode Island
South Carolina
South Dakota
Tennessee
Texas
Utah
Vermont
Virginia
Washington
West Virginia
Wisconsin
Wyoming
Run Code Online (Sandbox Code Playgroud)

我想cat states.txt并获得以下开头Idaho和结束的状态South Dakota.

我也想忽略这样一个事实,即状态是按字母顺序排列的(我要的实际文件内容不按此顺序排列).

结果应如下所示:

Idaho
Illinois 
Indiana
Iowa
Kansas
Kentucky …
Run Code Online (Sandbox Code Playgroud)

bash awk grep sed cat

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

shell 命令中缺少右括号?

我正在尝试向我的 makefile 添加一条警告消息。Make 无法找到右括号。

以下内容来自我的 GNUmakefile:

341  ALIGNED_ACCESS = $(shell cat config.h | $(EGREP) -c "^#define NO_UNALIGNED_DATA_ACCESS")
342  ifeq ($(ALIGNED_ACCESS),0)
343  $(info WARNING: NO_UNALIGNED_DATA_ACCESS is not defined in config.h)
344  endif
Run Code Online (Sandbox Code Playgroud)

提出投诉:

$ make
GNUmakefile:341: *** unterminated call to function 'shell': missing ')'.  Stop.
Run Code Online (Sandbox Code Playgroud)

我真的在 makefile 中另外 25 次,所以我不确定为什么它找不到上面 shell 的正确括号。

我还将命令移到第 1 行进行测试,我得到了相同的消息(以行号更改为模)。为了好玩,我添加了一个不平衡的右括号,但我得到了同样的信息。

为什么找不到正确的括号?

如何强制执行命令?

shell makefile gnu-make cat

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

使用系统调用(打开、读取、写入)来显示文件内容

我正在尝试实现一个名为 displaycontent 的命令,该命令将文本文件名作为参数并显示其内容。我将在 Linux 中使用open()read()write()close()系统调用来执行此操作。它应该有点像cat用于显示文件内容的 UNIX命令。

这是我到目前为止所拥有的:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
    int fd;
    char content[fd];   

    errno = 0;
    fd = open(argv[1], O_RDONLY);


    if(fd < 0)
    {
        printf("File could not be opened.\n");
        perror("open");
        return 1;
    }
    else 
    {

        read(fd, content, sizeof(content)-1);
        write(1, content, sizeof(content)-1);
    }

return 0;
}
Run Code Online (Sandbox Code Playgroud)

我有一个名为 hello2.txt 的文件,其中包含以下文本: hellooooooooooooooo

当我这样做时./displaycontent hello2.txt …

c system-calls cat

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

3个动作,cat,grep然后wc,来自之前的信息

我搜索过但到目前为止没有找到任何东西 我希望列出一个目录,然后cat每个文件的唯一项目然后使用这两个项目来计算文件中的项目.

第一个ls

ls
Run Code Online (Sandbox Code Playgroud)
  • FILE1.TXT
  • FILE2.TXT
  • file3.txt等...

第二个grep每个独特

cat $file | awk '{print $8}' |  sort | uniq 
Run Code Online (Sandbox Code Playgroud)

哪个应输出数字

  • 83886096
  • 1040187393
  • 201326673等...

第三,使用找到的uniq数字grep它来自的文件来计算有多少

cat $file | grep $output | wc -l
Run Code Online (Sandbox Code Playgroud)

并以某种方式在行上获得$ file $ output $ count的良好输出

提前谢谢你

我假设我将不得不做这种性质的事情,但更复杂(因为我无法让它工作)

FILE="$(ls -1)"
ls > list.txt
input=list.txt
while read line
do
OUTPUT=cat ${FILE} | awk '{print $8}' |  sort | uniq 
cat ${FILE} | grep ${OUTPUT} | wc -l
done < "$input"
Run Code Online (Sandbox Code Playgroud)

当我运行它,似乎有点工作,我得到以下输出

grep: 0652-033 Cannot open 83886096.
       0
grep: 0652-033 Cannot …
Run Code Online (Sandbox Code Playgroud)

unix bash awk grep cat

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

在R中对齐打印字符串的标准工具是什么?

这是一系列的cat陈述

words = c("Hello","bar","ROCKnROLL","R","Supercalifragilisticexpialidocious")
for (i in 1:length(words))
{
    cat(paste0(words[i], "\t: ",nchar(words[i]),"\n"))
}

Hello   : 5
bar : 3
ROCKnROLL   : 9
R   : 1
Supercalifragilisticexpialidocious  : 34
Run Code Online (Sandbox Code Playgroud)

我该怎么做才能像这样对齐它们

Hello                               : 5
bar                                 : 3
ROCKnROLL                           : 9
R                                   : 1
Supercalifragilisticexpialidocious  : 34
Run Code Online (Sandbox Code Playgroud)

或者像这样

                             Hello  : 5
                               bar  : 3
                         ROCKnROLL  : 9
                                 R  : 1
Supercalifragilisticexpialidocious  : 34
Run Code Online (Sandbox Code Playgroud)

或最终喜欢这个

             Hello                  : 5
              bar                   : 3
           ROCKnROLL                : 9
               R                    : 1
Supercalifragilisticexpialidocious  : 34
Run Code Online (Sandbox Code Playgroud)

printing string r alignment cat

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

管道命令输出到 awk

我想将命令的输出通过管道传输到 awk。我想将该数字添加到现有 .txt 文件中新列的每一行。新列应该在最后,不一定是第 2 列。

$命令 1

4512438
Run Code Online (Sandbox Code Playgroud)

$输入.txt

A
B
C
D
Run Code Online (Sandbox Code Playgroud)

$desired_output.txt

A   4512438
B   4512438
C   4512438
D   4512438
Run Code Online (Sandbox Code Playgroud)

我想我需要按照以下方式做一些事情。我不确定如何指定管道进入新列 - 这个 awk 命令只会将整数添加到列中。

$命令1 | awk -F, '{$(NF+1)=++i;}1' OFS=, input.txt > desired_ouput.txt

awk pipe cat

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

在 Bash 与 ZSH 中结合 heredoc 和输入重定向

以下代码在 ZSH 上运行没有问题,将 heredoc 与文件 test.csv 的内容相结合:

cat <<EOF <test.csv
id,name,age
EOF
Run Code Online (Sandbox Code Playgroud)

如何在 Bash 中编写相同的命令?

bash zsh heredoc cat io-redirection

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

如何 cat 多行 bash 变量?

当我尝试保存 bash 变量列,然后尝试将它们组合在一起时,出现错误。

这些是变量:

$ a_var=$(grep -i "word" {input} | awk '{print $3}')

$ echo $a_var
hello1
hello2
hello3

$ b_var=$(grep -i "otherword" {input} | awk '{print $3}')

$ echo $b_var
hello10
hello11
hello12
Run Code Online (Sandbox Code Playgroud)

但是当我尝试时:

$ new_var=$(cat $a $b)
Run Code Online (Sandbox Code Playgroud)

我得到一些无法保存到文件或新变量的奇怪输出。

cat: hello1: No such file or directory
cat: hello2: No such file or directory
cat: hello3: No such file or directory
cat: hello10: No such file or directory
cat: hello11: No such file or directory
cat: hello12: No such file …
Run Code Online (Sandbox Code Playgroud)

unix variables bash cat

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

标签 统计

cat ×10

bash ×4

awk ×3

unix ×3

grep ×2

alignment ×1

c ×1

file ×1

gnu-make ×1

heredoc ×1

io-redirection ×1

makefile ×1

pipe ×1

printing ×1

r ×1

regex ×1

sed ×1

shell ×1

string ×1

system-calls ×1

variables ×1

zsh ×1