LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 03-25-2010, 09:02 AM   #1
dragonandante
LQ Newbie
 
Registered: Feb 2010
Posts: 4

Rep: Reputation: 0
Reading a file, with a bit of a twist to it


I'm writing a script that reads from a file. Here is an example of what the file looks like.

username1 24-Mar-10.19:47:21 /directorytheyworkedon
Some comment
username2 24-Mar-10.19:46:40 /directorytheyworkedon
Some comment
username3 24-Mar-10.19:43:44 /directorytheyworkedon
Some comment

Now what I want to do is pull out only the usernames and put them into a list. After I intend to email each user individually and tell them what directory they worked on, their comment, and the date. The road block I ran into is I can't figure out how to differentiate between the line with the usernames, and the line with the comments. Any suggestions would be greatly appreciated.

Last edited by dragonandante; 03-25-2010 at 09:05 AM.
 
Old 03-25-2010, 09:19 AM   #2
rizhun
Member
 
Registered: Jun 2005
Location: England
Distribution: Ubuntu, SLES, AIX
Posts: 268

Rep: Reputation: 47
Hi dragonandante,

Awk, if you didn't know, is the best program in the command-line world.

Today Awk is going to save your ass:

Code:
awk '

  !/.+[0-3][0-9]-[A-Z][a-z][a-z]-/ {
    next
  }

  {
    print $1
  }

' /path/to/your/input/file.txt
You could do that all on one line, but I think it's clearer to read over multiple lines.

Code breakdown:
Code:
  !/.+[0-3][0-9]-[A-Z][a-z][a-z]-/ {
    next
  }
This bit says if the current line does NOT (this is what the '!' does) match the pattern (this bit, which matches the date stamp from your example: '.+[0-3][0-9]-[A-Z][a-z][a-z]-') then move on to the next line.

Code:
  {
    print $1
  }
If you make it to this part of the code, we must be looking at the data lines you are interested in (because they passed the pattern match (regex)), it just prints the 1st field ($1).

Happy Awk'ing.
 
2 members found this post helpful.
Old 03-25-2010, 10:14 AM   #3
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: Slackware®
Posts: 12,728
Blog Entries: 27

Rep: Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058
Hi,

Welcome to LQ!

Quote:
Originally Posted by dragonandante View Post
I'm writing a script that reads from a file. Here is an example of what the file looks like.

username1 24-Mar-10.19:47:21 /directorytheyworkedon
Some comment
username2 24-Mar-10.19:46:40 /directorytheyworkedon
Some comment
username3 24-Mar-10.19:43:44 /directorytheyworkedon
Some comment

Now what I want to do is pull out only the usernames and put them into a list. After I intend to email each user individually and tell them what directory they worked on, their comment, and the date. The road block I ran into is I can't figure out how to differentiate between the line with the usernames, and the line with the comments. Any suggestions would be greatly appreciated.
Now that we are all aware of your 'needs'. What are your 'deeds'?

It would be easier to aid you if the problem was defined better along with what you have attempted.

Just a few links to aid you;

Linux Documentation Project
Rute Tutorial & Exposition
Linux Command Guide
Utimate Linux Newbie Guide
LinuxSelfHelp
Getting Started with Linux
Bash Reference Manual <<<<<
Advanced Bash-Scripting Guide <<<<<<
Linux Home Networking
Virtualiation- Top 10


The above links and others can be found at 'Slackware-Links'. More than just Slackware® links!
 
1 members found this post helpful.
Old 03-26-2010, 06:56 AM   #4
dragonandante
LQ Newbie
 
Registered: Feb 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Smile

Many thanks for the help and the links. I'll be sure to throw down my deeds in the future.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] File size limitations of a 32 bit program running under a 64 bit kernel acerostech Linux - Server 1 04-19-2009 10:01 AM
file comparison script with a twist poconn02 Programming 2 05-30-2008 06:11 AM
Reading/Wirting file/parsing xml file using javascript fakhrul Programming 1 08-14-2007 05:08 PM
Sort File by Field - but with a Twist! ;) moo-cow Programming 8 06-12-2006 11:26 AM
awk: fatal:cannot open file for reading (no such file or Directory) in Linux sangati vishwanath Linux - Software 4 07-06-2005 12:59 AM


All times are GMT -5. The time now is 05:00 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration