LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Script: splitting lines in multiple files and joining them (https://www.linuxquestions.org/questions/programming-9/script-splitting-lines-in-multiple-files-and-joining-them-430116/)

timmay9162 03-30-2006 03:34 PM

Script: splitting lines in multiple files and joining them
 
I need to split up lines in multiple files and join them on the screen.

Say I have 2 files
The first file is like this:
Mary Wednesday
Joe Monday
Sally Saturday

The second file is this:
Sally 999.999.9990
Mary 999.999.9998
Joe 999.999.9993

I am a c programmer so this is something i am not use to.

Any help would be greatly appreciated.

Thanks
Tim

Seniltai 03-30-2006 03:47 PM

In this case it would be best to use fscanf I think. Make a struct which contains the name, and number and day, and scan the file line by line. Then make that an static array, or a dynamic array with malloc and realloc, whichever you like, and then, for every line that you scan, see if the name is already in one of the array items, if so, add the data, if not, add it to a new position in the array. This way you can load the first file first, and then the second file to add the rest of the data.

Hope this helps :)

timmay9162 03-30-2006 04:14 PM

Code:

while read line
do
set $line
echo $1
done < file

Ok that is what i have so far but it spits out a whole bunchog garbage. When i tell it to echo the line it does that fine but then when i try to set the line and use $1 to grap the first part of line say bob it isnt working. Why? from what i have found it shoudl work fine.

/bin/bash 03-31-2006 04:53 AM

How do you want the output to come out?
Mary Wednesday Mary 999.999.9998
Joe Monday Joe 999.999.9993
Sally Saturday Sally 999.999.9990

or maybe:

Mary Wednesday 999.999.9998
Joe Monday 999.999.9993
Sally Saturday 999.999.9990

or maybe:

Mary 999.999.9998 Wednesday
Joe 999.999.9993 Monday
Sally 999.999.9990 Saturday



$ cat list1.txt
Mary Wednesday
Joe Monday
Sally Saturday

$ cat list2.txt
Sally 999.999.9990
Mary 999.999.9998
Joe 999.999.9993

Code:

#!/bin/bash
while read line;do
  line2=`grep "${line/ */}" list2.txt`
  #Output example #1
  echo "Example 1 = $line $line2"
  #Output example #2
  echo "Example 2 = $line ${line2/* /}"
  #Output example #3
  echo "Example 3 = $line2 ${line/* /}"
done <list1.txt

Output:
Example 1 = Mary Wednesday Mary 999.999.9998
Example 2 = Mary Wednesday 999.999.9998
Example 3 = Mary 999.999.9998 Wednesday
Example 1 = Joe Monday Joe 999.999.9993
Example 2 = Joe Monday 999.999.9993
Example 3 = Joe 999.999.9993 Monday
Example 1 = Sally Saturday Sally 999.999.9990
Example 2 = Sally Saturday 999.999.9990
Example 3 = Sally 999.999.9990 Saturday

Gins 04-01-2006 04:41 AM

I tried this program. It didn't work the way ' /bin/bash ' showed here.

-----------------------------
[nissanka@c83-250-107-194 ~]$ cat lixt1.txt

Mary Wednesday

Joe Monday

Sally Saturday

[nissanka@c83-250-107-194 ~]$
------------------------------



[nissanka@c83-250-107-194 ~]$ cat lixt2.txt

Sally 999.999.9990

Mary 999.999.9998

Joe 999.999.9993

[nissanka@c83-250-107-194 ~]$
--------------------------------------------


As you see, I made 2 files namely 'lixt1.txt' and 'lixt2.txt'

Afterwards, I made the following file. I named it 'try1'.

----------------------------------

[nissanka@c83-250-107-194 ~]$ cat try1

#!/bin/bash

while read line;

do

line2=`grep "${line/ */}" lixt2.txt`
#Output example #1

echo "Example 1 = $line $line2"

