grep question: stop search from finding entire line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers grep question: stop search from finding entire line
# 1  
Old 07-16-2009
grep question: stop search from finding entire line

Sorry for the title, I really don't know how to word this question or what to even search for. I tried "grep one match", "grep 1 match", "stop grep" on both google and here and haven't found something that helps, so here I go:

I have a file that's about 1.5 million lines long, every line looks like this

"name" "data data data data" "timestamp" "id"

Where each of the fields wrapped in quotes is different every time and the quoted fields are seperated by a tab. So basically a file might look like this:

"Rob" "Hello this is post 1" "06/07/09" "87124024"
"Rob" "Information here" "06/08/09" "2358234"
"Rob" "Another bit of information would be here" "06/08/09" "349235"
"James" "My name is James." "06/01/09" "3942394"
"Thomas" "Go to google.com, it's great" "06/04/09" "342349"

And so on 1.5 million times.

I need to go through the file and get rid of the "name"\t part and the \t"id" part so that


"Rob" "Hello this is post 1" "06/07/09" "87124024"
"Rob" "Information here" "06/08/09" "2358234"
"Rob" "Another bit of information would be here" "06/08/09" "349235"
"James" "My name is James." "06/01/09" "3942394"
"Thomas" "Go to google.com, it's great" "06/04/09" "342349"

would look like

"Hello this is post 1" "06/07/09"
"Information here" "06/08/09"
"Another bit of information would be here" "06/08/09"
"My name is James." "06/01/09"
"Go to google.com, it's great" "06/04/09"

I tried:

^".*"\t but that goes through the "name" "data" "timestamp" fields. I have no idea how to get it to stop after the first tab and not keep going. I tried restricting it with {1} but I now realize it'll still go to the end since it's still only one tab, it's just at the end...

I'm at a loss at what to try, I've tried a dozen other terms, but none of them did what I want. I tried ^"[[:alpha:]]"\t but that won't work since the name might have numbers or symbols in it.

Any suggestions? Any help would be appreciated.
# 2  
Old 07-16-2009
First, for future reference, please put anything that requires fixed width display (formated input, code, formated output) between [code][/code] tags (it's the '#' symbol in the button bar above the editor)

Looks like you don't want grep but awk:
Code:
$ cat input.txt
"Rob"   "Hello this is post 1"  "06/07/09"      "87124024"
"Rob"   "Information here"      "06/08/09"      "2358234"
"Rob"   "Another bit of information would be here"      "06/08/09"      "349235"
"James" "My name is James."     "06/01/09"      "3942394"
"Thomas"        "Go to google.com, it's great"  "06/04/09"      "342349"
$ awk -F"\t" '{print $2"\t"$3}' input.txt
"Hello this is post 1"  "06/07/09"
"Information here"      "06/08/09"
"Another bit of information would be here"      "06/08/09"
"My name is James."     "06/01/09"
"Go to google.com, it's great"  "06/04/09"

# 3  
Old 07-16-2009
Never heard of awk before, but thanks for the help. It worked perfectly!
# 4  
Old 07-16-2009
you can use cut two. If you only have four fields you can cut out only the 2nd an 3rd fields using something like

Code:
cut -f2-3 $yourinputfile

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Conditional grep command to search entire file

Let me give you a complete example what I am trying to achieve. 1. Below is the log file structure where I need 2,5 and 14th column of the logs after grepping through the linkId=1ddoic. Log file structure:- abc.com 20120829001415 127.0.0.1 app none11111 sas 0 0 N clk Mozilla/5.0... (3 Replies)
Discussion started by: kmajumder
3 Replies

2. UNIX for Advanced & Expert Users

grep for entire line from a log folder

As per my understanding below mentioned line of code finding a word 'boy' in $ACULOG... num_errors=`grep -i -e fail -e illegal -e exception -e "<E" -e boy $ACULOG | wc -l` if I'm not corerct, please correct me. How I can find entire line like "This is a boy" with something similar as above... (1 Reply)
Discussion started by: heyitsmeok
1 Replies

3. Shell Programming and Scripting

grep to find entire line ....

As per my understanding below mentioned line of code finding a word 'boy' in $ACULOG... num_errors=`grep -i -e fail -e illegal -e exception -e "<E" -e boy $ACULOG | wc -l` if I'm not corerct, please correct me. How I can find entire line like "This is a boy" with something similar as above... (1 Reply)
Discussion started by: heyitsmeok
1 Replies

4. UNIX for Dummies Questions & Answers

Search for a string and copy the entire line

Hello All, I am after the script or the command which can scan the entire file for a string $PART_ID and when found to extract/copy the corresponding $PART_ID value (e.g THIRE_PTY_SOFTWARE for the 1st occurance of $PART_ID in the attached file) to a file. Appreciate your help. Thanks in... (3 Replies)
Discussion started by: forumthreads
3 Replies

5. UNIX for Dummies Questions & Answers

grep line pattern search

Hello everyone, I have been trying to get a list of all files containing a line of this type: };#followed by anything with any spaces (0 or more or 0 or more tabs) before the } and between each of the characters. I have been trying this : grep '*}*;*#*' *.c but I have not been fully... (1 Reply)
Discussion started by: gio001
1 Replies

6. Shell Programming and Scripting

Awk+Grep Input file needs to match a column and print the entire line

I'm having problems since few days ago, and i'm not able to make it works with a simple awk+grep script (or other way to do this). For example, i have a input file1.txt: cat inputfile1.txt 218299910417 1172051195 1172070231 1172073514 1183135117 1183135118 1183135119 1281440202 ... (3 Replies)
Discussion started by: poliver
3 Replies

7. Shell Programming and Scripting

finding the line number from a grep ?

Hi there does anybody know how i can get the line number from an entry or entries in a file ?? for example if i had a file test1 test2 test3 test1 and i needed to get the line numbers for all instances of test1 in that file with the answer being (1,4) Would anybody be able... (7 Replies)
Discussion started by: hcclnoodles
7 Replies

8. UNIX for Dummies Questions & Answers

grep entire statement not just line

(extract from SQL binlog file...) # at 4960 #080801 14:35:31 server id 4 end_log_pos 195 Query thread_id=63121426 exec_time=0 error_code=0 use d_jds; SET TIMESTAMP=1217581531; UPDATE bid_details set bidding = 3170.37 ,deduction=if((3170.37 < 37.43),0,deduction) where... (3 Replies)
Discussion started by: shantanuo
3 Replies

9. Shell Programming and Scripting

Perl Search and replace entire line

I have a perl function in my script that needs to replace an entire line in a file sub changestate { my $base = (); my @base = (); open(BASE, $file) || die("Could not open file!"); @base=<BASE>; close (BASE); foreach $base(@base) { if($base =~... (1 Reply)
Discussion started by: insania
1 Replies

10. Shell Programming and Scripting

search whole line using grep

hi, how to search whole line using grep in a file. (1 Reply)
Discussion started by: useless79
1 Replies
Login or Register to Ask a Question