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 |
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 :) |
Code:
while read line |
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 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 |
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 = |
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.
|
No hurry. You may attend it when possible.
|
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 |
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. |
${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` Takes the first line of list1.txt |
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 |
Quote:
Code:
1 5 Code:
6 |
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. |
$ cat a.txt
1 2 3 4 $ cat b.txt 5 6 7 8 Code:
#!/bin/bash 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. |
Quote:
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 |
All times are GMT -5. The time now is 10:13 AM. |