944,216 Members | Top Members by Rank

Ad:
Nov 5th, 2009
0

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

Expand 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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
Mattpd is offline Offline
47 posts
since Nov 2009
Nov 5th, 2009
0
Re: How could I grep a line of text, and delete certian fields?
Click to Expand / Collapse  Quote originally posted by Mattpd ...
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Hilliard is offline Offline
5 posts
since Nov 2009
Nov 6th, 2009
0
Re: How could I grep a line of text, and delete certian fields?
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; Nov 7th, 2009 at 5:33 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
whizkidash is offline Offline
20 posts
since Nov 2009
Nov 7th, 2009
0
Re: How could I grep a line of text, and delete certian fields?
Post an example of the data file you're working with and the give indications what changes you want to make
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Nov 7th, 2009
0
Re: How could I grep a line of text, and delete certian fields?
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
whizkidash is offline Offline
20 posts
since Nov 2009
Nov 20th, 2009
0
Re: How could I grep a line of text, and delete certian fields?
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; Nov 20th, 2009 at 12:20 am.
Reputation Points: 10
Solved Threads: 0
Light Poster
Mattpd is offline Offline
47 posts
since Nov 2009
Nov 20th, 2009
0
Re: How could I grep a line of text, and delete certian fields?
Click to Expand / Collapse  Quote originally posted by Mattpd ...
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
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Dec 7th, 2009
0
Re: How could I grep a line of text, and delete certian fields?
thanks for this information its really useful for me. because i am new in this field.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hichnam is offline Offline
3 posts
since Dec 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: Hey guys!!!
Next Thread in Shell Scripting Forum Timeline: CRONTAB help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC