LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-10-2012, 08:42 AM   #1
rperezalejo
LQ Newbie
 
Registered: Nov 2011
Posts: 4

Rep: Reputation: Disabled
bash matching two files lines by lines


hello people, i have two files,

one with
a,b,c
d,e,f

the other
1
2

I need to do get in one file something like
a,1
b,1
c,1
d,2
e,2
f,2

i did a nested for loop but it doesn't work, and i have no idea of who to solve these problem. I tried to read by a line number but no succeeded.

thank you very much.....

hugs
 
Old 01-10-2012, 08:56 AM   #2
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,943
Blog Entries: 5

Rep: Reputation: 753Reputation: 753Reputation: 753Reputation: 753Reputation: 753Reputation: 753Reputation: 753
Assuming both files have the same number of lines you could use sdiff to do side by side comparision then massage the output to get what you want:

Code:
sdiff file1 file2 |awk '{print $1","$3}
'
 
Old 01-10-2012, 10:39 AM   #3
rperezalejo
LQ Newbie
 
Registered: Nov 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
the problem with that is that the rows in the fist file are of different length, so the $3 in not in all the row the same column.
I did something like these, but is wrong because it reads all the letters for each number, and what i want is for each row, read the other row.

Code:
cat $temp_num | while read num; do
	cat $temp_letters | while read lett; do
		IFS=,\ 
		array=( $lett )
		echo ${#array[@]}
		for ((i=0; i<${#array[@]}; i++))
		do
			echo "( '${array[i]}', '$num')," >> $query 
		done
	done	
done
hugs

Last edited by rperezalejo; 01-10-2012 at 10:43 AM.
 
Old 01-10-2012, 02:05 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,386

Rep: Reputation: 808Reputation: 808Reputation: 808Reputation: 808Reputation: 808Reputation: 808Reputation: 808
awk:
Code:
awk -F, -vnum_file="$temp_num" '{getline num < num_file;
  for (i=1; i <= NF; i++) printf("%s,%d\n", $i, num) }' "$temp_letters"
just bash:
Code:
# open files for reading
exec 3< "$temp_num" 4< "$temp_letters"

while read -u3 num && IFS=, read -u4 -a letters ; do
    printf "%s,$num\n" "${letters[@]}"
done

# close files
exec 3<&- 4<&-
 
Old 01-10-2012, 03:28 PM   #5
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,943
Blog Entries: 5

Rep: Reputation: 753Reputation: 753Reputation: 753Reputation: 753Reputation: 753Reputation: 753Reputation: 753
Quote:
Originally Posted by rperezalejo View Post
the problem with that is that the rows in the fist file are of different length, so the $3 in not in all the row the same column.
What I wrote works for the data you provided initially. Note that the $3 is NOT the position in either file but rather the position in sdiff output.

sdiff will output data like:

file1line1 | file1line1
file1line2 | file2line2

In the above file1line# = $1, the pipe sign (|) = $2 and file2line# = $3.

So for your data: a,b,c and d,e,f are $1 and 1 and 2 are $3 for their respective lines.

Therefore what I suggested would work IF the data was formatted as you originally indicated (i.e. comma delimited with no spaces). If you are saying you have variable length lines with embedded whitespace in file1 or file2 then you're correct it wouldn't work.

Last edited by MensaWater; 01-10-2012 at 03:30 PM.
 
Old 01-11-2012, 01:46 PM   #6
rperezalejo
LQ Newbie
 
Registered: Nov 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
Yes my friend, with the example i wrote it works, but i forgot to put the lines with different length, sorry about that, i found the solution with your help and with the other partner who wrote.

Code:
exec 3< "$temp_pkg" 4< "$temp_class"
while read -u3 pkg && IFS=, read -u4 -a class ; do
	for ((i=0; i<${#class[@]}; i++))
	do 
		temp=${class[i]#\ }
		echo "('$temp', '$pkg')," >> $query 
	done	
done
exec 3<&- 4<&-
more or less the same, i used both the "for" and "printf" solutions for educational reasons, some time printf is complex to understand.

thank you very much
 
1 members found this post helpful.
Old 01-12-2012, 06:42 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
That's a neat way to use file descriptors. Thanks for posting it!

By the way, bash has the parameter substitution pattern "${!array[@]}", which outputs a list of all existing indexes for the array. You can use it instead of the c-style loop.

Code:
for i in "${!class[@]}"; do

Last edited by David the H.; 01-12-2012 at 06:45 AM. Reason: added tip
 
  


Reply

Tags
bash, bash scripting, read


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash - match lines from two files LocoMojo Programming 8 01-25-2011 04:45 AM
[SOLVED] list 10 lines by 10 lines in bash sqn Programming 3 03-30-2010 04:04 AM
bash- how to compare only certain lines of text files daberkow Linux - Newbie 2 06-01-2009 04:48 PM
Sed command to print matching lines and 2 lines above.. DX398 Programming 12 10-01-2008 08:25 AM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM


All times are GMT -5. The time now is 04:28 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration