| | |
How could I grep a line of text, and delete certian fields?
![]() |
•
•
Join Date: Nov 2009
Posts: 16
Reputation:
Solved Threads: 0
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?
•
•
Join Date: Nov 2009
Posts: 5
Reputation:
Solved Threads: 0
0
#2 19 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?
man awk
0
#3 18 Days Ago
Hi
you can use
here it will delete the 4 and 5 file and transfer the remaing file in newfile
regards
whizkidash
you can use
Shell Scripting Syntax (Toggle Plain Text)
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; 17 Days Ago at 5:33 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
0
#4 17 Days Ago
Post an example of the data file you're working with and the give indications what changes you want to make
0
#5 17 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
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
•
•
Join Date: Nov 2009
Posts: 16
Reputation:
Solved Threads: 0
0
#6 4 Days Ago
If the txt file looks like this
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
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)
cat file.txt HOOK:MARK:IN TOOL:TIM:OUT:06/12/2007:MATT 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)
cat file.txt HOOK:MARK:IN TOOL:TIM:IN 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)
grep -w "^$VAR:.*out" file.txt | sed 's/\<out\>.*/in/' >> file.txt sed '/'"^$VAR":.*out'/d' parts.txt > parts.tmp mv parts.tmp parts.txt
Last edited by Mattpd; 4 Days Ago at 12:20 am.
0
#7 4 Days Ago
•
•
•
•
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)
grep -w "^$VAR:.*out" file.txt | sed 's/\<out\>.*/in/' >> file.txt sed '/'"^$VAR":.*out'/d' parts.txt > parts.tmp mv parts.tmp parts.txt
Shell Scripting Syntax (Toggle Plain Text)
sed 's/OUT.*$/IN/g' < original_file > result_file
![]() |
Similar Threads
- Delete last line from text file (Java)
- how to delete the last line of the text file using c# (ASP.NET)
- Removing First Line Of Text From multiline textbox (VB.NET)
- Code Snippet: delete a line from a text file (C++)
- selecting a line from a text box (Visual Basic 4 / 5 / 6)
- VB6 - Spredsheet - Some line of text in cell not visible when printed (Visual Basic 4 / 5 / 6)
- ostream - how to insert a line of text (C)
- Sitewide Multi Line Text Link. Min. 250,000 views (Ad Space for Sale)
- how do i read the last line of a text file? (Python)
- 10 line text file (Java)
Other Threads in the Shell Scripting Forum
- Previous Thread: Using user input in BASH array.
- Next Thread: Removing Lines That Contain Spaces In A File
| Thread Tools | Search this Thread |






