Grep Commands


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep Commands
# 1  
Old 08-28-2013
Grep Commands

I have a text file that has multiple lines in it with multiple dates and years. I was trying to figure out the best way to remove the line that had 2010 and 2011 to an archive file. We only need to keep the 2012 and 2013 lines in the original file. The following is a sample line in the txt file:

Code:
20126789|Active|08/28/2013 08:42:28 AM

If I do the following command it will take all of the 2010's and place it in the archive file and leave the original file alone:

Code:
egrep '[0-9][0-9]/[0-9][0-9]/2010' "textfile.txt" >> "textfile_2010.txt"

What is the correct format to also grab 2011? I tried [2010][2011] and that did not work.

I also tried the following to grab everything but 2010 to put in a new file:

Code:
egrep -v '[0-9][0-9]/[0-9][0-9]/2010' "textfile.txt" >> "textfile_archive.txt"

However, I would prefer to only have one or two commands to do what I need. Therefore, what's the simplest way to do the following:

1) Delete the lines that have a date of ##/##/2010 and ##/##/2011 in textfile.txt and place them in textfile_archive.txt

Results would be:

textfile.txt
Code:
20126789|Active|01/01/2012 08:42:28 AM
20126789|Active|12/31/2013 08:40:28 AM

textfile_archive.txt
Code:
20136789|Active|01/01/2010 11:42:28 AM
20126789|Active|12/31/2011 05:40:28 PM

Any help is appreciated!

Thanks!

Last edited by Scott; 08-28-2013 at 01:25 PM.. Reason: Please use code tags for code and data
# 2  
Old 08-28-2013
You could try something like this:
Code:
awk -F\| '{
  print > ($3 ~ /201[01]/ ? "textfile_archive.txt" : "__tmp__")
  }' textfile.txt

textfile_archive.txt will contain the records from 2010/2011, __tmp__ will contain the rest of the records.
If you want to overwrite the original textfile.txt, just add the following after the awk code:
Code:
mv __tmp__ textfile.txt

# 3  
Old 08-28-2013
Try this ORed regex:
Code:
egrep '[0-9][0-9]/[0-9][0-9]/(2010|2011)' file
20136789|Active|01/01/2010 11:42:28 AM
20126789|Active|12/31/2011 05:40:28 PM
egrep -v '[0-9][0-9]/[0-9][0-9]/(2010|2011)' file
20126789|Active|01/01/2012 08:42:28 AM
20126789|Active|12/31/2013 08:40:28 AM

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Task grep and awk commands

Hi everyone, I have a question, please help me. awk -F: '$3<75' /etc/passwd grep -ir nashorn ./ | grep "^*\.java" what do these commands do ? Thanks (2 Replies)
Discussion started by: burak171
2 Replies

2. Homework & Coursework Questions

Grep commands: need some practice help please

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Use the less command to view the words file. To see the results of a grep that returns many rows, pipe the... (3 Replies)
Discussion started by: asaint
3 Replies

3. Shell Programming and Scripting

Help with ls, grep commands

Oracle Linux 6.4/Bash shell I have six files as shown below. Using ls/grep (or anything) , I need to list all files which start with the pattern stomper but not the ones which ends with 1. $ touch stompera $ touch stomperb $ touch stomperc $ touch stompera1 $ touch stomperb1 $ touch... (7 Replies)
Discussion started by: kraljic
7 Replies

4. Shell Programming and Scripting

Join two commands sed and grep

Hi all, I have two separate commands which I would like to join. Basically, I want to match a line and insert a character at the end of the previous line to the matched line Here is what I have gotgrep -B1 '^>' sed 's/$/*/' Any help is much appreciated thanks (5 Replies)
Discussion started by: kaav06
5 Replies

5. Shell Programming and Scripting

Grep commands & format

I have these grep commands and need to put them next each other (in horizontal layout). cat /tmp/dsmc.out |grep Done cat /tmp/dsmc.out |grep "Elapsed processing time:" cat /tmp/dsmc.out |grep "Client date/time:" cat /tmp/dsmc.out |grep "Total number of bytes transferred:" so that it... (6 Replies)
Discussion started by: Daniel Gate
6 Replies

6. Shell Programming and Scripting

echo and grep commands

Hey im new in this...anything will be helpful... The user will input the word or phrase .... I want to search the user input in file (by lines) but not all then with this line search on another file ( with the specific line) and show to the user. Example: file1.txt ======= a aa aaa... (2 Replies)
Discussion started by: Sundown
2 Replies

7. Shell Programming and Scripting

Difference between 2 grep - commands

Hi, I need to know the difference between this commands: grep * *search* grep "*" *search* As far as i know does the 2nd command search for files which have a name with *search* and greps then all which have chars from a-z in the file content. But was does the first command?? Best... (1 Reply)
Discussion started by: xus
1 Replies

8. UNIX for Dummies Questions & Answers

grep commands

I need your's help to display user with greep command form /etc/passwd 1. to display all login to begin and finishing with letter a or b etc.users admina bserb broota 2. beginning and finishing with the same sign etc. users aghata:.... roootr:....3. to contain what the least three... (1 Reply)
Discussion started by: ViruS89
1 Replies

9. Shell Programming and Scripting

Grep commands in loop

Hi All, Reference to my previous post I need to compare all the lines in the file1 with file2 for this condition if file1 {$3,$5} ==file2 {$3,$5} then grep file2{$1}latest date. need output in file3 10/04/2008 09/04/2008 09/04/2008 08/04/2008 can anyone suggest me Thanks... (0 Replies)
Discussion started by: karthikn7974
0 Replies

10. UNIX for Dummies Questions & Answers

find and grep commands

I'm having trouble with the following commands i. count the number of lines which end in a 4 letter word grep '{4\}$' bfile <<seems to print out everything abc abc abcd joe joe john bob bill gregory greg greg gregory the grep command prints out the lines with 4 letter words and the... (3 Replies)
Discussion started by: StrengthThaDon
3 Replies
Login or Register to Ask a Question