LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-17-2004, 04:26 AM   #1
ericcarlson
Member
 
Registered: Jan 2002
Posts: 162

Rep: Reputation: 30
Grep pattern first line of a file


Hi again, I need to get a string from a file and can't figure out how to grep it.

This file is from a subversion commit. Its always of the form

X project/sub1/sub2/sub3/filename1.ext
X project/sub1/sub2/sub3/filename2.ext
X project/sub1/sub2/sub3/filename3.ext

X is an operation. There will be at least 1 line.
All I need is it to return is the text "project" in this example from first line.
So its something like "in the first line, get the string starting at the end of the first set of spaces up but not including the next "/" you hit. Ignore all other lines. Thanks in advance
 
Old 07-17-2004, 04:31 AM   #2
frob23
Senior Member
 
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: OpenBSD, Debian, FreeBSD
Posts: 1,450

Rep: Reputation: 48
Okay,

awk is going to be better than grep and I can find a simple command for you later when I get home (if no one else can punch one out). If you are impatient or just want to learn... `man awk`

But my real reason for posting this. Did you go to FIT and do you want to be a Vegas showgirl? Sorry if these questions seem weird but I think I either know you or you share a name with someone I know.

Let me look up a command for you.
 
Old 07-17-2004, 04:37 AM   #3
frob23
Senior Member
 
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: OpenBSD, Debian, FreeBSD
Posts: 1,450

Rep: Reputation: 48
head -n 1 | awk '{FS = "[ \/]" } {print $2}'

Okay, that should work... again, I am not at a *nix box to test this all I have is manual pages and I don't use awk everyday.



EDIT: fix typo

Last edited by frob23; 07-17-2004 at 04:40 AM.
 
Old 07-17-2004, 05:00 AM   #4
ericcarlson
Member
 
Registered: Jan 2002
Posts: 162

Original Poster
Rep: Reputation: 30
Hi Man, nahh, you got the wrong guy there - sorry!

The command is real close but not quite right.
Used as you wrote, I get "warning escape sequence '\/' treated as plain '/' then the whole path without the X part.
I tried changing the FS to [\\/] and the warning went but the rest of the path still appeared:

project/sub1/sub2/sub3/filename1.ext

Thanks for your help

Last edited by ericcarlson; 07-17-2004 at 05:02 AM.
 
Old 07-17-2004, 05:10 AM   #5
frob23
Senior Member
 
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: OpenBSD, Debian, FreeBSD
Posts: 1,450

Rep: Reputation: 48
Did you try not escaping the / ?

head -n 1 | awk '{FS = "[ /]" } {print $2}'

That might work or it might not... it is really hard to test these things when you are at a windows box. As far as I can tell that should define two seperate field seperators and not one of " /" but I could be wrong. I didn't know if I needed to escape the / so I did it out of caution.

lol, I didn't know if you were him. But I had to ask.

EDIT: If that doesn't work I will try it when I get home... but that will be after 11am EDT.

Last edited by frob23; 07-17-2004 at 05:11 AM.
 
Old 07-17-2004, 05:16 AM   #6
ericcarlson
Member
 
Registered: Jan 2002
Posts: 162

Original Poster
Rep: Reputation: 30
This works!

head changed-files.txt -n 1 | gawk '{print $2}' | gawk 'BEGIN {FS ="/"} {print $1}'
project

I saw an awk example that was nearly the same so picked up the BEGIN.
Using just one grep with [ /] gave a blank line. Youre doing better than me and im at a linux box!!
 
Old 07-17-2004, 05:32 AM   #7
frob23
Senior Member
 
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: OpenBSD, Debian, FreeBSD
Posts: 1,450

Rep: Reputation: 48
Glad to hear you got it working.

Although I had to smack myself in the forehead when I saw the solution... why was I so determined to do it in one go? Of course grabbing it like you did makes more sense.
 
Old 07-17-2004, 11:48 AM   #8
frob23
Senior Member
 
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: OpenBSD, Debian, FreeBSD
Posts: 1,450

Rep: Reputation: 48
head changed-files.txt -n 1 | gawk 'BEGIN {FS="[ \t]*|[/]"}{print $2}'

Okay, I couldn't admit to myself that I was really going down the wrong path. The above solves the problem in one sweep of awk... with the added bonus of accepting whatever amount of spaces or tabs might be between the first field and the rest as one seperator. Just in case the file format should change.

I know... I shouldn't have bothered. I just knew it could be done... just took some figuring and some time at the prompt.
 
Old 07-20-2004, 04:44 AM   #9
ericcarlson
Member
 
Registered: Jan 2002
Posts: 162

Original Poster
Rep: Reputation: 30
Hey thanks again, I'll definitely use your update. Neat work - I learned a fair bit there.
 
Old 07-20-2004, 10:01 AM   #10
mikshaw
LQ Addict
 
Registered: Dec 2003
Location: Maine, USA
Distribution: Slackware/SuSE/DSL
Posts: 1,320

Rep: Reputation: 45
Although you got it working, I thought I'd mention an alternative to head:
"grep -m 1 project" will grep project and stop after the first instance is found. This is useful in case you have something different on the first line of the file (such as a comment or a #!)
 
Old 07-20-2004, 10:36 AM   #11
frob23
Senior Member
 
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: OpenBSD, Debian, FreeBSD
Posts: 1,450

Rep: Reputation: 48
mikshaw,

The thing is, as I understand it, the file is in that format but the directory "project" may change dependant on the project the file is for. Of course I could be wrong and maybe he just wanted code to spit out the word project.
 
Old 07-20-2004, 10:51 AM   #12
ericcarlson
Member
 
Registered: Jan 2002
Posts: 162

Original Poster
Rep: Reputation: 30
Heres whats really going on

The log file is produced buy a hook script on a subversion commit.
What I am doing is automating java builds on source checkin. Its working now ;-)
You are right about the "project" part being the only variable amongst multiple projects.
All I needed was that field, the subversion log is a little limited in this respect but no matter with you guys around ;-) Its fired on checkin, so I do the magic and feed it into the various ant tasks to build the .jars. I know java well, and subversion, just not too hot on bash scripting even though I knew exactly what I needed it to do.

Oh, its also gonna be used as a cron job for overnight builds.
 
  


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
Shell Script to Delete line if pattern exists topcat Programming 22 08-23-2011 04:58 AM
printing pattern match and not whole line that matches pattern Avatar33 Programming 13 05-06-2009 06:17 AM
using grep when the pattern contains a ! farmerjoe Programming 9 03-15-2005 11:04 PM
Pattern search in a line jitz Linux - General 2 12-06-2003 04:50 AM
grep a line in a file from dir bkeating Linux - Newbie 4 07-24-2003 02:04 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:34 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