programing

출력의 여러 줄을 한 줄에 연결하려면 어떻게 해야 합니까?

oldcodes 2023. 4. 19. 23:28
반응형

출력의 여러 줄을 한 줄에 연결하려면 어떻게 해야 합니까?

명령어를 실행하면cat file | grep pattern출력 라인이 많이 나옵니다.모든 라인을 한 줄로 연결하여 각 라인을 효과적으로 대체하려면 어떻게 해야 합니까?"\n"와 함께"\" "(로 끝나다"(공백에 이어)

cat file | grep pattern | xargs sed s/\n/ /g나한테는 안 먹혀요.

사용하다tr '\n' ' '모든 줄바꿈 문자를 공백으로 변환하려면:

$ grep pattern file | tr '\n' ' '

주의:grep파일 읽기,cat파일을 연결합니다.하지 마cat file | grep!

편집:

tr는 단일 문자 변환만 처리할 수 있습니다.사용할 수 있습니다.awk출력 레코드 구분 기호를 다음과 같이 변경합니다.

$ grep pattern file | awk '{print}' ORS='" '

이 경우 다음과 같이 변환됩니다.

one
two 
three

대상:

one" two" three" 

파이프 출력 대상xargs는 출력의 각 행을 공백이 있는 단일 행에 연결합니다.

grep pattern file | xargs

또는 임의의 명령어(예: ls | xargs. 디폴트 제한은xargs출력은 최대 4096자이지만 다음과 같이 늘릴 수 있습니다. xargs -s 8192.

배쉬 중echo따옴표 없이 캐리지 리턴, 탭 및 여러 공백을 제거합니다.

echo $(cat file)

이게 네가 원하는 것일 수도 있어

cat file | grep pattern | paste -sd' '

당신의 편집에 대해서입니다만, 무슨 뜻인지 잘 모르겠습니다만, 혹시 이것은 아닐까요?

cat file | grep pattern | paste -sd'~' | sed -e 's/~/" "/g'

(이것은, 다음과 같은 것을 전제로 하고 있습니다.~에서는 발생하지 않습니다.file)

다음 예에서는 쉼표로 구분된 출력을 보여 줍니다.필요한 구분 기호로 쉼표를 대체할 수 있습니다.

cat <<EOD | xargs | sed 's/ /,/g'
> 1
> 2
> 3
> 4
> 5
> EOD

생산:

1,2,3,4,5

이 문제를 해결하는 가장 빠르고 쉬운 방법은 다음과 같습니다.

문자를 바꾸려는 경우 \n 공백 포함:

xargs < file

xargs에는 행당 문자 수 및 모든 문자를 합친 수에 대한 제한이 있지만 늘릴 수 있습니다.자세한 내용은 다음 명령을 실행하여 확인할 수 있습니다.xargs --show-limits물론 매뉴얼에는 다음과 같은 내용이 기재되어 있습니다.man xargs

문자를 다른문자로 바꾸려면 다음 절차를 수행합니다.

tr '\n' ' ' < file

문자를 여러 문자로 바꾸는 경우:

tr '\n' '~' < file | sed s/~/many_characters/g

먼저 줄바꿈 문자를 바꿉니다.\n길드를 위해서~(또는 텍스트에 없는 다른 고유 문자를 선택) 그런 다음 칠드 문자를 다른 문자로 바꿉니다(many_characters각 칠드에 대해 실행합니다(예:g).

여기 또 다른 간단한 방법이 있습니다.awk:

# cat > file.txt
a
b
c

# cat file.txt | awk '{ printf("%s ", $0) }'
a b c

또한 파일에 열이 있는 경우 특정 열만 쉽게 연결할 수 있습니다.

# cat > cols.txt
a b c
d e f

# cat cols.txt | awk '{ printf("%s ", $2) }'
b e

마음에 들어요.xargs그러나 공간을 축소하지 않는 것이 중요한 경우 대신 다음을 수행할 수 있습니다.

sed ':b;N;$!bb;s/\n/ /g'

이렇게 하면 공백의 줄바꿈이 바뀝니다.마지막 줄바꿈은 다음과 같습니다.tr '\n' ' '그럴 것이다.

이것은 또한 쉼표와 같은 공백 외에 다른 결합 문자열을 사용할 수 있습니다.xargs할 수 없습니다.

$ seq 1 5 | sed ':b;N;$!bb;s/\n/,/g'
1,2,3,4,5

에디터(Vim의 일부)를 사용하는 방법은 다음과 같습니다.

  • 모든 행을 결합하고 표준 출력으로 인쇄:

    $ ex +%j +%p -scq! file
    
  • (파일 내의) 모든 행에 임플레이스 결합:

    $ ex +%j -scwq file
    

    주의: 이렇게 하면 파일 내의 모든 행이 자동으로 연결됩니다!

가장 좋은 방법은 출력을 한 줄로 생성하는 'awk' 도구를 사용하는 것입니다.

$ awk ' /pattern/ {print}' ORS=' ' /path/to/file

모든 행을 공백 구분 기호와 함께 하나로 병합합니다.

paste -sd'~'오류를 범하다에러가 발생하고 있습니다.

Here's what worked for me on mac using Mac에서는 다음과 같이 작업을 했습니다.bash

cat file | grep pattern | paste -d' ' -s -

부에서man paste .. ..

-d list     Use one or more of the provided characters to replace the newline characters instead of the default tab.  The characters
                 in list are used circularly, i.e., when list is exhausted the first character from list is reused.  This continues until
                 a line from the last input file (in default operation) or the last line in each file (using the -s option) is displayed,
                 at which time paste begins selecting characters from the beginning of list again.

                 The following special characters can also be used in list:

                 \n    newline character
                 \t    tab character
                 \\    backslash character
                 \0    Empty string (not a null character).

                 Any other character preceded by a backslash is equivalent to the character itself.

     -s          Concatenate all of the lines of each separate input file in command line order.  The newline character of every line
                 except the last line in each input file is replaced with the tab character, unless otherwise specified by the -d option.
                 If ‘-’ is specified for one or more of the input files, the standard input is used; standard input is read one line at a time,  

순환적으로, '-'의 각 인스턴스에 대해.

red hat linux에서는 echo만 사용합니다.

echo $(cat /some/file/name)

이렇게 하면 한 줄에 파일 기록이 모두 표시됩니다.

언급URL : https://stackoverflow.com/questions/15580144/how-to-concatenate-multiple-lines-of-output-to-one-line

반응형