LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 02-07-2003, 04:51 AM   #1
yawgmoth81
LQ Newbie
 
Registered: Feb 2003
Location: Apeldoorn, Netherlands
Distribution: Mandrake 8.0/IBM AIX
Posts: 6

Rep: Reputation: 0
Shell Scripting problems (newbie)


I want to compare the first row of two files, take out the differences and merge the tow files together...

This is what I have:

cut -f1 $1 > temp1
cut -f1 $2 > temp2
diff -D ver temp1 temp2 > verschil.txt

this is the output file:

51185682482
51143082482
51185243482
52285682482
51235682482
#ifndef ver <- needs to be deleted
51185684562 <- needs to be deleted
#endif /* ver */ <- needs to be deleted
51185121482
#ifdef ver <- needs to be deleted
52286121482 <- needs to be deleted
#endif /* ver */ <- needs to be deleted

Is there a way in shell scripting to delete the lines "#ifndef ver" and "#endif /* ver */" and the number in between?

If so, what can I do?

TIA

Last edited by yawgmoth81; 02-07-2003 at 04:52 AM.
 
Old 02-07-2003, 07:14 AM   #2
vladkrack
Member
 
Registered: Oct 2002
Location: Curitiba - Brazil
Distribution: Conectiva
Posts: 334

Rep: Reputation: 30
Yes, there are several ways of doing that. A simpler solution is to use grep -v , like:

# grep -v "#ifdef ver" temp1


Hope that helps ...
 
Old 02-07-2003, 07:19 AM   #3
yawgmoth81
LQ Newbie
 
Registered: Feb 2003
Location: Apeldoorn, Netherlands
Distribution: Mandrake 8.0/IBM AIX
Posts: 6

Original Poster
Rep: Reputation: 0
Could you be more specific?

I need to delete every occurence of these 3 lines in the file...

#ifdef ver
a number
#endif /* ver */

Can this be done?? And if so, how?
 
Old 02-07-2003, 07:27 AM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
i'm lost.. is that the first row or the first column??
 
Old 02-07-2003, 07:32 AM   #5
vladkrack
Member
 
Registered: Oct 2002
Location: Curitiba - Brazil
Distribution: Conectiva
Posts: 334

