LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
LinkBack Search this Thread
Old 05-31-2006, 02:14 AM   #1
trey85stang
Senior Member
 
Registered: Sep 2003
Posts: 1,068

Rep: Reputation: 41
Bash Scripting help, altering data in text file.


Latly Ive been coming across the need to use data from one text file to update a second text file. I've been doing all this manually and I know there is an easier way to do it. So I thought I would ask and see if anyone might want to help me with some scripting ideas to make my life a little easier.

First of all I only know a small handful of scripting techniques... and not nearly enough to acomplish what I need to do. However here is a quick example of what I need to script.

I have two files that are similar...

File A has line entries that look like so (it can have between 200-20,000 lines)...

Code:
#file /directory/app/data1/sub/file.name
#file /directory/app/data1/sub/file.name17
#file /directory/app/data1/sub/file.name23
File B has entries like so

Code:
INSTANCE file /directory/app/data1/sub/file.name createdate
INSTANCE file /directory/app/data1/sub/file.name16 createdate
INSTANCE file /directory/app/data1/sub/file.name23 createdate
Now, what I need to do is if the dirctory/file listed in File A, matches the directory/file listed in File B... I need the corrosponding line in File B to be deleted.

Is there anyway to automate this? I can easily use sed and remove the "#file" from the entries in File A if that would make it easier? I know there is a way to do this but I am no good with scripting so I do not know where to begin to get started testing a script to do this?

Any help would be appreciated.

thanks,
Trey

Last edited by trey85stang; 05-31-2006 at 02:15 AM.
 
Old 05-31-2006, 03:30 AM   #2
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 31
The following should do the trick unless the file names in your first file contain whitespace or colons:
Code:
#! /bin/bash

FILEA="first filename"
FILEB="second filename"

for i in $( sed -e "s/^#file \([^ ]*\)/\1/" -e "s/\./\\\./g" "$FILEA" )
do
	sed -ine "\:^INSTANCE file $i :d" "$FILEB"
done
BTW, you mentioned that you did that manually until now. You really did, examining files with up to 20000 lines?

Last edited by spirit receiver; 05-31-2006 at 03:36 AM.
 
Old 05-31-2006, 04:15 AM   #3
trey85stang
Senior Member
 
Registered: Sep 2003
Posts: 1,068

Original Poster
Rep: Reputation: 41
Quote:
Originally Posted by spirit receiver
The following should do the trick unless the file names in your first file contain whitespace or colons:
Code:
#! /bin/bash

FILEA="first filename"
FILEB="second filename"

for i in $( sed -e "s/^#file \([^ ]*\)/\1/" -e "s/\./\\\./g" "$FILEA" )
do
	sed -ine "\:^INSTANCE file $i :d" "$FILEB"
done
BTW, you mentioned that you did that manually until now. You really did, examining files with up to 20000 lines?
It is very rare that I have to do it... I had been putting everything into a spread sheet and comparing them. This pops up ever 9 months or so that I have to make these type of changes, but yes I examined them manually.

thanks for the script I will play around with it and see if I can get this to work! BTW, do you have know of any tutorials that deal with awk and sed to create these types of scripts on my own? I understand what awk and sed do.. but I have never been able to grasp the "s/^#file \([^ ]*\)/\1/" -e "s/\./\\\./g" parts of both
 
Old 05-31-2006, 04:31 AM   #4
exman
LQ Newbie
 
Registered: May 2006
Location: Germany, BS
Distribution: Debian Kernel 2.6.15, Kubuntu Dapper
Posts: 24

Rep: Reputation: 15
Hi,
a good introduction to sed:
google for sed tutorial and take the 3rd link
Would give you the link but i got too few posts
exman
 
Old 05-31-2006, 05:18 AM   #5
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 31
As for regular expressions, there are good tutorials for Perl, like this one for example. These are not exactly the regular expressions used in the shell (although grep can be told to use Perl regular expressions), but once you got accustomed to these, the differences can easily be figured out from the man pages.
 
  


Reply


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Scripting Empty file john8675309 Linux - Software 2 08-08-2007 04:05 PM
Bash scripting to check text in a website carlp Programming 2 09-20-2005 11:14 AM
Reading text from a file using shell scripting. mrobertson Programming 11 06-29-2005 12:12 PM
Bash Scripting - Waiting for a certain amount of data welby Programming 12 05-12-2004 12:40 PM
bash scripting read from file cadj Programming 2 02-29-2004 10:42 PM


All times are GMT -5. The time now is 03:16 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
Open Source Consulting | Domain Registration