Overclock.net › Forums › Software, Programming and Coding › Operating Systems › Linux, Unix › Using sed to remove text from a file
New Posts  All Forums:Forum Nav:

Using sed to remove text from a file

post #1 of 8
Thread Starter 
I was to search a file for a particular string and delete it from the file.

Is sed the best way of doing this?

I've tried this:
Code:
#!/sbin/sh
busybox sed 's/String_1/ /' /system/file.txt
busybox sed 's/String_2/ /' /system/file.txt
busybox sed 's/String_3/ /' /system/file.txt

But it doesn't actually delete anything.

This:
Code:
#!/sbin/sh
busybox sed 's/String_1/ /' /system/file.txt > /system/file.txt
busybox sed 's/String_2/ /' /system/file.txt > /system/file.txt
busybox sed 's/String_3/ /' /system/file.txt > /system/file.txt

...deletes the whole file

I need to use busybox for this as it has be run on a Android device...
The Riginator
(20 items)
 
 
Wife's Rig
(5 items)
 
CPUGraphicsRAMHard Drive
Qualcomm Snapdragon S600 Adreno 330 2GB LPDDR3 NAND Storage 
Hard DriveOSMonitorKeyboard
Samsung 32GB MicroSD Android 4.4.2 KitKat 5 inch (441ppi) 1080x1920 Super AMOLED SwiftKey 
Power
2600mAh Battery 
  hide details  
Reply
The Riginator
(20 items)
 
 
Wife's Rig
(5 items)
 
CPUGraphicsRAMHard Drive
Qualcomm Snapdragon S600 Adreno 330 2GB LPDDR3 NAND Storage 
Hard DriveOSMonitorKeyboard
Samsung 32GB MicroSD Android 4.4.2 KitKat 5 inch (441ppi) 1080x1920 Super AMOLED SwiftKey 
Power
2600mAh Battery 
  hide details  
Reply
post #2 of 8
Try this...
Code:
#!/sbin/sh
sed -e 's/String_1//' input_file > output_file
mv output_file input_file
sed -e 's/String_2//' input_file > output_file
mv output_file input_file
sed -e 's/String_3//' input_file > output_file
mv output_file input_file
Meh
(14 items)
 
   
CPUMotherboardGraphicsRAM
i5 760 @ 3.8 Ghz GA-P55-USB3 Gigabyte GTX 460 16GB(4x4) Corsair Vengeance 
Hard DriveOptical DriveCoolingOS
Samsung Spinpoint F3 1TB Asus Cheapo Corsair H70 Arch Linux 
MonitorKeyboardPowerCase
LG 26LH20 HDTV Das Model S Ultimate Silent Corsair HX850 Lian Li(Lancool) PC-K62 
MouseMouse Pad
Logitech MX 518 My desk. 
CPUMotherboardGraphicsRAM
Intel Core i5-4200U Toshiba Intel HD Graphics 4400 6 GB DDR3L @1600 Mhz 
Hard DriveOptical DriveCoolingOS
750 GB 5400RPM SATA HDD DVD SuperMulti drive Stock fans Arch Linux 
MonitorKeyboardPowerCase
15.6" 1920x1080 Screen Toshiba Toshiba battery and A/C adapter Toshiba 
MouseAudio
Touchpad Intel HD Audio 
  hide details  
Reply
Meh
(14 items)
 
   
CPUMotherboardGraphicsRAM
i5 760 @ 3.8 Ghz GA-P55-USB3 Gigabyte GTX 460 16GB(4x4) Corsair Vengeance 
Hard DriveOptical DriveCoolingOS
Samsung Spinpoint F3 1TB Asus Cheapo Corsair H70 Arch Linux 
MonitorKeyboardPowerCase
LG 26LH20 HDTV Das Model S Ultimate Silent Corsair HX850 Lian Li(Lancool) PC-K62 
MouseMouse Pad
Logitech MX 518 My desk. 
CPUMotherboardGraphicsRAM
Intel Core i5-4200U Toshiba Intel HD Graphics 4400 6 GB DDR3L @1600 Mhz 
Hard DriveOptical DriveCoolingOS
750 GB 5400RPM SATA HDD DVD SuperMulti drive Stock fans Arch Linux 
MonitorKeyboardPowerCase
15.6" 1920x1080 Screen Toshiba Toshiba battery and A/C adapter Toshiba 
MouseAudio
Touchpad Intel HD Audio 
  hide details  
