How to append the filename to every

后端 未结 1 1596
甜味超标
甜味超标 2021-01-16 10:16

I have 32 cells in a file so i need to read the name of the file and append the file name at the starting of every line how can i do it

example

MACRO         


        
1条回答
  •  伪装坚强ぢ
    2021-01-16 10:31

    This can be done with a relatively simple awk script, as per the following transcript:

    pax$ printf "MACRO XYZ\nline 2\nline3\n\nMACRO GEN\nline 6\n\n\n" | awk '
    ...$    $1=="MACRO"{mac=$2;print;next}$NF>0{print mac" "$0;next}{print}'
    MACRO XYZ
    XYZ line 2
    XYZ line3
    .
    MACRO GEN
    GEN line 6
    .
    . 
    

    (those . characters are added by me just to show where the blank lines are).

    Expanding on the script:

    $1 == "MACRO" {      # If first word is macro:
        mac=$2;          #    store second word,
        print;           #    print line,
        next             #    and go get next line.
    }
    $NF > 0 {            # Otherwise, if line has some fields:
        print mac" "$0;  #    print stored macro name before line,
        next             #    and go get next line.
    }
    {                    # Otherwise (lines with no fields):
        print            #    just print it as is.
    }
    

    0 讨论(0)
提交回复
热议问题