How can I "merge" patterns in a single line?
Solution 1
{...pipeline...} | paste -d " " - -
That says: "read a line from stdin (the first -
), read another line from stdin (the second -
), then join them with a space"
a bash-specific technique:
$ x=$(grep -o pattern. test.txt)
$ echo "$x"
pattern1
pattern2
$ mapfile -t <<< "$x"
$ echo "${MAPFILE[*]}"
pattern1 pattern2
ref: http://www.gnu.org/software/bash/manual/bashref.html#index-mapfile
Solution 2
I'll put three versions different methods in a row
AWK
printf %s\\n pattern1 pattern2 | awk -vRS="\n" -vORS=" " '1; END {print RS}'
SED
printf %s\\n pattern1 pattern2 | sed '$!N;s/\n/ /'
TR
printf %s\\n pattern1 pattern2 | tr '\n' ' '; echo
And there are many more.
Solution 3
With sed
, you can do this:
<your previous commands> | sed '{N; s/\n/ /}'
-
N;
tellssed
to add the next line into the pattern space, so nowsed
is working with both lines. -
s/\n/ /
replaces the newline character with a space, "merging" the two lines together.
Solution 4
you can do it using shell script or in command-line, just put the output of the command in a variable then echo
it:
# x=$(grep -e "pattern1\|pattern2" test)
# printf '%s\n' "$x"
pattern1 pattern2
Solution 5
A simple way, pipe output to xargs
:
$ echo -e 'a\nb' | xargs
a b
This only works with small ouput, because it's limited by maximum characters per command line. The largest value depends on system, you can get this value using getconf ARG_MAX
.
Related videos on Youtube
Jim
Updated on September 18, 2022Comments
-
Jim over 1 year
I am doing grep and sed and I get 2 lines of a file that I am interested in. How can I get these lines in a single line ending with the new line character?
Now I am getting:pattern1 pattern2
I would like to get
pattern1 pattern2 \n
-
slm over 9 yearsCan you share more of the sample data?
-
slm over 9 yearsPaste is the tool for the job, if you're intending to merge multiple lines of output into one.
-
Ludwig Schulze over 9 yearsany specific tool? How do you plan to do this?
-
-
Jim over 9 yearsWhat is
- -
? Is the command like that? -
Angel Todorov over 9 yearsunquoted, the variable is also subject to filename expansion, so if you have any glob characters (like
*
or?
) in the result, the values may change. -
Nidal over 9 years@glennjackman, thanks for mention this, how can we fix this?
-
Marek Zakrzewski over 9 years@Networker by not using backticks but
$()
instead.. and using printfprintf '%s\n' "$x"
-
Angel Todorov over 9 yearsI would expect that
printf '%s\n' "$x"
will NOT remove the "internal" newline. -
Angel Todorov over 9 yearsThe
tr
solution will swallow the ending newline, so you might want to add; echo
to that one -
Marek Zakrzewski over 9 years@glennjackman ah right! Thanks for the hint. You are free to modify the answer though. I missed that bit!
-
slm over 9 years@Jim - yes that's correct. Those are telling
paste
how you want it to process the input. 2 at a time. Add more to do 3 at at time.seq 10 | paste -d " " - -
. -
Angel Todorov over 9 yearsUh, no. That will execute the output of the sed command.