简体   繁体   中英

paste command on to different line number files

If I have two files with different line numbers, I only want to output the same lines and omit the excess lines in the larger file.

What paste command should I wrote?

I tried:

paste -d: file1 file2|awk  -F ':'  '{print $1,$7}'

But it did not work.

There is no such option in popular versions of paste ; but it's not hard to do in Awk.

awk 'NR==FNR { a[NR]=$0; next }
    ! (FNR in a) { exit 0 }
    { print a[FNR] ":" $0 }' file1 file2

FNR is the line number within the current file, and NR is the line number accrued across input files. The first condition will be true while the first input file is being processed; then, we print pairs until we run out.

This simple version is good enough if you just need two files; generalizing to three or more will require fairly significant refactoring, as the problem is more complex then.

As a quick workaround, if this is a one-off requirement, you can start with two files and pipe into another invocation with one more input file, etc until you have processed all files.

If, as your example suggests, you only want to extract some fields, maybe change the $0 s (the whole input line) to $1 (the first field) or whatever is suitable.

Just for fun, here is a Python implementation which doesn't need to read the first file's lines into memory, and handles an arbitrary number of input files (probably subject to the number of open files allowed by your OS, though). Option parsing and error checking left as an exercise.

from sys import argv

h = []
for filename in argv[1:]:
    h.append(open(filename, 'Ur'))
while True:
    line = []
    try:
        for handle in h:
            result = handle.readline()
        if result == '':
            raise StopIteration
        line.append(result.rstrip('\n'))
    print(':'.join(line))
    except StopIteration:
        break
for handle in h:
    handle.close()

alternative to awk solution, especially if you are not editing fields

$ join <(cat -n file1) <(cat -n file2) | cut -d' ' -f2- 

decorate/undecorate pattern. Add line numbers, merge 1-1 with the line numbers and strip them at the end.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM