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?

Recommended Answers

All 7 Replies

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

Hi

you can use

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

Post an example of the data file you're working with and the give indications what changes you want to make

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

If the txt file looks like this

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

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.

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

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.

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

Drop the use of grep in this case, since it is not necessary.

sed 's/OUT.*$/IN/g' < original_file > result_file

thanks for this information its really useful for me. because i am new in this field.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.