Hai para ahli, saya memiliki file teks kecil yang disimpan di input.txt dan seperti di bawah ini

sel all
sel m:all
seada FITOISY1.ASCII
mrite tmp1 all
!read tmp1 all
get/file all rrrrr 27-NOV-2020_00:00:00.000
mbt sadfile MATU.LOC
get/file all hapyness 
get/file all mention syn
get/file all pmoc r
get/file all abc 32.5
get/file all def 70.8
mrite FITOISY1 all


sel all
sel m:all
seada FITOISY2.ASCII
mrite tmp2 all
!read tmp2 all
get/file all rrrrr 27-NOV-2020_00:00:00.000
mbt sadfile MATU.LOC
get/file all hapyness 
get/file all mention syn
get/file all pmoc r
get/file all abc 32.5
get/file all def 70.8
mrite FITOISY2 all


sel all
sel m:all
seada FITOISY3.ASCII
mrite tmp3 all
!read tmp3 all
get/file all rrrrr 27-NOV-2020_00:00:00.000
mbt sadfile MATU.LOC
get/file all hapyness 
get/file all mention syn
get/file all pmoc r
get/file all abc 32.5
get/file all def 70.8
mrite FITOISY3 all

Dan saya ingin menulis beberapa nilai untuk hapyness di atas dari file teks lain values.txt dan file teksnya adalah

>
3.88499999
>
4.02930021
>
4.17359972
>

Dan hasil yang saya harapkan adalah

    sel all
    sel m:all
    seada FITOISY1.ASCII
    mrite tmp1 all
    !read tmp1 all
    get/file all rrrrr 27-NOV-2020_00:00:00.000
    mbt sadfile MATU.LOC
    get/file all hapyness 3.88499999
    get/file all mention syn
    get/file all pmoc r
    get/file all abc 32.5
    get/file all def 70.8
    mrite FITOISY1 all
    
    
    sel all
    sel m:all
    seada FITOISY2.ASCII
    mrite tmp2 all
    !read tmp2 all
    get/file all rrrrr 27-NOV-2020_00:00:00.000
    mbt sadfile MATU.LOC
    get/file all hapyness 4.02930021
    get/file all mention syn
    get/file all pmoc r
    get/file all abc 32.5
    get/file all def 70.8
    mrite FITOISY2 all
    
    
    sel all
    sel m:all
    seada FITOISY3.ASCII
    mrite tmp3 all
    !read tmp3 all
    get/file all rrrrr 27-NOV-2020_00:00:00.000
    mbt sadfile MATU.LOC
    get/file all hapyness 4.17359972
    get/file all mention syn
    get/file all pmoc r
    get/file all abc 32.5
    get/file all def 70.8
    mrite FITOISY3 all

Saya mencoba kode seperti di bawah ini tetapi tidak dapat melakukannya dengan benar, semoga para ahli akan membantu saya. Terima kasih sebelumnya.

#!/bin/sh
while readline `cat input.txt`
do
cat input.txt | grep < values.txt
    

Data Asli Yang Dipermasalahkan, Sekarang Diganti

input.txt

a.sh  21
b.ah  25
c.sh

a.sh  24
b.ah  12
c.sh

a.sh  45
b.ah  95
c.sh

a.sh  20
b.ah  11
c.sh

a.sh  21
b.ah  12
c.sh

a.sh  23
b.ah  41
c.sh

values.txt

>
14
>
22
>
43
>
96
>
45
>
61

Keluaran Asli yang Diminta

a.sh  21
b.ah  25
c.sh  14

a.sh  24
b.ah  12
c.sh  22

a.sh  45
b.ah  95
c.sh  43

a.sh  20
b.ah  11
c.sh  96

a.sh  21
b.ah  12
c.sh  45

a.sh  23
b.ah  41
c.sh  61
-3
lijun 23 Desember 2020, 09:32

3 jawaban

Jawaban Terbaik

