How to make paste -d second file print down while looping?


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How to make paste -d second file print down while looping?
# 1  
Old 08-29-2019
How to make paste -d second file print down while looping?

]I would like to make the second file label 'b' print down the first file label 'a', like shifting down the file creating new lines I want it to print all the way down until the first line of the second file hit the last line of the first file. Would I have to put this into a file itself or could I do it from the terminal?

What it does now:


paste -d ' ' a b

Code:
1           a 
  2         b
  3         c
  4         d 
  5         e
  6         
  7         
  8        
  9


Desired output:

1 ↓
2 a
3 b
4 c
5 d
6 e
7
8
9



1
2
3 ↓
4 a
5 b
6 c
7 d
8 e
9


Re-looping
1 b
2 c
3 d
4 e
5
6
7 ↓
8 List is about to loop
9 a

Last edited by Corona688; 08-29-2019 at 07:53 PM..
# 2  
Old 08-29-2019
One way:
Code:
#!/bin/bash

# Create temporary file to hold newlines
touch /tmp/$$

# Open both files to read line-by-line
exec 5<file1
exec 6<file2

# Read lines from both until file2 runs out
while read LINE <&6 && read LINE <&5
do
        :
done

# Run loop same number of times as lines left in file1.
while read LINE <&5
do
        cat /tmp/$$ file2 | paste file1 /dev/stdin
        echo >> /tmp/$$
        echo
done

cat /tmp/$$ file2 | paste file1 /dev/stdin


rm -f /tmp/$$ # Delete temporary file
# Close file descriptors
exec 5>&-
exec 6>&-

Re-looping I'm less certain of. Perhaps the files themselves should be generated at need. How large are they in a non-trivial example?
# 3  
Old 08-29-2019
Quote:
Originally Posted by Corona688
One way:
Code:
#!/bin/bash

# Create temporary file to hold newlines
touch /tmp/$$

# Open both files to read line-by-line
exec 5<file1
exec 6<file2

# Read lines from both until file2 runs out
while read LINE <&6 && read LINE <&5
do
        :
done

# Run loop same number of times as lines left in file1.
while read LINE <&5
do
        cat /tmp/$$ file2 | paste file1 /dev/stdin
        echo >> /tmp/$$
        echo
done

cat /tmp/$$ file2 | paste file1 /dev/stdin


rm -f /tmp/$$ # Delete temporary file
# Close file descriptors
exec 5>&-
exec 6>&-

Re-looping I'm less certain of. Perhaps the files themselves should be generated at need. How large are they in a non-trivial example?
Thank you for the reply,

Re-looping mean just looping through the list again, the files are over 20gbs.

and how to take the space out between the files to merge them together?

Last edited by bigvito19; 08-29-2019 at 10:26 PM..
# 4  
Old 08-29-2019
Just following the example posted. Using files a and b to create c loop example:
Code:
awk '
NR==FNR {a[c++]=$0; next;}
{b[++d]=$0;}
END { for (i=0; i<c; i++) {
for (j=0; j<c; j++) print a[j], b[(j-i)<0?(c+j-i):(j-i)];
print _;
}
}' a b > c


Last edited by RavinderSingh13; 02-28-2020 at 01:58 AM..
# 5  
Old 08-29-2019
Quote:
Originally Posted by rdrtx1
Just following the example posted. Using files a and b to create c loop example:
Code:
awk '
NR==FNR {a[c++]=$0; next;}
{b[++d]=$0;}
END { for (i=0; i<c; i++) {
         for (j=0; j<c; j++) print a[j], b[(j-i)<0?(c+j-i):(j-i)];
         print _;
      }
}' a b > c

Thank you for the reply,

I just want the whole second file to print through the first file. I didn't want the line to have breaks. I was just showing an example of a file completely printing through another.

And how to merge the files together instead of having a space between them?

Last edited by bigvito19; 08-29-2019 at 10:57 PM..
# 6  
Old 08-29-2019
try:
Code:
awk '
NR==FNR {a[c++]=$0; next;}
{b[++d]=$0;}
END { for (i=0; i<c; i++) {
for (j=0; j<c; j++) {
v=j-i;
if (v < 0) v=v+c;
print a[j], b[v];
}
print _;
}
}' a b > c