Rep: Reputation: 30
Grep will not show the lines that has the specified Regular Expression (that is inside "). Your comand will be something like:

# cut -f1 $1 | grep -v "#ifdef ver" | grep -v "52286121482" |grep -v "#endif /* ver */"> temp1
 
Old 02-07-2003, 07:49 AM   #6
yawgmoth81
LQ Newbie
 
Registered: Feb 2003
Location: Apeldoorn, Netherlands
Distribution: Mandrake 8.0/IBM AIX
Posts: 6

Original Poster
Rep: Reputation: 0
But what if the number is different every time...??

As is the case in this file:

#ifndef ver
51185684562
#endif /* ver */
51185121482
#ifdef ver
52286121482
#endif /* ver */

I need to delete the "#ifdef ver", "#ifndef ver", "#endif /* ver */" and the numbers between them... In this case it's 51185684562 and 52286121482
 
Old 02-07-2003, 10:11 AM   #7
vladkrack
Member
 
Registered: Oct 2002
Location: Curitiba - Brazil
Distribution: Conectiva
Posts: 334

Rep: Reputation: 30
Sorry, I didn't see it before. In that case will be easier to use sed:

# cat verschil.txt | tr "\n" § | sed 's/#ifdef ver§[0-9]*[0-9]§#endif \/\* ver \*\/§//g' | tr § "\n" > verschill.txt.new
 
Old 02-07-2003, 10:58 AM   #8
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
This a little simpler, and maybe a bit more reliable:
Code:
mv verschil.txt verschil.bak
sed '/^#ifn\{0,1\}def ver/,/^#endif/d' verschil.bak >verschil.txt

Last edited by Hko; 02-07-2003 at 11:02 AM.
 
Old 02-10-2003, 05:30 AM   #9
yawgmoth81
LQ Newbie
 
Registered: Feb 2003
Location: Apeldoorn, Netherlands
Distribution: Mandrake 8.0/IBM AIX
Posts: 6

Original Poster
Rep: Reputation: 0
Thanks everybody, problem solved!!

HKO's option worked perfectly...

I've tested it on a bigger file and came to the conclusion that there also is an #else /* ver */

So i edited the code a bit and it works like a charm:

Code:
sed '/^#ifn\{0,1\}def ver/,/^#else/d' verschil.bak > verschil.txt
sed '/^#endif/d' verschil.txt > verschil.dat
Now it deletes from if to else and it deletes the single line endif

If someone has a better way of doing this, please tell me...

If not, then I think this topic can be closed, because everything works fine...

Thanks again everyone!

PS: I was trying something but it didn't work so well... Can I delete everything before #if(n)def and everything after #endif /* ver */ leaving everything inside these lines intact? I also need to delete the #ifndef, #else and #endif lines when I do that...

51185683111
51185683112
#ifndef ver
51185683113
#else /* ver */
52285683113
#endif /* ver */
51185683114
51185683115

Last edited by yawgmoth81; 02-10-2003 at 09:13 AM.
 
Old 02-10-2003, 03:23 PM   #10
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally posted by yawgmoth81
[...] Can I delete everything before #if(n)def and everything after #endif /* ver */ leaving everything inside these lines intact? I also need to delete the #ifndef, #else and #endif lines when I do that...
You mean the other way around?
Code:
sed -n '/^#ifn\{0,1\}def ver/,/^#endif/p' -e '/^#else/d' verschil.bak > verschil.txt
 
Old 02-11-2003, 05:56 AM   #11
yawgmoth81
LQ Newbie
 
Registered: Feb 2003
Location: Apeldoorn, Netherlands
Distribution: Mandrake 8.0/IBM AIX
Posts: 6

Original Poster
Rep: Reputation: 0
Hmmm that doesn't work for me...

I'm stuck with this:

51185683111
51185683112
#ifndef ver
51185683113
#else /* ver */
52285683113
#endif /* ver */
51185683114
51185683115
#ifndef ver

I only want to have the numbers between #if and #else and I want to delete the rest, it works almost perfectly, there's only one small thing I can't delete the lines before the first #if.

As a small test I made this so I could see all the steps this script took:

Code:
sed '/^#endif/d' tijdens.txt > tijdens2.txt
sed '/^#else/,/^#ifn\{0,1\}def ver/d' tijdens2.txt > tijdens3.txt
sed '/^#ifn\{0,1\}def ver/d' tijdens3.txt > na5.txt
As you can see it first deletes all occurences of #endif, then it deletes everything between #else and #if, and after that it deletes the remaining #if. (usually the first one in the file)

In the example above my file will look like this:

51185683111
51185683112
51185683113

But I want it to look like this:

51185683113

Can this be done?

Last edited by yawgmoth81; 02-11-2003 at 05:58 AM.
 
Old 02-24-2003, 02:31 AM   #12
yawgmoth81
LQ Newbie
 
Registered: Feb 2003
Location: Apeldoorn, Netherlands
Distribution: Mandrake 8.0/IBM AIX
Posts: 6

Original Poster
Rep: Reputation: 0
For those of you who want to know:

I have a script doing precisely what I want it to do...

I solved my last problem (see post above this one) by inserting a "#begin" line in the beginning of the file that had to be processed.

That way I could even delete the first lines BEFORE the first "#ifn..." line.

I'm now trying to convert this script into AWK, if I have any problems with that I'll be sure to post it here...

Thanks for all the help...
 
  


Reply



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
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
shell interface vs shell scripting? I'm confused jcchenz Linux - Software 1 10-26-2005 03:32 PM
Shell scripting help -- newbie rnj Linux - Newbie 1 09-12-2005 12:08 AM
Shell scripting Problems! herve2001 Linux - General 7 02-26-2005 08:57 PM
Shell Scripting - Need some help The Grepper Programming 1 03-29-2002 04:10 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:20 PM.

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
Open Source Consulting | Domain Registration