LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 03-30-2006, 04:34 PM   #1
timmay9162
LQ Newbie
 
Registered: Feb 2006
Posts: 20

Rep: Reputation: 0
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
 
Old 03-30-2006, 04:47 PM   #2
Seniltai
LQ Newbie
 
Registered: Nov 2005
Location: Friesland, The Netherlands
Distribution: Fedora Core 5 Test 2 / FreeBSD 6 / Windows XP SP2
Posts: 17

Rep: Reputation: 0
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
 
Old 03-30-2006, 05:14 PM   #3
timmay9162
LQ Newbie
 
Registered: Feb 2006
Posts: 20

Original Poster
Rep: Reputation: 0
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.
 
Old 03-31-2006, 05:53 AM   #4
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 46
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
 
Old 04-01-2006, 05:41 AM   #5
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,638

Rep: Reputation: 46
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 =
 
Old 04-01-2006, 06:37 AM   #6
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 46
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.
 
Old 04-01-2006, 07:26 AM   #7
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,638

Rep: Reputation: 46
No hurry. You may attend it when possible.
 
Old 04-01-2006, 06:35 PM   #8
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 46
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
 
Old 04-02-2006, 12:13 PM   #9
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,638

Rep: Reputation: 46
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.
 
Old 04-03-2006, 04:58 AM   #10
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 37
${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
 
Old 04-03-2006, 06:26 AM   #11
vikrambhimbar
Member
 
Registered: Aug 2005
Location: India
Posts: 33

Rep: Reputation: 15
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
 
Old 04-03-2006, 09:18 AM   #12
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 37
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!
 
Old 04-03-2006, 04:45 PM   #13
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,638

Rep: Reputation: 46
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.
 
Old 04-03-2006, 06:09 PM   #14
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 46
$ 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.

Last edited by /bin/bash; 04-03-2006 at 06:11 PM.
 
Old 04-04-2006, 09:43 AM   #15
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 37
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
 
  


Reply

Tags
commands


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
Help with Bash Script - Rename Multiple Files embsupafly Programming 16 04-02-2010 04:50 AM
Splitting Text lines? 0aniel Programming 9 11-30-2005 04:08 AM
run script on multiple files statmobile Programming 6 07-17-2004 12:35 AM
Need help with shell script - renaming multiple files NiallC Linux - Newbie 25 07-04-2004 11:45 AM
Joining multiple lines and summing fields elconde Programming 1 02-13-2004 11:42 PM


All times are GMT -5. The time now is 01:22 AM.

Main Menu
Advertisement
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