PDA

View Full Version : [bash] edit and remove a specific line from file



Ristar
August 10th, 2008, 09:31 AM
what is the code in bash to edit or remove a specific line from file?

assuming the file is:


Andy,Manager,4000
Bryan,Programmer,3000
Charlie,Clerk,2000
Danny,Clerk,1500


how do i make the program search for a match in any fields, so that the user can select which record with a field that matches the keyword the user typed to delete from the file, or edit a field?

thanks..

ghostdog74
August 10th, 2008, 09:58 AM
it seems to be related to this post (http://ubuntuforums.org/showthread.php?t=884648) and i have introduced awk to you. Why don't you try and code something. Its no use giving you code to use, and in the end you learn nothing. If in doubt, look up the awk/bash links in my sig to learn it.

Ristar
August 10th, 2008, 11:04 AM
using



sed 2d file > file


deletes the whole file... there's a work around, but it wouldnt be good if the file's very large.

wats the correct way of resolving this?

ghostdog74
August 10th, 2008, 11:33 AM
using



sed 2d file > file


deletes the whole file... there's a work around, but it wouldnt be good if the file's very large.

wats the correct way of resolving this?
redirect it to a new file. or use the -i option of sed command.

Ristar
August 10th, 2008, 02:24 PM
'

Ristar
August 10th, 2008, 10:20 PM
how can i make awk stop looking for a line after the first match and not lose the rest of the data?

Yuzem
March 11th, 2010, 12:05 PM
This is how I do it using ONLY bash:

file=$(<file)
echo "$file" | {
while read line; do
if [[ $line != *$1* ]]; then
echo $line
fi
done
} > file

Run it like this:
script text

The script should remove any line containing "text"

DaithiF
March 11th, 2010, 10:42 PM
I find it a little curious how these old threads get replied to so long after the last posting. but in any case, the OP seemed to want a command to delete only the first line that matches a particular piece of text. no doubt there are many better ways to do this, but in sed this would do it:

sed '/foo/{x;/^$/d;x}' somefile