Script to remove lines within file

AIX.

we have multiple files eg B1234567 B1235468 etc. (numbers change daily, only constant is the B prefix)

trying to use using sed '/numberrange and length varies /d ' to remove a specific number range out of one of these files , we just don't know which one its in, as it could be in any one of them. We don't want to rename the filename, we want to keep the orginal filename that came in due to reconciliation processes.

I have no problems doing this online with the file but I want a script do it

So, what commands would you use if you were doing it online?

(And, by the way, you don't use slashes in a sed command to delete a range of numbered lines from a file.)

hi

apologies, I didn't explain that well. We get multiple B* files from the banks and the names of the actual B* file changes. However one of the B* files now references two accounts/groups in that file. We want to delete the content of the 2nd account of that original file without changing the name of the original file.

we are wanting to delete lines of texts from one of those B* files . but we cant always say its an exact line number. the /^999-999/d seems to work for deleting that whole line any other lines starting with that reference number.

if I run the following manually it looks ok from the unix prompt

for i in B*
do
sed '/^999-999/d'  
done 

it seems to work, but I don't know how to save it. I tried the $i after the delete as the name but the file didn't save the removed part.

That seems strange. If you run the command:

sed '/^999-999/d' B1234567

manually, it will write the modified text of B1234567 to standard output, but it will not save the changes in B1234567.

Using your script format, you can use a temporary file:

for i in B*
do	sed '/^999-999/d' "$i" > "$i.$$" && cp "$i.$$" "$i"
	rm -f "$i.$$"
done

To rewrite the files in place on AIX, you can use ed instead of sed :

for i in B*
do	ed -s "$i" <<-EOF
		g/^999-999/d
		w
		q
EOF
done
1 Like

Thank You Don.

this worked perfectly.

for i in B*
do	sed '/^999-999/d' "$i" > "$i.$$" && cp "$i.$$" "$i"
	rm -f "$i.$$"