Visit Our UNIX and Linux User Community


ls fed into sed help


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers ls fed into sed help
# 1  
Old 11-15-2011
Network ls fed into sed help

I am writing a file that will delete a line from all files in a directory.
Code:
sed "/#Text_to_be_deleted/d" #files_from_ls >tmp
mv tmp #files_from_ls

For some reason, whenever I test it, it will not delete the line, but it will work if I enter only one file. Is there a way to fix this?
# 2  
Old 11-16-2011
You may want to consider using grep with its -v option.

Are you invoking sed with more than one file? Can you provide a more realistic example?
# 3  
Old 11-16-2011
try this....

if u want to work with all the files in the current directory, you may to use a looping structure,
Code:
ls > ./list.txt
while read line
do
  sed "/#Text_to_be_deleted/d" $line >tmp
  mv tmp $line
done < ./list.txt
rm ./list.txt

Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 11-17-2011 at 05:39 AM.. Reason: Please use code tags, thanks
# 4  
Old 11-16-2011
Better to do cat tmp >"$line" since this won't alter file permissions and so forth, which replacing might.

You're forgetting to delete your tmp file.

You also don't need to store the files in the temporary file list.txt, you can pipe them straight into the while loop:

Code:
# CREATE A BACKUP!  You're overwriting your originals, what if something went wrong?
tar -cf ~/sed-backup.tar ./

ls | while read line
do
        sed "/#Text_to_be_deleted/d" "$line" >tmp
        cat tmp>"$line"
done
rm -f tmp


Last edited by Corona688; 11-16-2011 at 02:32 PM..
This User Gave Thanks to Corona688 For This Post:
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #47
Difficulty: Easy
Von Neumann is considered to be the 'Father of Computing'.
True or False?

6 More Discussions You Might Find Interesting

1. Solaris

Replace critical files with errors - pam_UNIX_auth.so.1 & fed

I moved my server while it was running. I was in a hurry. Also it's a bare motherboard with drives in a cage sitting basically on their own. Once I realized how badly I had hurt it doing this, I mounted the board in a tower case I repurposed, mounted the drives, power supply etc. Now the... (1 Reply)
Discussion started by: BillyPrefect
1 Replies

2. Shell Programming and Scripting

sed and awk giving error ./sample.sh: line 13: sed: command not found

Hi, I am running a script sample.sh in bash environment .In the script i am using sed and awk commands which when executed individually from terminal they are getting executed normally but when i give these sed and awk commands in the script it is giving the below errors :- ./sample.sh: line... (12 Replies)
Discussion started by: satishmallidi
12 Replies

3. UNIX for Dummies Questions & Answers

[ksh93+] Array fed by function is empty when used in main.

I feel that i am missing something obvious but i can't find what is wrong. I have a script that is launching some functions with "&" and each call is feeding the array with a value. When all calls are finished I just want to retrieve the values of that array. It is looking like that : ... (5 Replies)
Discussion started by: bibou25
5 Replies

4. Red Hat

Help reqd in installing wireless usb on fed 14

Hello all, I am fed up on trying to install my newly purchased usb wireless antenna to my fedora14 machine; so I seek help of all you linux masterminds out there. It worked well in my Win XP. Please help me in setting up it to my Fedora. . Details follows: The device model is : Aztech... (5 Replies)
Discussion started by: gsabarinath
5 Replies

5. UNIX for Dummies Questions & Answers

read user input from within a wile loop that is being fed from below

hi! i need to do a ksh script that uses a wile loop that is fed form below while read line do some things done < myfile inside the while loop i need to read user input to ask the user what he wants to do, but "read" reads the file, and not the standard input while read line do ... (2 Replies)
Discussion started by: broli
2 Replies

6. Cybersecurity

Fed up of Viruses

I am fed up of getting viruses on my computer. They come from other computers on the network and I'm fed up with them. Can anyone reccomend some virus scanning software or something that I can use to prevent my machine from getting viruses? Thanks (3 Replies)
Discussion started by: jacx2
3 Replies

Featured Tech Videos