#Output example #2

echo "Example 2 = $line ${line2/* /}"

#Output example #3

echo "Example 3 = $line2 ${line/* /}"

done <lixt1.txt

[nissanka@c83-250-107-194 ~]$

--------------------------------------






The following is a part of the output. It is differen from what /bin/bash has mentioned. What is the problem?




[nissanka@c83-250-107-194 ~]$ ./try1
Example 1 =

Sally 999.999.9990

Mary 999.999.9998

Joe 999.999.9993
Example 2 = 999.999.9993
Example 3 =

Sally 999.999.9990

Mary 999.999.9998

Joe 999.999.9993
Example 1 =

Sally 999.999.9990

Mary 999.999.9998

Joe 999.999.9993
Example 2 = 999.999.9993
Example 3 =


Sally 999.999.9990

Mary 999.999.9998

Joe 999.999.9993
Example 1 =


Sally 999.999.9990

Mary 999.999.9998

Joe 999.999.9993
Example 2 = 999.999.9993
Example 3 =


Sally 999.999.9990

Mary 999.999.9998

Joe 999.999.9993
Example 1 =


Sally 999.999.9990

Mary 999.999.9998

Joe 999.999.9993
Example 2 = 999.999.9993
Example 3 =

Sally 999.999.9990

Mary 999.999.9998

Joe 999.999.9993
Example 1 = Mary Wednesday Mary 999.999.9998
Example 2 = Mary Wednesday 999.999.9998
Example 3 = Mary 999.999.9998 Wednesday
Example 1 =


Sally 999.999.9990

Mary 999.999.9998

Joe 999.999.9993
Example 2 = 999.999.9993
Example 3 =

Sally 999.999.9990

Mary 999.999.9998

Joe 999.999.9993
Example 1 = Joe Monday Joe 999.999.9993
Example 2 = Joe Monday 999.999.9993
Example 3 = Joe 999.999.9993 Monday
Example 1 =

/bin/bash 04-01-2006 05:37 AM

I wrote the script using cygwin so that may be the problem. I will check it when I get home to see if it works in normal bash shell.

Gins 04-01-2006 06:26 AM

No hurry. You may attend it when possible.

/bin/bash 04-01-2006 05:35 PM

Probably you have some extra lines in the 2 files list1.txt and list2.txt. When you do cat list1.txt and list2.txt it should look like this:
$ cat list?.txt
Mary Wednesday
Joe Monday
Sally Saturday
Sally 999.999.9990
Mary 999.999.9998
Joe 999.999.9993

Gins 04-02-2006 11:13 AM

Now it is fine. Please read the following. I have one more question on the program.

---------------------------------------------------------------------------


[nissanka@c83-250-107-194 ~]$ ./try1
Example 1 = Mary Wednesday Mary 999.999.9998
Example 2 = Mary Wednesday 999.999.9998
Example 3 = Mary 999.999.9998 Wednesday
Example 1 = Joe Monday Joe 999.999.9993
Example 2 = Joe Monday 999.999.9993
Example 3 = Joe 999.999.9993 Monday
Example 1 = Sally Saturday Sally 999.999.9990
Example 2 = Sally Saturday 999.999.9990
Example 3 = Sally 999.999.9990 Saturday
Example 1 = Sally 999.999.9990
Mary 999.999.9998
Joe 999.999.9993
Example 2 = 999.999.9993
Example 3 = Sally 999.999.9990
Mary 999.999.9998
Joe 999.999.9993
Example 1 = Sally 999.999.9990
Mary 999.999.9998
Joe 999.999.9993
Example 2 = 999.999.9993
Example 3 = Sally 999.999.9990
Mary 999.999.9998
Joe 999.999.9993
Example 1 = Sally 999.999.9990
Mary 999.999.9998
Joe 999.999.9993
Example 2 = 999.999.9993
Example 3 = Sally 999.999.9990
Mary 999.999.9998
Joe 999.999.9993
Example 1 = Sally 999.999.9990
Mary 999.999.9998
Joe 999.999.9993
Example 2 = 999.999.9993
Example 3 = Sally 999.999.9990
Mary 999.999.9998
Joe 999.999.9993
Example 1 = Sally 999.999.9990
Mary 999.999.9998
-----------------------------------------