Reply
post #3 of 8
Thread Starter 
Thanks, that's exactly what I needed smile.gif
The Riginator
(20 items)
 
 
Wife's Rig
(5 items)
 
CPUGraphicsRAMHard Drive
Qualcomm Snapdragon S600 Adreno 330 2GB LPDDR3 NAND Storage 
Hard DriveOSMonitorKeyboard
Samsung 32GB MicroSD Android 4.4.2 KitKat 5 inch (441ppi) 1080x1920 Super AMOLED SwiftKey 
Power
2600mAh Battery 
  hide details  
Reply
The Riginator
(20 items)
 
 
Wife's Rig
(5 items)
 
CPUGraphicsRAMHard Drive
Qualcomm Snapdragon S600 Adreno 330 2GB LPDDR3 NAND Storage 
Hard DriveOSMonitorKeyboard
Samsung 32GB MicroSD Android 4.4.2 KitKat 5 inch (441ppi) 1080x1920 Super AMOLED SwiftKey 
Power
2600mAh Battery 
  hide details  
Reply
post #4 of 8
An easier way is to use the -i flag. For instance:
Code:
busybox sed -i 's/String_1/ /' /system/file.txt
busybox sed -i 's/String_2/ /' /system/file.txt
busybox sed -i 's/String_3/ /' /system/file.txt
post #5 of 8
Thread Starter 
I went with:
Code:
#!/sbin/sh
sed -e '/BlahBlahBlah/d' /system/file.txt > /system/file.txt.bak

But it won't work via CWM for Android. Runs perfectly IN Android, but won't flash using Edify.
The Riginator
(20 items)
 
 
Wife's Rig
(5 items)
 
CPUGraphicsRAMHard Drive
Qualcomm Snapdragon S600 Adreno 330 2GB LPDDR3 NAND Storage 
Hard DriveOSMonitorKeyboard
Samsung 32GB MicroSD Android 4.4.2 KitKat 5 inch (441ppi) 1080x1920 Super AMOLED SwiftKey 
Power
2600mAh Battery 
  hide details  
Reply
The Riginator
(20 items)
 
 
Wife's Rig
(5 items)
 
CPUGraphicsRAMHard Drive
Qualcomm Snapdragon S600 Adreno 330 2GB LPDDR3 NAND Storage 
Hard DriveOSMonitorKeyboard
Samsung 32GB MicroSD Android 4.4.2 KitKat 5 inch (441ppi) 1080x1920 Super AMOLED SwiftKey 
Power
2600mAh Battery 
  hide details  
Reply
post #6 of 8
if it's a whole line you're deleting, then you're probably better off using grep. sed comes into it's own when you're replacing text
post #7 of 8
Thread Starter 
Quote:
Originally Posted by Plan9 View Post

if it's a whole line you're deleting, then you're probably better off using grep. sed comes into it's own when you're replacing text

How would I do this?
The Riginator
(20 items)
 
 
Wife's Rig
(5 items)
 
CPUGraphicsRAMHard Drive
Qualcomm Snapdragon S600 Adreno 330 2GB LPDDR3 NAND Storage 
Hard DriveOSMonitorKeyboard
Samsung 32GB MicroSD Android 4.4.2 KitKat 5 inch (441ppi) 1080x1920 Super AMOLED SwiftKey 
Power
2600mAh Battery 
  hide details  
Reply
The Riginator
(20 items)
 
 
Wife's Rig
(5 items)
 
CPUGraphicsRAMHard Drive
Qualcomm Snapdragon S600 Adreno 330 2GB LPDDR3 NAND Storage 
Hard DriveOSMonitorKeyboard
Samsung 32GB MicroSD Android 4.4.2 KitKat 5 inch (441ppi) 1080x1920 Super AMOLED SwiftKey 
Power
2600mAh Battery 
  hide details  
Reply
post #8 of 8
Quote:
Originally Posted by Rubers View Post

How would I do this?
Code:
grep -v "search string" input.txt
You may need to redirect to another file. eg
Code:
grep -v "search string" input.txt > output.txt
as grep might go a little odd if you're trying to modify a file while it's being read in.

[edit]

grep also supports regex.
Edited by Plan9 - 11/12/12 at 3:39am
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Linux, Unix
Overclock.net › Forums › Software, Programming and Coding › Operating Systems › Linux, Unix › Using sed to remove text from a file