Reputation: 141
I have 2 files which look like this
file1.txt
GYFUFGYO1 KMP-app [email protected] CODE_SMELL
GYFUFGYO2 KMP-app [email protected] CODE_SMELL
GYFUFGYG3 AFP-Login [email protected] BUG
GYFUFGYG4 AFP-Login [email protected] BUG
GYFUFGYO5 KMP-app [email protected] CODE_SMELL
GYFUFGYO6 KMP-app [email protected] 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 [email protected] CODE_SMELL MAC 135 2022-09-02-09:35
GYFUFGYO2 KMP-app [email protected] CODE_SMELL MAC 135 2022-09-02-09:35
GYFUFGYG3 AFP-Login [email protected] BUG MAC 135 2022-09-02-09:35
GYFUFGYG4 AFP-Login [email protected] BUG MAC 135 2022-09-02-09:35
GYFUFGYO5 KMP-app [email protected] CODE_SMELL MAC 135 2022-09-02-09:35
GYFUFGYO6 KMP-app [email protected] 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 [email protected] CODE_SMELL GYFUFGYO2 KMP-app [email protected] CODE_SMELL GYFUFGYG3 AFP-Login [email protected] BUG GYFUFGYG4 AFP-Login [email protected] BUG GYFUFGYO5 KMP-app [email protected] CODE_SMELL GYFUFGYO6 KMP-app [email protected] CODE_SMELL
BAU 133 2022-09-02-09:35
Can someone help me to figure out this? Thanks in advance!
Upvotes: 2
Views: 166
Reputation: 2815
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
Upvotes: 0
Reputation: 203219
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.
Upvotes: 2
Reputation: 36370
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)
Upvotes: 2
Reputation: 22012
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.Upvotes: 4
Reputation: 189327
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.)
Upvotes: 1