If I run the command cat file | grep pattern, I get many lines of output. How do you concatenate all lines into one line, effectively replacing each "\n" with "\" " (end with " followed by space)?

cat file | grep pattern | xargs sed s/\n/ /g isn't working for me.

4

11 Answers

Use tr '\n' ' ' to translate all newline characters to spaces:

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

Note: grep reads files, cat concatenates files. Don't cat file | grep!

Edit:

tr can only handle single character translations. You could use awk to change the output record separator like:

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

This would transform:

one two three 

to:

one" two" three" 
5

Piping output to xargs will concatenate each line of output to a single line with spaces:

grep pattern file | xargs 

Or any command, eg. ls | xargs. The default limit of xargs output is ~4096 characters, but can be increased with eg. xargs -s 8192.

3

In bash echo without quotes remove carriage returns, tabs and multiple spaces

echo $(cat file) 
6

This could be what you want

cat file | grep pattern | paste -sd' ' 

As to your edit, I'm not sure what it means, perhaps this?

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

(this assumes that ~ does not occur in file)

1

This is an example which produces output separate by commas. You can replace the comma by whatever separator you need.

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

produces:

1,2,3,4,5 

The fastest and easiest ways I know to solve this problem:

When we want to replace the new line character \n with the space:

xargs < file 

xargs has own limits on the number of characters per line and the number of all characters combined, but we can increase them. Details can be found by running this command: xargs --show-limits and of course in the manual: man xargs

When we want to replace one character with another exactly one character:

tr '\n' ' ' < file 

When we want to replace one character with many characters:

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

First, we replace the newline characters \n for tildes ~ (or choose another unique character not present in the text), and then we replace the tilde characters with any other characters (many_characters) and we do it for each tilde (flag g).

Here is another simple method using awk:

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

Also, if your file has columns, this gives an easy way to concatenate only certain columns:

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

I like the xargs solution, but if it's important to not collapse spaces, then one might instead do:

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

That will replace newlines for spaces, without substituting the last line terminator like tr '\n' ' ' would.

This also allows you to use other joining strings besides a space, like a comma, etc, something that xargs cannot do:

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

Here is the method using ex editor (part of Vim):

  • Join all lines and print to the standard output:

    $ ex +%j +%p -scq! file 
  • Join all lines in-place (in the file):

    $ ex +%j -scwq file 

    Note: This will concatenate all lines inside the file it-self!

Probably the best way to do it is using 'awk' tool which will generate output into one line

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

It will merge all lines into one with space delimiter

1

On red hat linux I just use echo :

echo $(cat /some/file/name)

This gives me all records of a file on just one line.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy