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.
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
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
)
In bash echo
without quotes remove carriage returns, tabs and multiple spaces
echo $(cat file)
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
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.
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
).
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
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
Source: Stackoverflow.com