PDA

View Full Version : "grep" on words, not lines? print matching word?



monkeyking
March 20th, 2007, 02:43 AM
I have Alot of files with the following structure


12374.1234 2345.431 #5/3 file=/afile1/myfile.data sput
16734.1234 2345.431 #5/6 file=/afile2/myfile.data sput
12334.1234 2345.431 #5/1 file=/afile4/myfile.data sput
12634.1234 2345.431 #5/7 file=/afile6/myfile.data sput
63g34.1234 2345.431 #5/3 file=/afile3/myfile.data sput


There a lot of lines, and alot of files.

What I want to do is extracting the


file=/afile4/myfile.data

part.

I thought I could use grep, but apperantly grep outputs hole lines only?
thanks in advance

simonn
March 20th, 2007, 03:36 AM
cat /path/to/file | cut -f4 -d" "

ghostdog74
March 20th, 2007, 08:26 AM
awk '{print $4}' file

ghostdog74
March 20th, 2007, 08:27 AM
cat /path/to/file | cut -f4 -d" "



cut -f4 -d" " file

Mr. C.
March 20th, 2007, 09:35 PM
cat /path/to/file | cut -f4 -d" "


Simonn,

Read week 11: "Loose Ends", "Using cat unnecessarily"

http://cis68b1.mikecappella.com/calendar.php

MrC

Cappy
March 22nd, 2007, 01:47 AM
Thanks Mr. C, it was a good read.

flip
July 23rd, 2009, 01:15 AM
I have Alot of files with the following structure


12374.1234 2345.431 #5/3 file=/afile1/myfile.data sput
16734.1234 2345.431 #5/6 file=/afile2/myfile.data sput
12334.1234 2345.431 #5/1 file=/afile4/myfile.data sput
12634.1234 2345.431 #5/7 file=/afile6/myfile.data sput
63g34.1234 2345.431 #5/3 file=/afile3/myfile.data sput


There a lot of lines, and alot of files.

What I want to do is extracting the


file=/afile4/myfile.data

part.

I thought I could use grep, but apperantly grep outputs hole lines only?
thanks in advance

Use --only-matching flag and write regex to match the 'file=...' part.
Sorry it took me so long to reply :)

soltanis
July 23rd, 2009, 06:13 AM
Simonn,

Read week 11: "Loose Ends", "Using cat unnecessarily"

http://cis68b1.mikecappella.com/calendar.php

MrC

If I didn't use cat unnecessarily, I don't know what I'd use it for at all.

ghostdog74
July 23rd, 2009, 07:39 AM
cat's primary purpose is to concatenate files. using it together with tools such as awk/cut/sed, even the while read loop is "considered" unnecessary as these tools(as well as the while read loop) takes in the file as arguments.(or redirection in the case of while read)

Sockerdrickan
July 23rd, 2009, 07:43 AM
yeah split and cat is a great combo

Linfreak
September 28th, 2010, 09:47 AM
Hi flip, Your method works fine with the given example. What if the file has content like the following?

ABCDE_VAR_JOHN_XYZ ABCDE_VAR_JACK_XYZ qwerABCDE_VAR_JACK_XYZ
.
.
similar lines.

And in the first line the "VAR" changes dynamically and also position of the ABCDE_VAR_JACK_XYZ string changes. My goal is to get just one instance of ABCDE_VAR_JACK_XYZ irrespective of how many times or where it is placed in the line?

If I use your and a regexp like "ABCDE.*JACK_XYZ" I will be getting the whole first line since it starts with ABCDE and ends with XYZ.

Thx in advance,
Siva

kurum!
September 28th, 2010, 01:47 PM
why are you resurrecting a 3yrs old thread?

Linfreak
September 28th, 2010, 04:33 PM
To get some answers for my problem thats closely related to this thread title.

Do you have some tips on how I could solve it?

Thx,
Siva