LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   How to remove all objects BEFORE first whitespace using sed or awk? (http://www.linuxquestions.org/questions/programming-9/how-to-remove-all-objects-before-first-whitespace-using-sed-or-awk-4175454344/)

CaptainDerp 03-16-2013 01:13 PM

How to remove all objects BEFORE first whitespace using sed or awk?
 
How to remove all objects BEFORE first white space using sed or awk?

source sample

derf3eef3fd34rf/rf3/4f34f/4/stuffudontwant stuffIwant.here

desired output

stuffiwant.here


I also need a script or one liner that will remove a list of objects from a text file,that is provided from a list in a seperate text file.

danielbmartin 03-16-2013 01:26 PM

Quote:

Originally Posted by CaptainDerp (Post 4912960)
How to remove all objects BEFORE first white space using sed or awk?

source sample

derf3eef3fd34rf/rf3/4f34f/4/stuffudontwant stuffIwant.here

desired output

stuffiwant.here

cut will do this job. This code ...

Code:

echo derf3eef3fd34rf/rf3/4f34f/4/stuffudontwant stuffIwant.here
| cut -d ' ' -f2

... produced this result ...
Code:

stuffIwant.here
Daniel B. Martin

danielbmartin 03-16-2013 01:28 PM

Quote:

Originally Posted by CaptainDerp (Post 4912960)
I also need a script or one liner that will remove a list of objects from a text file,that is provided from a list in a separate text file.

Help us to help you. Provide a sample input file (10-15 lines will do). Construct a sample output file which corresponds to your sample input and post both samples here. With "Before and After" examples we can better understand your needs and also judge if our proposed solution fills those needs.

Daniel B. Martin

CaptainDerp 03-16-2013 03:22 PM

Quote:

Originally Posted by danielbmartin (Post 4912965)
Help us to help you. Provide a sample input file (10-15 lines will do). Construct a sample output file which corresponds to your sample input and post both samples here. With "Before and After" examples we can better understand your needs and also judge if our proposed solution fills those needs.

Daniel B. Martin


Sure, I have a large list1 of line by line items, and I have another list2 of items that are in that large list1, and I needed to remove every line from large list1 contained in list2


Large list 1

site.com
url.com
stuff.com
porno.com
msn.com
rap.com
crap.com
aol.com
morecrap.com
shizzo.com
meatspin.com
blah.com
microsoft.com
eatpussy.com

And list2 contains a list of items to be removed from large list1

site.com
url.com
stuff.com
porno.com
rap.com
crap.com
morecrap.com
shizzo.com
meatspin.com

In this example the desired output should be

aol.com
msn.com
microsoft.com

danielbmartin 03-16-2013 05:29 PM

Quote:

Originally Posted by CaptainDerp (Post 4913004)
Sure, I have a large list1 of line by line items, and I have another list2 of items that are in that large list1, and I needed to remove every line from large list1 contained in list2.

I'm away from home now and have no access to a Linux machine...

I think the comm command will do the job, after sorting both files. Check into it.

Daniel B. Martin

ntubski 03-16-2013 09:20 PM

Quote:

Originally Posted by CaptainDerp (Post 4913004)
Sure, I have a large list1 of line by line items, and I have another list2 of items that are in that large list1, and I needed to remove every line from large list1 contained in list2

I would suggest grep for this:
Code:

grep -Fvf remove-these.txt items.txt > remaining-items.txt

danielbmartin 03-17-2013 11:15 AM

Quote:

Originally Posted by ntubski (Post 4913124)
Code:

grep -Fvf remove-these.txt items.txt > remaining-items.txt

Short and sweet! Love it!

Daniel B. Martin

CaptainDerp 03-24-2013 08:21 PM

:(
 
Quote:

Originally Posted by danielbmartin (Post 4912962)
cut will do this job. This code ...

Code:

echo derf3eef3fd34rf/rf3/4f34f/4/stuffudontwant stuffIwant.here
| cut -d ' ' -f2

... produced this result ...
Code:

stuffIwant.here
Daniel B. Martin


This isnt going to work for me because I have a huge list of lines like this.

ntubski 03-24-2013 09:28 PM

Quote:

Originally Posted by CaptainDerp (Post 4918010)
This isnt going to work for me because I have a huge list of lines like this.

How is that a problem?

danielbmartin 03-24-2013 09:53 PM

Quote:

Originally Posted by CaptainDerp (Post 4918010)
This isnt going to work for me because I have a huge list of lines like this.

Perhaps you want ...
Code:

cut -d ' ' -f2 $InFile > $OutFile
Your script would specify the complete file identifiers for InFile and OutFile.
In my programs they are usually named thusly:
Code:

# File identification
  Path=$(readlink -f $0 | cut -d'.' -f1)
OutFile=$Path"out.txt"
 InFile=$Path"inp.txt"

This assumes you will have all three files (the script, InFile, and OutFile) in the same folder.

Daniel B. Martin


All times are GMT -5. The time now is 09:19 PM.