LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 04-27-2005, 10:21 AM   #1
jedimastermopar
Member
 
Registered: Jun 2004
Distribution: fedora core 1 and core 2
Posts: 178

Rep: Reputation: 30
help with writing a script


I need a script to recursivly search and edit files from a directory start point with the second line being blank and have that line removed. All other lines that are blank in the file must be left intact.
so something like

if line 2 = null then
remove line 2

Does anyone know how this would be accomplished?
 
Old 04-27-2005, 11:08 AM   #2
ahh
Member
 
Registered: May 2004
Location: UK
Distribution: Gentoo
Posts: 293

Rep: Reputation: 31
Assuming the blank lines contain no spaces, the file names contain no spaces, and all the files are plain text:-
Code:
for i in $(ls); do [ -f $i ] && cat $i | tr -s "\n" > ./$i_new; done
will create a new file with the same name as the old file, but with "_new" on the end and all the blank lines removed, from all the files in the current directory.

You should be able to build on this to accomplish what you want.
 
Old 04-27-2005, 11:52 AM   #3
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Here's an example I was putzing with
Code:
#!/bin/bash

cd /home/check

for i in *.txt ; do
   a=`head -n 2 $i |grep -n "^$" | cut -d: -f1`
if [ $a = 2 ] ; then
   sed -ie $a'd' $i
fi
done
rm -f *.txte

Last edited by homey; 04-27-2005 at 11:56 AM.
 
Old 04-27-2005, 01:10 PM   #4
ahh
Member
 
Registered: May 2004
Location: UK
Distribution: Gentoo
Posts: 293

Rep: Reputation: 31
Whoops! Missed the second line thing...

A variation on homey's script:-
Code:
#!/bin/bash

cd /home/check

for i in *.txt ; do
  ! [ $(sed -n '2p' $i) ] && sed -ie '2d' $i
done

rm -f *.txte

Last edited by ahh; 04-27-2005 at 01:15 PM.
 
Old 04-27-2005, 02:19 PM   #5
jedimastermopar
Member
 
Registered: Jun 2004
Distribution: fedora core 1 and core 2
Posts: 178

Original Poster
Rep: Reputation: 30
Yah I had a similar one to your first one but that gets rid of all instances of blank lines in the whole file instead of just the first instance if its on the second line.
 
Old 04-28-2005, 07:57 AM   #6
jedimastermopar
Member
 
Registered: Jun 2004
Distribution: fedora core 1 and core 2
Posts: 178

Original Poster
Rep: Reputation: 30
Thats great thanks, never used sed before.
 
Old 04-30-2005, 01:28 AM   #7
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
For some dumb reason, the general problem on a sed command to delete a blank second line took me a while.

jschiwal@linux:~/sedwork> cat -n test1
1 line 1
2
3 line 3
4 line 4
5
6 line 5
7 line 6
8
jschiwal@linux:~/sedwork> cat -n test2
1 line 1
2 Line 2
3 line 3
4 line 4
5
6 line 5
7 line 6
8
jschiwal@linux:~/sedwork> sed '/^$/{2d}' test1
line 1
line 3
line 4

line 5
line 6

jschiwal@linux:~/sedwork> sed '/^$/{2d}' test2
line 1
Line 2
line 3
line 4

line 5
line 6

The test for line 2 is nested inside the blank line test.
So this one liner will do what you want:
for file in *; do sed -i '/^$/{2d}' $file; done


Of course, you might want to use double quotes around "$file" if a file-name could contain white space characters.

Last edited by jschiwal; 04-30-2005 at 01:33 AM.
 
Old 05-02-2005, 11:45 AM   #8
jedimastermopar
Member
 
Registered: Jun 2004
Distribution: fedora core 1 and core 2
Posts: 178

Original Poster
Rep: Reputation: 30
The problem with the first 2 suggestions is they seemed to be removing the 2nd line even if it wasn't blank. maybe I was missing something.
Now onto the last example that works good accept it won't span directiores so.. I put this into a a file fix.sh
sed -i '/^$/{2d}' $1

then I run the following

find -name "*" -exec /home/blah/fix.sh {} \;

That way it will recurse directories.
 
Old 05-02-2005, 12:06 PM   #9
jedimastermopar
Member
 
Registered: Jun 2004
Distribution: fedora core 1 and core 2
Posts: 178

Original Poster
Rep: Reputation: 30
Strange on my test machine running redhat I can run sed with the -i flag but on my production machine -i is an invalid option? what is the -i?
It said invalid option --i
? isn't -i the flag to tell it to overwrite the file and -ie would be tomake and archive of the file with an e at the end?

Last edited by jedimastermopar; 05-02-2005 at 12:10 PM.
 
Old 05-02-2005, 01:04 PM   #10
jedimastermopar
Member
 
Registered: Jun 2004
Distribution: fedora core 1 and core 2
Posts: 178

Original Poster
Rep: Reputation: 30
Also if a line looks like a blank line and it doesn't have a space in it what is it? The test files I was using are not the same as the real files I have to edit. When I run the script on a tes file with a blank line it works but when I take the real file sed doesn't seem to recognize the line as a blank line. but when I tell sed to just delete the line it can delete the line? very strange.
 
Old 05-02-2005, 01:34 PM   #11
ahh
Member
 
Registered: May 2004
Location: UK
Distribution: Gentoo
Posts: 293

Rep: Reputation: 31
Quote:
Originally posted by jedimastermopar
Strange on my test machine running redhat I can run sed with the -i flag but on my production machine -i is an invalid option? what is the -i?
It said invalid option --i
? isn't -i the flag to tell it to overwrite the file and -ie would be tomake and archive of the file with an e at the end?
The -i option is to edit the file in place, and the -e option is just telling sed to execute the following commands. You don't realkly need it if you are just giving sed one command, but you can string commands together with it.
Quote:
Originally posted by jedimastermopar
Also if a line looks like a blank line and it doesn't have a space in it what is it? The test files I was using are not the same as the real files I have to edit. When I run the script on a tes file with a blank line it works but when I take the real file sed doesn't seem to recognize the line as a blank line. but when I tell sed to just delete the line it can delete the line? very strange.
It could be that the lines are not actually blank, but have a space or tab in them.

Try:-
Code:
sed -i '/^[[:space:]]*$/{2d}' $1
to check for spaces as well.

EDIT: I should point out that if the "e" follows the "i", as in "-ie", then sed will use the "e" as a suffix to back up the file.

p.s. You seem to have a pretty good grasp of sed now, how about looking at "man sed" to see all the options?

Last edited by ahh; 05-02-2005 at 01:55 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
help writing script willinusf Programming 7 07-20-2004 11:37 AM
Help writing a rm script VisionZ Linux - Newbie 17 03-24-2004 07:15 AM
Help Writing a script. teeth44 Programming 2 10-14-2003 12:00 PM
Writing a Script Nyc0n Linux - General 2 05-27-2002 07:21 PM
writing a Script spanky5125 Linux - Security 5 01-08-2002 09:22 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration