LinuxQuestions.org
Help answer threads with 0 replies.
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
 
Search this Thread
Old 01-19-2014, 03:38 PM   #1
MrTwister
LQ Newbie
 
Registered: Jan 2014
Posts: 2

Rep: Reputation: Disabled
Deleting lines from a file in a bash script


Hello,

I'm writing a bash script where I need to set some variables. The variables will be set if a line exists in a specific file.

I'm planning on creating one file with a list of lines, e.g.:

devicedump.txt
word1
word2
word3
word4

And I'll have a second file which may read:

word1
word4

I want to create a new file by removing the matching lines between the two original files, so just leaving me with:

output.txt
word2
word3

...And then use this specific file to set my variables, i.e. set a variable for any words that exist in this file.

What's the best way for me to do this? I was playing around with sed, but not sure how to work it to best fit.

Thanks in advance!
 
Old 01-19-2014, 04:18 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,758

Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
Maybe something as simple as:

Code:
cat devicedump.txt | grep -vFf pattern_file.txt > output.txt
<edit>Sorry, forgot the second part of your question, maybe just copy the output file to a temporary file and append or prepend 'something=<line>' or '<line>=something' to create variable assignments, then just source it in your script to set the variables?</edit>

Last edited by kbp; 01-19-2014 at 04:22 PM.
 
1 members found this post helpful.
Old 01-19-2014, 08:20 PM   #3
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 523

Rep: Reputation: 58
we would be happy to help you provided that you Show us something you tried to play with to achieve this..

well for a hint:

Code:
while read line
do
something
....
..
done < input_file
 
Old 01-27-2014, 07:30 AM   #4
MrTwister
LQ Newbie
 
Registered: Jan 2014
Posts: 2

Original Poster
Rep: Reputation: Disabled
I actually just set the variables and unset if they existed elsewhere - same end result. Probably inefficient code but it seems to work!

also not with the code now so can't post it I'm afraid.

thanks for the replies though!!
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash script:: How to make columns from lines inf file turdus Programming 7 11-29-2011 06:19 PM
Basic bash script question re: file size or # of lines in a file the_fornicator Programming 6 09-03-2009 09:41 AM
Need a bash shell script which will delete lines from file scjohnie Linux - Newbie 1 09-13-2008 08:51 PM
bash script to remove the blank lines in the file naveensankineni Programming 7 03-25-2008 08:34 PM
Can't get lines of a file with a Bash script.. barisdemiray Programming 2 08-11-2004 12:42 PM


All times are GMT -5. The time now is 01:29 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 Google+: linuxquestions
Open Source Consulting | Domain Registration