简体   繁体   中英

How to get contents from 2 files and append that contents to a new file using a shell script

I have 2 files which look like this

file1.txt

GYFUFGYO1  KMP-app   james@qt.com  CODE_SMELL
GYFUFGYO2  KMP-app   james@qt.com  CODE_SMELL
GYFUFGYG3  AFP-Login   nathan@qt.com  BUG
GYFUFGYG4  AFP-Login   nathan@qt.com  BUG
GYFUFGYO5  KMP-app   james@qt.com  CODE_SMELL
GYFUFGYO6  KMP-app   james@qt.com  CODE_SMELL

file2.txt

MAC 135 2022-09-02-09:35

I have to append those contents(file1, file2) to file3.txt then expected output is

GYFUFGYO1  KMP-app   james@qt.com  CODE_SMELL MAC 135 2022-09-02-09:35
GYFUFGYO2  KMP-app   james@qt.com  CODE_SMELL MAC 135 2022-09-02-09:35
GYFUFGYG3  AFP-Login   nathan@qt.com  BUG     MAC 135 2022-09-02-09:35
GYFUFGYG4  AFP-Login   nathan@qt.com  BUG     MAC 135 2022-09-02-09:35
GYFUFGYO5  KMP-app   james@qt.com  CODE_SMELL MAC 135 2022-09-02-09:35
GYFUFGYO6  KMP-app   james@qt.com  CODE_SMELL MAC 135 2022-09-02-09:35

this is what I tried

paste -s file1.txt file2.txt > file3.txt

then output is (file3.txt)

GYFUFGYO1  KMP-app   james@qt.com  CODE_SMELL   GYFUFGYO2  KMP-app   james@qt.com  CODE_SMELL   GYFUFGYG3  AFP-Login   nathan@qt.com  BUG       GYFUFGYG4  AFP-Login   nathan@qt.com  BUG       GYFUFGYO5  KMP-app   james@qt.com  CODE_SMELL   GYFUFGYO6  KMP-app   james@qt.com  CODE_SMELL
BAU 133 2022-09-02-09:35

Can someone help me to figure out this? Thanks in advance!

Assuming file2.txt has just one line as shown, how about a paste solution:

paste file1.txt <(yes $(<file2.txt) | head -n $(wc -l <file1.txt))
  • yes $(<file2.txt) repeats the line of file2.txt.
  • $(wc -l <file1.txt) returns the line count of file1.txt.
  • head -n $(wc -l <file1.txt) prints as many lines as file1.txt.

Assuming that file2.txt has always exactly 1 line, then you might exploit GNU sed 's hold space, consider following simple example let file1.txt content be

123
456
789

and file2.txt file content be

ABC

then

sed -e '1{h;d}' -e 'G;s/\n/ /' file2.txt file1.txt

gives output

123 ABC
456 ABC
789 ABC

Explanation: I register two actions, for 1 line ( globally , that is first line of first of mentioned file, observe that it is file2.txt file1.txt not in reverse) I instruct GNU sed to save line into hold space ( h ) and immediately go further without output anything ( d ), for other lines I instruct GNU sed to append newline and content of hold space ( G ) and then replace said newline with space characters.

(tested in GNU sed 4.5)

paste expects two file of equal length. I'm guessing you basically want this common Awk two-liner:

awk 'BEGIN { FS=OFS="\t" } NR==FNR { a[++i] = $0; next }
    { print $0, a[(FNR-1) % i + 1] }' file2.txt file1.txt

(I have assumed your files are tab-separated; it's not clear from your question.)

awk 'NR==FNR{v=$0; next} {print $0, v}' file2.txt file1.txt

Set OFS to \t or pipe the output to column -t to get whatever separators/alignment you like if a blank isn't adequate.

assuming file2 only has 1 line

cat temptest_f_00_01.txt 
123
456
789

cat temptest_f_00_02.txt
ABC
 mawk -F'^$' 'FNR<NR || _*(ORS=" "$_ RS)' f2.txt f1.txt
123 ABC
456 ABC
789 ABC

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