Script to remove lines within file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to remove lines within file
# 1  
Old 07-28-2015
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
# 2  
Old 07-28-2015
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.)
# 3  
Old 07-28-2015
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

Code:
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.

Last edited by Don Cragun; 07-28-2015 at 11:17 PM.. Reason: Add CODE tags.
# 4  
Old 07-28-2015
Quote:
Originally Posted by newbee2015
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

Code:
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:
Code:
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:
Code:
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:
Code:
for i in B*
do	ed -s "$i" <<-EOF
		g/^999-999/d
		w
		q
EOF
done

This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 07-29-2015
Thank you... it works now

Thank You Don.

this worked perfectly.

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


Last edited by Don Cragun; 07-29-2015 at 03:07 AM.. Reason: Add CODE tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Remove Comment Lines From Script/Input File

Hello, I have a SAS code that predominantly has comments line and the real code like below and i want to remove ONLY THE COMMENTS from the code in the single line or spanned across multiple lines. /******************************************************************** *** This Is a Comment... (4 Replies)
Discussion started by: arooonatr
4 Replies

2. Shell Programming and Scripting

Remove lines that are subsets of other lines in File

Hello everyone, Although it seems easy, I've been stuck with this problem for a moment now and I can't figure out a way to get it done. My problem is the following: I have a file where each line is a sequence of IP addresses, example : 10.0.0.1 10.0.0.2 10.0.0.5 10.0.0.1 10.0.0.2... (5 Replies)
Discussion started by: MisterJellyBean
5 Replies

3. Shell Programming and Scripting

Please help, need to create script to remove lines by date in file

Please Help (novice to PERL and SHELL scripting)…. Need to create a script which removes all lines in $filename = "cycle_calendar_ftp_out" older than current date – a variable which will be a number of days passed to script. For Ex it will look at the end date which is the last field (4) and... (2 Replies)
Discussion started by: m3pwr
2 Replies

4. UNIX for Dummies Questions & Answers

Want to remove all lines but not latest 50 lines from a file

Hi, I have a huge file which has Lacs of lines. File system got full. I want your guys help to suggest me a solution so that I can remove all lines from that file but not last 50,000 lines. I want solution which can remove lines from existing file so that I can have some space left with. (28 Replies)
Discussion started by: prashant2507198
28 Replies

5. Shell Programming and Scripting

remove : lines from file

A small question I have a test.txt file I have contents as: a:google b:yahoo : c:facebook : d:hotmail How do I remove the line with : my output should be a:google b:yahoo c:facebook d:hotmail (5 Replies)
Discussion started by: aronmelon
5 Replies

6. Shell Programming and Scripting

shell script to remove all lines from a file before a line starting with pattern

hi,, i hav a file with many lines.i need to remove all lines before a line begginning with a specific pattern from the file because these lines are not required. Can u help me out with either a perl script or shell script example:- if file initially contains lines: a b c d .1.2 d e f... (2 Replies)
Discussion started by: raksha.s
2 Replies

7. Shell Programming and Scripting

To remove the lines in my file

Hi, There seems to some hack attempts in my site. I have attached the index page of my site and I need to remove the below lines from the index page. The below lines are at the center of the file. --> </style> <script>E V A L( unescape(... (5 Replies)
Discussion started by: gsiva
5 Replies

8. Shell Programming and Scripting

Shell script to remove duplicates lines in a file

Hi, I am writing a shell script that needs to remove duplicate lines within a file by category. example: section a a c b a section b a b a c I need to remove the duplicates within th category with out removing the duplicates from the 2 different sections (one of the a's in section... (1 Reply)
Discussion started by: RichElks
1 Replies

9. Shell Programming and Scripting

Shell script: Remove blank lines

Hi gurus, I have this file with blank lines in it. How do i remove them in shell? I tried these commands but not working: sed '/^ *$/d' or sed '/^$/d' Anybody has a better idea pls? Also there are lines which starts with a single space, how do we remove the space in those lines?... (3 Replies)
Discussion started by: gholdbhurg
3 Replies

10. Shell Programming and Scripting

Command/Script to remove duplicate lines from the file?

Hello, Can anyone tell Command/Script to remove duplicate lines from the file? (2 Replies)
Discussion started by: Rahulpict
2 Replies
Login or Register to Ask a Question