Pendekatan lain di awk menyimpan angka dari values.txt dalam array a[], kemudian untuk file kedua, jika hanya satu bidang, tambahkan nilai dari a[] dan naikkan indeks (jika ada adalah lebih banyak record dengan hanya 1-bidang di input.txt, kemudian gunakan nilai terakhir di values.txt secara berulang.

awk -v n=1 '                                        # set n=1
    FNR==NR && /^[0-9]/ {                           # 1st file & line starts [0-9]
        a[++i]=$1                                   # store value in a
        next                                        # skip to next record
    }
    NR>FNR && NF==1 {                               # 2nd file, only 1 field
        $0=$0""((n in a) ? a[n++] : a[i])           # add value from array
    }
    NR>FNR                                          # 2nd file, print record
' values.txt input.txt

Contoh Penggunaan/Keluaran

Anda cukup memilih-salin dan tempel mouse tengah ke xterm dengan file di direktori kerja saat ini dan akan mendapatkan:

$ awk -v n=1 '                                        # set n=1
>     FNR==NR && /^[0-9]/ {                           # 1st file & line starts [0-9]
>         a[++i]=$1                                   # store value in a
>         next                                        # skip to next record
>     }
>     NR>FNR && NF==1 {                               # 2nd file, only 1 field
>         $0=$0""((n in a) ? a[n++] : a[i])           # add value from array
>     }
>     NR>FNR                                          # 2nd file, print record
> ' values.txt input.txt
a.sh  21
b.ah  25
c.sh  14

a.sh  24
b.ah  12
c.sh  22

a.sh  45
b.ah  95
c.sh  43

a.sh  20
b.ah  11
c.sh  96

a.sh  21
b.ah  12
c.sh  45

a.sh  23
b.ah  41
c.sh  61

Jika ada jumlah nilai yang tidak sama dari values.txt dan catatan dengan 1 bidang di input.txt dan Anda ingin membiarkan nilai kosong untuk catatan c.sh tambahan, Anda cukup melakukan:

            $0=$0""((n in a) ? a[n++] : "")           # add value from array

(selalu lebih baik untuk menutupi diri Anda dengan satu atau lain cara)


Setelah File Masukan Diubah

Perubahan file input sedikit rumit, tidak terlalu banyak, tetapi butuh sedikit untuk mengatasinya. Jika saya sudah benar, berikut ini akan dilakukan:

awk -v n="1" -v need="1" '                          # set n=1 and need flag
    FNR==NR && /^[0-9]/ {                           # 1st file & line starts [0-9]
        a[++i]=$1                                   # store value in a
        next                                        # skip to next record
    }
    NR>FNR && need > 0 && /^get\/file/ {       # 2nd file, need flag, ^get/file
        if (need++ == 2) {
            $0=$0""((n in a) ? a[n++] : a[i])       # add value from array
            need=0                                  # set need flag false
        }
    }
    NR>FNR && NF==0 {                               # 2nd file & blank line
        need=1                                      # set need flag true
    }
    NR>FNR                                          # 2nd file, print record
' values.txt input.txt

Contoh Penggunaan/Keluaran

$ awk -v n="1" -v need="1" '                          # set n=1 and need flag
>     FNR==NR && /^[0-9]/ {                           # 1st file & line starts [0-9]
>         a[++i]=$1                                   # store value in a
>         next                                        # skip to next record
>     }
>     NR>FNR && need > 0 && /^get\/file/ {       # 2nd file, need flag, ^get/file
>         if (need++ == 2) {
>             $0=$0""((n in a) ? a[n++] : a[i])       # add value from array
>             need=0                                  # set need flag false
>         }
>     }
>     NR>FNR && NF==0 {                               # 2nd file & blank line
>         need=1                                      # set need flag true
>     }
>     NR>FNR                                          # 2nd file, print record
> ' values.txt input.txt
sel all
sel m:all
seada FITOISY1.ASCII
mrite tmp1 all
!read tmp1 all
get/file all rrrrr 27-NOV-2020_00:00:00.000
mbt sadfile MATU.LOC
get/file all hapyness 3.88499999
get/file all mention syn
get/file all pmoc r
get/file all abc 32.5
get/file all def 70.8
mrite FITOISY1 all


sel all
sel m:all
seada FITOISY2.ASCII
mrite tmp2 all
!read tmp2 all
get/file all rrrrr 27-NOV-2020_00:00:00.000
mbt sadfile MATU.LOC
get/file all hapyness 4.02930021
get/file all mention syn
get/file all pmoc r
get/file all abc 32.5
get/file all def 70.8
mrite FITOISY2 all


sel all
sel m:all
seada FITOISY3.ASCII
mrite tmp3 all
!read tmp3 all
get/file all rrrrr 27-NOV-2020_00:00:00.000
mbt sadfile MATU.LOC
get/file all hapyness 4.17359972
get/file all mention syn
get/file all pmoc r
get/file all abc 32.5
get/file all def 70.8
mrite FITOISY3 all
3
David C. Rankin 23 Desember 2020, 09:08

EDIT: Dengan sampel OP yang baru diedit, seseorang dapat mencoba mengikuti.

awk -v count3="1" '
FNR==NR{
  if($0~/^>/){
    count2=""
    count1++
    next
  }
  value[count1 OFS count2++]=$0
  next
}
!NF{
  count4=""
  count3++
  print
  next
}
/hapyness/{
  print $0,value[count3 OFS count4++]
  next
}
1
' values.txt input.txt


Bisakah Anda mencoba mengikuti, menulis, dan menguji dengan sampel yang ditampilkan di GNU awk.

awk -v count3="1" '
FNR==NR{
  if($0~/^>/){
    count2=""
    count1++
    next
  }
  value[count1 OFS count2++]=$0
  next
}
!NF{
  count4=""
  count3++
  print
  next
}
NF<2{
  print $0,value[count3 OFS count4++]
  next
}
1
' values.txt input.txt

Penjelasan: Menambahkan penjelasan rinci di atas.

awk -v count3="1" '                      ##Starting awk program from here and setting count3 as 1 here.
FNR==NR{                                 ##Checking FNR==NR which will be TRUE when values.txt is being read. 
  if($0~/^>/){                           ##Checking condition if line starts with > then do following.
    count2=""                            ##Nullify count2 variable here.
    count1++                             ##Increase count1 with 1 here.
    next                                 ##next will skip all further statements from here.
  }
  value[count1 OFS count2++]=$0          ##Creating value array with count1 OFS count2(with increasing count by 1) and its value is current line.
  next                                   ##next will skip all further statements from here.
}
!NF{                                     ##If line is an empty line then do following.
  count4=""                              ##Nullify count4 here.
  count3++                               ##Imcrease count3 with 1 here.
  print                                  ##Printing current line here.
  next                                   ##next will skip all further statements from here.
}
NF<2{                                    ##Checking if number of fields is lesser than 2 then do following.
  print $0,value[count3 OFS count4++]    ##Printing current line with value of array value.
  next                                   ##next will skip all further statements from here.
}
1                                        ##will print the current line.
' values.txt input.txt                   ##Mentioning Input_file names here.
3
RavinderSingh13 23 Desember 2020, 08:35

Silakan coba kode di bawah ini.

#!/bin/sh

cnt=0
while read line;
do
    if [ $line != '>' ]
    then
        cnt=$(( $cnt + 1 ))
        tmp="c.sh  ""$line"
        sed -z -i "s/c.sh/$tmp/$cnt" input.txt
    fi
done < 'values.txt'

(* langsung beroperasi pada file, tidak dioptimalkan)

-1
Nishad C M 23 Desember 2020, 08:38