I am wondering how to use Awk to process every 2 lines of data instead of every one. By default the record separator (RS) is set to every new line, how can I change this to every 2 lines.
相关问题
- JQ: Select when attribute value exists in a bash a
- bash print whole line after splitting line with if
- “command not found” errors in expect script execut
- grep using grep results
- softlinks atime and mtime modification
相关文章
- Check if directory exists on remote machine with s
- Reverse four length of letters with sed in unix
- Compile and build with single command line Java (L
- Launch interactive SSH bash session from PHP
- BASH: Basic if then and variable assignment
- Bash script that creates a directory structure
- Test if File/Dir exists over SSH/Sudo in Python/Ba
- How can I create a “tmp” directory with Elastic Be
If you want to merge lines, use the
paste
utility:This is a bit hackish, but it's a literal answer to your question:
Set the record separator to a regex which matches two lines. Then for each line, set
$0
to the record terminator (which is what matched the regex inRS
). This performs field splitting onFS
. The print statement is just a demonstration place holder.Note that
$0
will contain two newlines, but the fields will not contain any newlines.Divide&Conquer: do it in two steps:
to separate each two-line record:
NR%2==0 {print ""}
set record separator to blank line:
BEGIN {RS=""}
Advantage: In the second
awk
process you have all fields of the two lines accessible as$1 to $NF
.Note:
$1=$1
is used here to enforce an update on$0
(the whole record).This guaranties that the output prints the two-line record on one line.
Once you modify a field in your program when you process the two-line records this is no longer required.
It depends of what you want to achieve, but one way is to use the
getline
instruction. For each line, read next one and save it in a variable. So you will have first line in$0
and second one ineven_line
: