LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 06-21-2011, 02:49 AM   #1
anderssk
LQ Newbie
 
Registered: Jun 2011
Posts: 4

Rep: Reputation: Disabled
Remove many lines based on content of other file


Hi'

I a csv-file (A.csv) with a total of 4.600.000 lines.
Thats to many and only a few is nessesary

I have a txt-file with 150 lines (X.txt) (all lines is dataset from a mainframe and looks like abc.def.123.456

How do I remove lines from A.csv where none of the dataset from x.txt is present?
 
Old 06-21-2011, 03:11 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Code:
grep -vf X.txt A.csv > A_new.csv
 
1 members found this post helpful.
Old 06-21-2011, 11:04 AM   #3
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by anderssk View Post
How do I remove lines from A.csv where none of the dataset from x.txt is present?
What does that mean?
 
Old 06-22-2011, 05:16 AM   #4
anderssk
LQ Newbie
 
Registered: Jun 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
The content is one dataset pr. line
A mainframe dataset is build up by 8 char. a dot and 8 char.
ABCDEFGH.12345678.ABCDEFGH.12345678.ABCDEFGH

Max 44 char.
 
Old 06-22-2011, 05:34 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Just out of curiosity, does the code suggested in post #2 work?
 
Old 06-22-2011, 06:30 AM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by anderssk View Post
The content is one dataset pr. line
A mainframe dataset is build up by 8 char. a dot and 8 char.
ABCDEFGH.12345678.ABCDEFGH.12345678.ABCDEFGH

Max 44 char.
That doesn't help. What exactly is the format and what exactly do you want to do?

The way I understood it from your original post is that you want to remove lines from A.csv if they are not in file x.txt. Is that correct?
 
Old 06-22-2011, 06:33 AM   #7
anderssk
LQ Newbie
 
Registered: Jun 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
Yes - that's correct. I have tried with the grep -vf but the A_new.txt file is emty :-(
 
Old 06-22-2011, 06:36 AM   #8
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
<deleted>

Last edited by MTK358; 06-22-2011 at 06:38 AM. Reason: Wrong solution
 
Old 06-22-2011, 06:41 AM   #9
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Code:
while read line
do
    if grep -F "$line" x.txt > /dev/null
    then
        echo "$line"
    fi
done < <(cat A.csv)
 
Old 06-22-2011, 07:11 AM   #10
anderssk
LQ Newbie
 
Registered: Jun 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
Last line give a syntax error : redirection unexpected
 
Old 06-22-2011, 07:12 AM   #11
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
How about replacing the last line with:

Code:
done < A.csv
?
 
Old 06-22-2011, 07:21 AM   #12
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
Does the script start with #!/bin/sh instead of #!/bin/bash? <() redirection is present in bash only, not in its compatibility mode when it discovers it was started by /bin/sh.

MTK358 is right anyway: you don’t need it.
 
Old 06-22-2011, 12:49 PM   #13
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by anderssk View Post
I a csv-file (A.csv) with a total of 4.600.000 lines. [...]
I have a txt-file with 150 lines (X.txt) (all lines is dataset from a mainframe and looks like abc.def.123.456
It depends on how you interpret the fields in the CSV file, and what "dataset" means. You did not tell us either of those.

Do you wish to keep only the records in A.csv that contain a field with one of the abc.def.123.456 values? In other words, does each line of X.txt describe a field in the A.csv file?
 
Old 06-23-2011, 10:34 AM   #14
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
I think you should stop talking vaguely about the files and what you want to do with them, and post an actual example of the contents, demonstrating exactly what changes you need to make. Also show us the scripting attempts you've made so far.

And if you aren't using standard bash and standard linux (gnu) tools on standard *nix-based files, then give us the necessary background on that as well.

That should save us a lot of ping-pong Q&A and help you get the answers you need.
 
1 members found this post helpful.
  


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
delete a file based on content and date ElectroLinux Linux - Newbie 7 08-19-2009 07:01 AM
Remove lines in a text file based on another text file asiandude Programming 10 01-29-2009 10:59 AM
erase file content (lines...) yesilyurtav Linux - General 2 08-31-2006 06:48 AM
Remove files based on content stefaandk Linux - General 2 08-13-2005 08:03 AM
How to remove lines from a file doza Linux - General 2 04-27-2005 11:59 AM

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

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