How could I grep a line of text, and delete certian fields?

Reply

Join Date: Nov 2009
Posts: 13
Reputation: Mattpd is an unknown quantity at this point 
Solved Threads: 0
Mattpd Mattpd is offline Offline
Newbie Poster

How could I grep a line of text, and delete certian fields?

 
0
  #1
18 Days Ago
I need to grep a line from a text file, and delete the fourth and fifth field. then save it back to the text file. If that wont work I could also grep the line, and delete the first number and everything after it on that line. Both would accomplish my goal. Forgive me, I am a little newbish, but can anyone help?
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 5
Reputation: Hilliard is an unknown quantity at this point 
Solved Threads: 0
Hilliard Hilliard is offline Offline
Newbie Poster
 
0
  #2
17 Days Ago
Originally Posted by Mattpd View Post
I need to grep a line from a text file, and delete the fourth and fifth field. then save it back to the text file. If that wont work I could also grep the line, and delete the first number and everything after it on that line. Both would accomplish my goal. Forgive me, I am a little newbish, but can anyone help?

man awk
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 15
Reputation: whizkidash is an unknown quantity at this point 
Solved Threads: 0
whizkidash's Avatar
whizkidash whizkidash is offline Offline
Newbie Poster
 
0
  #3
17 Days Ago
Hi

you can use

Shell Scripting Syntax (Toggle Plain Text)
  1. sed ('4,5d') file.txt(your file) > newfile

here it will delete the 4 and 5 file and transfer the remaing file in newfile


regards
whizkidash
Last edited by peter_budo; 16 Days Ago at 5:33 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,187
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 571
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #4
16 Days Ago
Post an example of the data file you're working with and the give indications what changes you want to make
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 15
Reputation: whizkidash is an unknown quantity at this point 
Solved Threads: 0
whizkidash's Avatar
whizkidash whizkidash is offline Offline
Newbie Poster
 
0
  #5
16 Days Ago
Hi ,

Example as below
Suppose u have file called file1 and it contains:
vi file1
Hi this line one
this is line two
this is line three
this is line four
this is line five

Now you want to delete the 1st and 5th line and transfer the remaining lines to newbee file

then use sed '1,5d' file1 > newbee



Cheers!,
Whizkidash
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 13
Reputation: Mattpd is an unknown quantity at this point 
Solved Threads: 0
Mattpd Mattpd is offline Offline
Newbie Poster
 
0
  #6
3 Days Ago
If the txt file looks like this
Shell Scripting Syntax (Toggle Plain Text)
  1. cat file.txt
  2.  
  3. HOOK:MARK:IN
  4. TOOL:TIM:OUT:06/12/2007:MATT
  5. BULB:MIKE:IN

I want to grep the line containing TOOL, and delete the the 3rd and 4th field (field separator is : ), and change "IN" to "OUT" so the result is
Shell Scripting Syntax (Toggle Plain Text)
  1. cat file.txt
  2.  
  3. HOOK:MARK:IN
  4. TOOL:TIM:IN
  5. BULB:MIKE:IN

This script below gives me the proper result, but I had to cheat to do it. I just substituted "OUT:06/12/2007:MATT" for "IN" and appended it to the file.txt and then deleted the entire old line. Only problem is that the line is now at the end of the file and not in its original spot, which is not a problem at all in my case, but I am still interested if it can be done the way I originally asked.

Shell Scripting Syntax (Toggle Plain Text)
  1. grep -w "^$VAR:.*out" file.txt | sed 's/\<out\>.*/in/' >> file.txt
  2. sed '/'"^$VAR":.*out'/d' parts.txt > parts.tmp
  3. mv parts.tmp parts.txt
Last edited by Mattpd; 3 Days Ago at 12:20 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,029
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 176
Aia's Avatar
Aia Aia is offline Offline
Postaholic
 
0
  #7
3 Days Ago
Originally Posted by Mattpd View Post
This script below gives me the proper result, but I had to cheat to do it. I just substituted "OUT:06/12/2007:MATT" for "IN" and appended it to the file.txt and then deleted the entire old line. Only problem is that the line is now at the end of the file and not in its original spot, which is not a problem at all in my case, but I am still interested if it can be done the way I originally asked.

Shell Scripting Syntax (Toggle Plain Text)
  1. grep -w "^$VAR:.*out" file.txt | sed 's/\<out\>.*/in/' >> file.txt
  2. sed '/'"^$VAR":.*out'/d' parts.txt > parts.tmp
  3. mv parts.tmp parts.txt
Drop the use of grep in this case, since it is not necessary.
Shell Scripting Syntax (Toggle Plain Text)
  1. sed 's/OUT.*$/IN/g' < original_file > result_file
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC