Delete everything after/before a word in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete everything after/before a word in a file
# 1  
Old 03-17-2010
Delete everything after/before a word in a file

I'm looking for a command that will read a file listing information and delete everything after a certain word is found. I also may need to search the file and delete everything before a certain word. The file would contains fields of information like below repeating for the entire file;

Name
Title
Loacation
DOB
phone number
hire date

I'd want to parse the file find an entry and delete everything after the hire date for the pitcular entry leaving everything before it. I also may need to parse the file find an entry and delete everything before the Name leaving everything after it.
# 2  
Old 03-17-2010
That is trivial in Perl, awk, perhaps sed.

Post some example data (beyond the field names only) desired output and perhaps an example file.
# 3  
Old 03-17-2010
If I got you correct, see the example below...
Code:
>    cat  file
Joy
Title
Loacation
DOB
phone number
10012010
jax
Title
Loacation
DOB
phone number
01012010
john
Title
Loacation
DOB
phone number
03032010


 >   sed -n '/jax/,/01012010/p' file
jax
Title
Loacation
DOB
phone number
01012010

# 4  
Old 03-17-2010
Not quiet what I'm looking for. What I want is everything after line is reached. So in the example above it would reach jax and in one case print the following;

jax
Title
Loacation
DOB
phone number
01012010
john
Title
Loacation
DOB
phone number
03032010

In another script or a seperate case it would reach jax and print the following;

Joy
Title
Loacation
DOB
phone number
10012010
# 5  
Old 03-17-2010
Try:

Code:
awk '/jax/ {f=1} !f' file

# 6  
Old 03-17-2010
I filled in the fields you have with sample data and put in a file called "test2.dat." You can see the data in the output as "Before" part.

Code:
perl -0777 -nle 'print "\nBefore:$_\n===========\n"; s/^.*Name/Name/s; s/(hire date.*?)\n.*/\1/s; print "After:\n$_\n\n";' test2.dat
Before:

Noise before we 
do
not
want

Name "Bob Dawk"
Title "the devil"
Loacation "san diego, ca"
DOB 01/01/1969
phone number 444-555-6666
hire date 6/1/1999


bunch of 
stuff
after
that 
should 

go
away!!
===========
After:
Name "Bob Dawk"
Title "the devil"
Loacation "san diego, ca"
DOB 01/01/1969
phone number 444-555-6666
hire date 6/1/1999

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete word from the file

My file has contents like below. It is named as output_file.txt /usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/SASApp_STPServer_2015-08-27_tmptcmsaslva2_19142.log /usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/SASApp_STPServer_2015-08-27_tmptcmsaslva2_19142.log... (12 Replies)
Discussion started by: Ram Kumar_BE
12 Replies

2. Shell Programming and Scripting

Delete Word between two Char.

HI Guys, I have Input:- A nput A.txt 000100/port_xcu_dev_6/port_0_dev_7 000100/port_xcu_dev_6/port_1_dev_10 000100/port_xcu_dev_2/port_2_dev_8 000100/port_xcu_dev_3/port_3_dev_11 000100/port_xcuv_9/port_4_dev_9 ... (3 Replies)
Discussion started by: pareshkp
3 Replies

3. Shell Programming and Scripting

Search the word to be deleted and delete lines above this word starting from P1 to P3

Hi, I have to search a word in a text file and then I have to delete lines above from the word searched . For eg suppose the file is like this: Records P1 10,23423432 ,77:1 ,234:2 P2 10,9089004 ,77:1 ,234:2 ,87:123 ,9898:2 P3 456456 P1 :123,456456546 P2 abc:324234 (2 Replies)
Discussion started by: vsachan
2 Replies

4. UNIX for Dummies Questions & Answers

Script to delete a word based on position in a file

Hi, I am new to unix. I want to delete 2 words placed at position say for example at 23rd and 45th position in a line. I used sed but couldnt achieve this. Example: the file contains 2 lines 12345 98765 "12345" 876 12345 98765 "64578" 876 I want to delete " placed at position 13 and 19... (4 Replies)
Discussion started by: nbks2u
4 Replies

5. Shell Programming and Scripting

How to delete the last word in a file?

Hi All, I have a file with the data as below. In this i want to delete the last word. Could you pls help me. $INSTALL_HOME/lib/fm_voucher_pol.so $INSTALL_HOME/source/sys/fm_apn_pol/fm_apn_pol_device_set_state.c In the above two lines i want to delete fm_voucher_pol.so and... (5 Replies)
Discussion started by: girish.raos
5 Replies

6. Shell Programming and Scripting

Delete character from a word

Friends, I'm looking for a command that delete the first tho caractere in a word. Here is an exp : I want to replace "20091001" by "091001" or "replace" by "place" Thx, (13 Replies)
Discussion started by: newpromo
13 Replies

7. Shell Programming and Scripting

To find a word in a file and delete the file

Hi, I need to find a word in a file, and I need to delete the file where the word resides on the file. I did try with the grep -H "word" using the find command, but no luck. (6 Replies)
Discussion started by: gsiva
6 Replies

8. Shell Programming and Scripting

Delete repeated word in text file

Hi expert, I am using C shell. And i trying to delete repeated word. Example file.txt: BLUE YELLOW RED VIOLET RED RED BLUE WHITE YELLOW BLACK and i wan store the output into a new file: BLUE (6 Replies)
Discussion started by: vincyoxy
6 Replies

9. UNIX for Dummies Questions & Answers

Delete a word

i have created the following input script CREATE OR REPLACE VIEW MMSSENDAVGUPLOADTIME_GN AS sp_strip_mean_value_type.get_val(MMSSENDAVGUPLOADTIME) MMSSENDAVGUPLOADTIME_val sp_strip_mean_value_type.get_val(MMSSENDAVGUPLOADTIME) MMSSENDAVGUPLOADTIME_counter select... (1 Reply)
Discussion started by: gseptember
1 Replies

10. Shell Programming and Scripting

How to delete a word from a file?

Hi All, I want to delete a word from file. How to do that. I have file that contains the following Information. EntityName:alba00r1.mis.amat.com OverallStatus:Minor IfName:Gi1/0 EntityName:alba00r1.mis.amat.com ] OverallStatus:Normal IfName:Se0/0/0... (4 Replies)
Discussion started by: ntgobinath
4 Replies
Login or Register to Ask a Question