LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Reading a file, with a bit of a twist to it (https://www.linuxquestions.org/questions/linux-newbie-8/reading-a-file-with-a-bit-of-a-twist-to-it-797799/)

dragonandante 03-25-2010 09:02 AM

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.

rizhun 03-25-2010 09:19 AM

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.

onebuck 03-25-2010 10:14 AM

Hi,

Welcome to LQ!

Quote:

Originally Posted by dragonandante (Post 3911756)
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

:hattip:
The above links and others can be found at 'Slackware-Links'. More than just SlackwareŽ links!

dragonandante 03-26-2010 06:56 AM

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


All times are GMT -5. The time now is 04:20 PM.