or gawk instead of [ICODE]awk[/ICODE

Last edited by RavinderSingh13; 02-28-2020 at 02:00 AM..
# 7  
Old 08-29-2019
Quote:
Originally Posted by rdrtx1
try:
Code:
awk '
NR==FNR {a[c++]=$0; next;}
{b[++d]=$0;}
END { for (i=0; i<c; i++) {
         for (j=0; j<c; j++) {
            v=j-i;
            if (v < 0) v=v+c;
            print a[j], b[v];
         }
         print _;
      }
}' a b > c

or gawk instead of awk
Thank you for the reply,

I had go it to work, i had to go back and re-do it.

Last edited by bigvito19; 08-30-2019 at 01:39 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to make second file print down the whole first file?

Using Python I have 2 text files (big files over 1gb) that opens side by side on the same line in terminal, but I want the file on the right to print down the other file while the file on the left is stationary or displayed all at once. I want to print text file 2 through all of text file 1. ... (6 Replies)
Discussion started by: bigvito19
6 Replies

2. Shell Programming and Scripting

How to cut a pipe delimited file and paste it with another file to form a comma separated outputfile

Hello ppl I have a requirement to split (cut in unix) a file (A.txt) which is a pipe delimited file into A1.txt and A2.txt Now I have to join (paste in unix) this A2.txt with external file A3.txt to form output file A4.txt which should be CSV (comma separated file) so that third party can... (25 Replies)
Discussion started by: etldev
25 Replies

3. Shell Programming and Scripting

Looping structure to make up for lack of bash GOTO

Hello, I am re-processing some files when a specific condition is met. The condition is read from the filename. Since files may need to be re-processed a number of times before they no longer meet the condition, I need to know when to stop re-processing. I am having trouble visualizing the... (3 Replies)
Discussion started by: LMHmedchem
3 Replies

4. Shell Programming and Scripting

Looping inside directories based on a file which contains file directory list

Hi All, Please help. I have got a file which contains a listing of a file and some directories after it, one by one. I am calling this file xyz.txt here file1 dir1 dir2 dir3 dir4 file2 dir5 dir6 dir7 dir8 file3 dir9 dir10 dir11 dir12 (6 Replies)
Discussion started by: Piyush Jakra
6 Replies

5. Shell Programming and Scripting

FILE_ID extraction from file name and save it in CSV file after looping through each folders

FILE_ID extraction from file name and save it in CSV file after looping through each folders My files are located in UNIX Server, i want to extract file_id and file_name from each file .and save it in a CSV file. How do I do that? I have folders in unix environment, directory structure is... (15 Replies)
Discussion started by: princetd001
15 Replies

6. Shell Programming and Scripting

Make pwd print escape character

I decided I wanted to have the cd command print my full working directory after each cd command, so I put this cw command in .bashrc as a function. cw () { cd "${1}" pwd }While this works I would like pwd to print escapes when a space in a directory name exists. This would... (7 Replies)
Discussion started by: jelloir
7 Replies

7. Shell Programming and Scripting

How to make a long print string to shotcut form in perl?

print "1.readfromfile\n2.add_ex1(4,5)\n3.add_ex2(11,5)\n4.add_ex3(9,3)\n5.add_ex4(91,4)\n"; How to do it in this form: print "1.readfromfile\n 2.add_ex1(4,5)\n 3.add_ex2(11,5)\n 4.add_ex3(9,3)\n 5.add_ex4(91,4)\n"; (3 Replies)
Discussion started by: cola
3 Replies

8. Programming

makeutility: how to get the make-file name inside of the make-file?

How I can get the current make-file name in a make-file So, if I run make with specified file:make -f target.mak is it possible to have the 'target' inside of the that 'target.mak' from the file name? (2 Replies)
Discussion started by: alex_5161
2 Replies

9. Shell Programming and Scripting

Paste content of a file to another file and make it as columned

Pls help me on this. I have to 2 files like shown below: File 1 TAIJM AXPKIM BEMGW File 2 PXMPA JYGE IMJP What i want to do is to paste both file to a new file on thir format: File 3 TAIJM PXMPA AXPKIM JYGE BEMGW IMJP I tried cat and print, but it doesn't work. Cn... (6 Replies)
Discussion started by: kingpeejay
6 Replies

10. UNIX for Dummies Questions & Answers

How to print something in make utility

Hi want to know the syntax of printing something (value or variable) in GNU make utility. I give this in the Makefile: echo "Hi" OR @echo "Hi" But I only get error this when I run make (at the line where I have echo): Makefile:9: *** missing separator. Stop. Whats the problem? How can... (2 Replies)
Discussion started by: radiatejava
2 Replies
Login or Register to Ask a Question