|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Need shell script to read two file at same time and print output in single file
Example I have two files 1) file1.txt 2) file2.txt File1.txt contains Aaa Bbb Ccc Ddd Eee Fff File2.txt contains Zzz Yyy Xxx www vvv uuu I need output file like output.txt Aaa Zzz ----- Bbb Yyy ---- Ccc Xxx ---- Ddd www please help me any one ![]() |
| Sponsored Links | ||
|
|
#2
|
|||
|
|||
|
There are probably a million better ways to do this... but here goes... Code:
#!/bin/sh
linecount1=$(wc -l file1.txt | cut -d\ -f 1 )
linecount2=$(wc -l file2.txt | cut -d\ -f 1 )
i=1 j=1
while [ $i -le $linecount1 -a $j -le $linecount2 ]; do
sed -n -e "${i}p" file1.txt >> output.txt
sed -n -e "${j}p" file2.txt >> output.txt
echo "-----" >> output.txt
i=$(( i + 1 )); j=$(( j + 1))
doneI had to do the cut stuff when assigning linecount, because my wc -l prints the file name after the number of lines. (Debian) Regards, MD |
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
With (g|n|m)awk Code:
awk 'NR==FNR{_[NR]=$0}NR!=FNR{print _[FNR] "\n" $0 "\n---"}' file1 file2 |
|
#4
|
|||
|
|||
|
ripat, I knew there would be a better way, most likely awk. Here, I try to explain how it works... Code:
awk '
NR==FNR {_[NR]=$0}
# NR is total number of lines read in both files.
# FNR is number of lines read, reset for each file.
# _[NR] is an array named _ with NR as index.
# This loads array with contents of file1,
# and ignores file2.
NR!=FNR {print _[FNR] "\n" $0 "\n---"}
# This part ignores file1.
# For each line in file 2,
# Print the corresponding line from the array,
# using FNR as index.
# then a newline, then the line from file2,
# another newline, then the dashes, then another newline.
' file1 file2Hopefully, this is a correct explanation. I was initially confused by the _[NL] thing, thinking this was some kind of special awk reserved variable or something, but then I realized it's just an array named _ I guess that if file1 were super huge, you could run out of memory because it gets loaded into an array. There's probably a way to do this in perl that can handle huge files without slurping into an array... Regards, MD |
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
That's exactly it.
Quote:
Well, maybe. However, awk never hit the memory barrier on my systems even on large test files (1 Mega lines and more). But you are probably right. You could do that by parallel looping through two file handles in perl. |
| Sponsored Links | |
|
|
#6
|
|||
|
|||
|
Quote:
Code:
awk '{print;getline < "file1";print $0 "\n---"}' file2Assuming the files have the same number of lines. Regards |
| Sponsored Links | |
|
|
#7
|
|||
|
|||
|
thank you all for quick reply
|
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| search file and print results with shell script | dodasajan | Shell Programming and Scripting | 3 | 02-07-2009 09:42 AM |
| how to write a shell script that print the last modified file ? | FunnyWolF | Shell Programming and Scripting | 5 | 08-13-2008 02:25 AM |
| Executing Multiple .SQL Files from Single Shell Script file | anushilrai | Shell Programming and Scripting | 3 | 04-07-2008 10:09 AM |
| search for the contents in many file and print that file using shell script | cdfd123 | Shell Programming and Scripting | 3 | 10-07-2007 10:17 PM |
| why shell scripting takes more time to read a file | brkavi_in | Shell Programming and Scripting | 1 | 06-23-2006 08:20 AM |
|
|