Results 1 to 5 of 5
Hi all,
I'm still fairly new to scripting and programming and i'm having trouble using a loop for this solution. What I need to to do is store individual lines ...
- 04-13-2008 #1Just Joined!
- Join Date
- Oct 2006
- Posts
- 24
storing individual lines from file in loop to use for editing in another file
Hi all,
I'm still fairly new to scripting and programming and i'm having trouble using a loop for this solution. What I need to to do is store individual lines from a file in seperate variables so I can use those to filter out matching lines in a completely different file,and removing them permanently from the second file:
Example:
File 1 contains:
John
Sean
Mike
Craig
Jenny
File 2 contains:
John
Sean
Mike
Craig
Jenny
Alex
Ray
Ryan
Christine
-- So I would need to remove John, Sean, Mike, Craig, Jenny from the second file but I need to use information from the first file to do that. Which i would need to construct a loop to store those individual lines in the first file in its own variable.
-- I still can't figure out how to create a loop to get those individual lines from the first file and store them in variables for later use so I can apply grep, sed, awk to remove thier names in the second file
- Any help, suggestions, or directions to specific tutorials for this problem would be great, thanks.
- 04-13-2008 #2Linux User
- Join Date
- Aug 2006
- Posts
- 458
see the link in my sig. Learning bash. And no, i think you should not be new to scripting, since you joined in 2006. you should know a bit or two about scripting by now.
- 04-13-2008 #3Linux Enthusiast
- Join Date
- Apr 2004
- Location
- UK
- Posts
- 658
from the grep man page:Code:chris@angua:~/dev/scratch$ cat > file1.txt John Sean Mike Craig Jenny chris@angua:~/dev/scratch$ cat > file2.txt John Sean Mike Craig Jenny Alex Ray Ryan Christine chris@angua:~/dev/scratch$ grep -f file1.txt -v file2.txt Alex Ray Ryan Christine chris@angua:~/dev/scratch$
Of course, a bash script would be more flexible if your task was fundamentally more complex than your example. In that case I'd direct you to the useless use of cat awards, particularly the section on dangerous backticks, for examples of good and bad ways to do this kind of thing.-f FILE, --file=FILE
Obtain patterns from FILE, one per line. The empty file con‐
tains zero patterns, and therefore matches nothing.
-v, --invert-match
Invert the sense of matching, to select non-matching lines.
Let us know how you get on,
Chris...To be good, you must first be bad. "Newbie" is a rank, not a slight.
- 04-13-2008 #4Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
Simplest solution to this specific problem, although not teaching you scripting techniques, is to use the "comm" command: this allows you to compare 2 files and distinguish lines that are unique to the first and second files and lines that are common to both. "man comm" for details. The art of scripting is knowing your commands!
- 04-15-2008 #5Just Joined!
- Join Date
- Oct 2006
- Posts
- 24
Thanks for that, exactly what I was looking for, worked like a charm and I didn't have to implement anymore code after that.


Reply With Quote