[nissanka@c83-250-107-194 ~]$ cat try1


#!/bin/bash

while read line;

do

line2=`grep "${line/ */}" lixt2.txt`
#Output example #1

echo "Example 1 = $line $line2"

#Output example #2

echo "Example 2 = $line ${line2/* /}"

#Output example #3

echo "Example 3 = $line2 ${line/* /}"

done <lixt1.txt
-----------------------------------------------------------------------------


line2='grep "${line/ */}" lixt2.txt [ What is this line is doing?]

Especially {line/ */} is not clear to me.

muha 04-03-2006 03:58 AM

${BASH/bash/bar}
Replaces occurance of bash with bar in the variable $BASH. (source: rute)

${test/ */}
Only outputs the word before the first space. Replaces 'space anything' with 'nothing'

To see it working on your file try this:
Code:

$ test=`head -1 list1.txt`
$ echo $test
Mary Wednesday
$ echo ${test/ */}
Mary
$ echo ${test/Mary/Joey}
Joey Wednesday
$ echo ${test/W*/Joey}
Mary Joey

head -1 list1.txt
Takes the first line of list1.txt

vikrambhimbar 04-03-2006 05:26 AM

Re:script
 
ok I will tell you exactly what I want..

See I have a file a.txt and it's contains are
1
2
3
4
in row,, and in same way suppose I have file b.txt
with contains
5
6
7
8...ok. Now I want to add first no from file "a" with first no of file "b" and store it in some file "c"..like this for all no's from both files

muha 04-03-2006 08:18 AM

Quote:

Originally Posted by vikrambhimbar
Now I want to add first no from file "a" with first no of file "b" and store it in some file "c"..like this for all no's from both files

So you want?
Code:

1 5
2 6
3 7
4 8

ahh, maybe you want to add?
Code:

6
8
10
12

Let us know!

Gins 04-03-2006 03:45 PM

Muha thanks for taking time to reply.


${BASH/bash/bar}

$ --> This sigh stands for variables.


${BASH/bash}

What does the above mean?

I don't know very much about those things. Please tell me more when possible.

/bin/bash 04-03-2006 05:09 PM

$ cat a.txt
1
2
3
4
$ cat b.txt
5
6
7
8
Code:

#!/bin/bash
XX=( $(<a.txt) )
YY=( $(<b.txt) )
for i in `seq 1 ${#XX[*]}`;do
  ZZ=$(( i - 1 ))
  expr ${XX[$ZZ]} + ${YY[$ZZ]} >> c.txt
done

$ cat c.txt
6
8
10
12

If the files a.txt and b.txt do not have the same amount of elements then you'll get an expr error.

muha 04-04-2006 08:43 AM

Quote:

Originally Posted by Gins
${BASH/bash/bar}
What does the above mean?

${variable} is a normal way of calling a variable
Now we can also search and replace some things inside ${ } so in general it means:
${variable/searchterm/replacement}
For more on this try rute: http://rute.2038bug.com/index.html.gz

@vikrambhimbar & /bin/bash:
this is my one-liner attempt to calculate lines of two files OR next one displays the two files in two columns:
Code:

$ j=1;for i in `cat a.txt`;do echo $((i+`sed -n $j'p' b.txt`));j=$((j+1));done
6
8
10
12
$ j=1;for i in `cat a.txt`;do echo -e "$i\t`sed -n $j'p' b.txt`";j=$((j+1));done
1      5
2      6
3      7
4      8



All times are GMT -5. The time now is 10:13 AM.