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)
 
 
CPUGraphicsRAMHard Drive
Exynos 4412 Quad Core ARMv7 Cortex-A9 @1.4GHz ARM Mali-400 MP4 1 GB LP DDR2 Green Memory Samsung KMVTU000LM eMMC(16GB)+MDDR(64MB) NAND F... 
OSMonitor
UltimaROM XXDLJ5 Jelly Bean 4.1.1 4.8" 720x1280 HD Super AMOLED 
  hide details  
Reply
The Riginator
(20 items)
 
 
CPUGraphicsRAMHard Drive
Exynos 4412 Quad Core ARMv7 Cortex-A9 @1.4GHz ARM Mali-400 MP4 1 GB LP DDR2 Green Memory Samsung KMVTU000LM eMMC(16GB)+MDDR(64MB) NAND F... 
OSMonitor
UltimaROM XXDLJ5 Jelly Bean 4.1.1 4.8" 720x1280 HD Super AMOLED 
  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. 
  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. 
  hide details  
Reply
post #3 of 8
Thread Starter 
Thanks, that's exactly what I needed smile.gif
The Riginator
(20 items)
 
 
CPUGraphicsRAMHard Drive
Exynos 4412 Quad Core ARMv7 Cortex-A9 @1.4GHz ARM Mali-400 MP4 1 GB LP DDR2 Green Memory Samsung KMVTU000LM eMMC(16GB)+MDDR(64MB) NAND F... 
OSMonitor
UltimaROM XXDLJ5 Jelly Bean 4.1.1 4.8" 720x1280 HD Super AMOLED 
  hide details  
Reply
The Riginator
(20 items)
 
 
CPUGraphicsRAMHard Drive
Exynos 4412 Quad Core ARMv7 Cortex-A9 @1.4GHz ARM Mali-400 MP4 1 GB LP DDR2 Green Memory Samsung KMVTU000LM eMMC(16GB)+MDDR(64MB) NAND F... 
OSMonitor
UltimaROM XXDLJ5 Jelly Bean 4.1.1 4.8" 720x1280 HD Super AMOLED 
  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
My Rig
(11 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II X6 1100T Processor ASUS Crosshair IV Formula gigabyte radeon hd 7970 ghz edition Kingston ValueRAM ECC 4G (x4) 
Hard DriveHard DriveOptical DriveCooling
Intel SSD Hitachi Black 2TB LG Blu-ray Burner Cooler Master 212 Evo 
OSPowerCase
Gentoo GNU/Linux Silverstone 1500W Antec 1200 
  hide details  
Reply
My Rig
(11 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II X6 1100T Processor ASUS Crosshair IV Formula gigabyte radeon hd 7970 ghz edition Kingston ValueRAM ECC 4G (x4) 
Hard DriveHard DriveOptical DriveCooling
Intel SSD Hitachi Black 2TB LG Blu-ray Burner Cooler Master 212 Evo 
OSPowerCase
Gentoo GNU/Linux Silverstone 1500W Antec 1200 
  hide details  
Reply
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)
 
 
CPUGraphicsRAMHard Drive
Exynos 4412 Quad Core ARMv7 Cortex-A9 @1.4GHz ARM Mali-400 MP4 1 GB LP DDR2 Green Memory Samsung KMVTU000LM eMMC(16GB)+MDDR(64MB) NAND F... 
OSMonitor
UltimaROM XXDLJ5 Jelly Bean 4.1.1 4.8" 720x1280 HD Super AMOLED 
  hide details  
Reply
The Riginator
(20 items)
 
 
CPUGraphicsRAMHard Drive
Exynos 4412 Quad Core ARMv7 Cortex-A9 @1.4GHz ARM Mali-400 MP4 1 GB LP DDR2 Green Memory Samsung KMVTU000LM eMMC(16GB)+MDDR(64MB) NAND F... 
OSMonitor
UltimaROM XXDLJ5 Jelly Bean 4.1.1 4.8" 720x1280 HD Super AMOLED 
  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)
 
 
CPUGraphicsRAMHard Drive
Exynos 4412 Quad Core ARMv7 Cortex-A9 @1.4GHz ARM Mali-400 MP4 1 GB LP DDR2 Green Memory Samsung KMVTU000LM eMMC(16GB)+MDDR(64MB) NAND F... 
OSMonitor
UltimaROM XXDLJ5 Jelly Bean 4.1.1 4.8" 720x1280 HD Super AMOLED 
  hide details  
Reply
The Riginator
(20 items)
 
 
CPUGraphicsRAMHard Drive
Exynos 4412 Quad Core ARMv7 Cortex-A9 @1.4GHz ARM Mali-400 MP4 1 GB LP DDR2 Green Memory Samsung KMVTU000LM eMMC(16GB)+MDDR(64MB) NAND F... 
OSMonitor
UltimaROM XXDLJ5 Jelly Bean 4.1.1 4.8" 720x1280 HD Super AMOLED 
  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