Read two text files in bash


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Read two text files in bash
# 1  
Old 10-20-2010
Read two text files in bash

Hi all,

I have two files;

file1.txt contains
Jan
Feb
March

file2.txt contains
01
02
03


How do i read the two files in a bash script so that it echos the following;

Jan 01
Feb 02
Mar 03

Doing so reads only one file;
Code:
while read line; do
 echo "$line"
done < file1.txt

How do I incorporate the second file?

Thanks
# 2  
Old 10-20-2010
Hi.

If that's all the data you have, try using the paste command.

Code:
$ cat file1
Jan
Feb
Mar

$ cat file2
01
02
03

$ paste -d" " file1 file2
Jan 01
Feb 02
Mar 03

Otherwise, something like:
Code:
$ cat Dates.sh
exec 3<file2

while read Month; do
  read -u3 Day
  echo $Month $Day
done < file1

$ ./Dates.sh
Jan 01
Feb 02
Mar 03

This User Gave Thanks to Scott For This Post:
# 3  
Old 10-20-2010
hi scottn,

Thanks for the reply. I'm going to do further process with my files hence the "cat" command does not meet the requirement.
# 4  
Old 10-20-2010
Quote:
Originally Posted by Muhammad Rahiz
hi scottn,

Thanks for the reply. I'm going to do further process with my files hence the "cat" command does not meet the requirement.
I used the cat command to show the files, not to process them. The "work" is done by the paste command, or by the while-loop.
# 5  
Old 10-20-2010
hi again scottn,

i didn't realise your post contains two parts - i overlooked the second part.

And yes, that is what I want - thanks!

I did more search and found this works as well;

Code:
paste -d'\n' file1.txt file2.txt | while read f1 && read f2; do
  echo "$f1 $f2"
done

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Read .xlsx and text files in a loop using openpyxl

I have a list of .xlsx files (names 1.xlsx, 2.xlsx etc) in a directory, on which I need to insert data from its corresponding text file (named 1.txt, 2.txt etc) on the second worksheet named 'Filtered' and save it. The code I am trying is #!/usr/bin/python import os from... (8 Replies)
Discussion started by: nans
8 Replies

2. Shell Programming and Scripting

BASH script to read external file to perform text replacements?

Hi all, I have a moderate size (300 lines) BASH Shell script that performs various tasks on different source reports (CSV files). One of the tasks that it performs, is to use SED to replace 'non-conforming' titles with conformant ones. For example "How to format a RAW Report" needs to become... (3 Replies)
Discussion started by: richardsantink
3 Replies

3. Shell Programming and Scripting

Bash script read specific value from files of an entire folder

Hello, I heva a problem creating a script that read specifc value from all the files of an entire folder I have a number of email files into a directory and i need to extrect from each file 2 specific values. After that i have to put them into a new file that looks like that: To: value1 ... (1 Reply)
Discussion started by: ahmenty
1 Replies

4. Programming

Read columns from text files

Dear All, I have basic structure of the C++ code . It suppose to read particular columns from some txt file. The txt file look like following (snippet). I have to ask the details for instance 'id' information for rows satisfying text with red color. The issue is that the txt file has not just the... (2 Replies)
Discussion started by: emily
2 Replies

5. Shell Programming and Scripting

Read text between regexps and write into files based on a field in the text

Hi, I have a huge file that has data something like shown below: huge_file.txt start regexp Name=Name1 Title=Analyst Address=Address1 Department=Finance end regexp some text some text start regexp Name=Name2 Title=Controller Address=Address2 Department=Finance end regexp (7 Replies)
Discussion started by: r3d3
7 Replies

6. Shell Programming and Scripting

Read n lines from a text files getting n from within the text file

I dont even have a sample script cause I dont know where to start from. My data lookes like this > sat#16 #data: 15 site:UNZA baseline: 205.9151 0.008 -165.2465 35.8109 40.6685 21.9148 121.1446 26.4629 -18.4976 33.8722 0.017 -165.2243 48.2201 40.6908 ... (8 Replies)
Discussion started by: malandisa
8 Replies

7. Shell Programming and Scripting

How can I read variable text files through cat command?

Hi. I'm just wondering how can I read variable text files through cat command. I made a shell script to count frequency of words and array them with variable conditions. It's only working for one file that I wrote in script now. But I want to make it working for every text file when I execute... (2 Replies)
Discussion started by: rlaxodus
2 Replies

8. Shell Programming and Scripting

how to read two text files in the same shell script

Folks i have written two scripts that do reading of csv files , i am currently fetching the all part of the line in to variables and braking the line into several variables. I am doing the same thing in an another shell script, i want to integrate both the scripts in to a single one where i can... (2 Replies)
Discussion started by: venu
2 Replies

9. Shell Programming and Scripting

Bash Script Help...search then read from file: change text and pipe back...

Hello, I am trying to make a bash script that can pull data from a file and then change one part of said data. I want to search by username and pull the full line. That way there is a way to replace just one part of that line then return it back to the file. My Data is stored like: ... (1 Reply)
Discussion started by: serverfull
1 Replies

10. Shell Programming and Scripting

Why are my text files sometimes empty when I make a read to them?

My implementation: I have a script Caller.sh that runs in the background that continuously calls the following scripts: createtext.sh createtext2.sh createtext3.sh Each of these scripts does the following (but with different text file names): #! /bin/bash ... (1 Reply)
Discussion started by: etranman1
1 Replies
Login or Register to Ask a Question