怎麼使用 bash 合併兩行變成一行呢?
簡單提供兩種作法 可以使用 xargs 或是 awk
% cat /tmp/ex
1
2
3
4
5
6
% cat /tmp/ex | xargs -n 2 -d '\n'
1 2
3 4
5 6
-n max-args, --max-args=max-args
Use at most max-args arguments per command line. Fewer than max-args arguments will be
used if the size (see the -s option) is exceeded, unless the -x option is given, in which
case xargs will exit.
--delimiter=delim, -d delim
Input items are terminated by the specified character. The specified delimiter may be a
single character, a C-style character escape such as \n, or an octal or hexadecimal escape
code. Octal and hexadecimal escape codes are understood as for the printf command.
Multibyte characters are not supported. When processing the input, quotes and backslash
are not special; every character in the input is taken literally. The -d option disables
any end-of-file string, which is treated like any other argument. You can use this option
when the input consists of simply newline-separated items, although it is almost always
better to design your program to use --null where this is possible.
% cat /tmp/ex | awk 'NR%2{printf "%s ",$0;next;}{print;}'
1 2
3 4
5 6
NR current record number in the total input stream.
There are two output statements, print and printf.
print writes $0 ORS to standard output.
print expr1, expr2, ..., exprn
writes expr1 OFS expr2 OFS ... exprn ORS to standard output. Numeric expressions
are converted to string with OFMT.
printf format, expr-list
duplicates the printf C library function writing to standard output. The complete
ANSI C format specifications are recognized with conversions %c, %d, %e, %E, %f, %g,
%G, %i, %o, %s, %u, %x, %X and %%, and conversion qualifiers h and l.
Program flow at the pattern {action} level can be changed with the
next
exit opt_expr
statements. A next statement causes the next input record to be read and pattern testing to
restart with the first pattern {action} pair in the program. An exit statement causes immediate
execution of the END actions or program termination if there are none or if the exit occurs in an
END action. The opt_expr sets the exit value of the program unless overridden by a later exit or
subsequent error.
沒有留言:
張貼留言