Issue
I am quite new to bash and I cannot really figure out this problem. So I have two files which look like:
File 1:
Jack
Peter
John
...
New York
Houston
Boston
Chicago
Los Angeles
San Diego
Dallas
San Jose
Phoenix
...
From the first line of file 2, for every fourth line I need to add the corresponding line in file 1 with a "-" as delimiter. The outcome should look like this:
New York-Jack
Houston
Boston
Chicago
Los Angeles-Peter
San Diego
Dallas
San Jose
Phoenix-John
So far I have confirmed that the number of lines in file 2 is exactly four times that in file 1. How should I get the outcome above? Appreciate any help. Thanks!
Solution
With two GNU sed
:
sed '1~4 R file1' file2 | sed '1~5{ N; s/\n/-/ }'
Output:
New York-Jack Houston Boston Chicago Los Angeles-Peter San Diego Dallas San Jose Phoenix-John
From man sed
:
first~step
: Match every step'th line starting with line first.
R filename
: Append a line read from filename. Each invocation of the command reads a line from the file. This is a GNU extension.
Answered By - Cyrus Answer Checked By - Clifford M. (WPSolving Volunteer)