Visit Our UNIX and Linux User Community


Apologies from a newbie!


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Apologies from a newbie!
# 1  
Old 07-23-2009
Apologies from a newbie!

Apologies because this may have been covered elsewhere, but I DID search but couldn't find exactly what I was looking for.

My problem is as follows....

I'm doing an extract from an Oracle database, my SQL script does some date manipulation and creates the output file with some date details in the title. For example, this month the file will be called MyfileJuly09.txt, next month it will be created as MyfileAugust09, etc...

This file is then to be ftpd onto another server, and I want to automate all of this.

Due to the nature of the SQL statements in my script, my text file has a blank line between the header and data. i.e.

Member No Policy No

1 000000001
2 000000032

What I want to do is to remove the blank line so the file looks like this...

Member No Policy No
1 000000001
2 000000032

It's only this one line I want to remove. I can't remove it in the SQL script itself - all to do with concatenating columns together etc...)

I know I can use sed or perl to remove the blank line, but my problem is that I want to retain the file name!

If I use sed - sed '2,1d' inputfile > outputfile - I need to specify the inputfile name (but that changes each month) and sed wants to create a new output file, but I want to keep the original filename

How can I have a script pick up the correct file, remove the blank line and then save the file with it's original name.

If it helps, there will only ever be one file (the one just created) in the current directory with a name like - Myfile.......

Any help would be greatly appreciated.
# 2  
Old 07-23-2009
See if this helps.


Code:
 
  sed -e '2,1d' file >file.tmp&&mv file.tmp file


Last edited by lathavim; 07-23-2009 at 05:17 AM..
# 3  
Old 07-23-2009
Hello,

Per our forum rules, all threads must have a descriptive subject text. For example, do not post questions with subjects like "Help Me!", "Urgent!!" or "Doubt". Post subjects like "Execution Problems with Cron" or "Help with Backup Shell Script".

The reason for this is that nearly 95% of all visitors to this site come here because they are referred by a search engine. In order for future searches on your post (with answers) to work well, the subject field must be something useful and related to the problem!

In addition, current forum users who are kind enough to answer questions should be able to understand the essence of your query at first glance.

So, as a benefit and courtesy to current and future knowledge seekers, please be careful with your subject text. You might receive a forum infraction if you don't pay attention to this.

Thank you.

The UNIX and Linux Forums
# 4  
Old 07-23-2009
HTH
Code:
$ cat file.txt
Member No Policy No

1 000000001
2 000000032
$ perl -ni -e 'print if !/^\s*$/;' file.txt
$ cat file.txt
Member No Policy No
1 000000001
2 000000032

# 5  
Old 07-23-2009
lathavim / pludi, thank you for your super-fast reply!
DukeNuke2 - apologies for not giving correct subject text!

lathavim / pludi - If I was running either of your commands manually it would work perfectly as I would be able to enter the name of the file I want to modify, however, remember my filename changes each month and I want to automate this.

Using either

sed -e '2,1d' file >file.tmp&&mv file.tmp file
or
perl -ni -e 'print if !/^\s*$/;' file.txt

requires me to enter the filename - but like I said, it changes each month and I want to automate the process. I need the script to actually fill in the filename for me. The file will always start with Myfile, and it will be the only file in the directory starting like this.

I had thought of using

ls Myfile* > nameholder

This creates a file called nameholder, within which is the name of the current months file to be modified.

If I could then somehow get the script to read that file and save the value as a parameter I could then use that parameter in the sed or perl command

sed -e '2,1d' $myfile >file.tmp&&mv file.tmp $myfile

Thanks for your excellent suggestions though.

Any thoughts?
# 6  
Old 07-23-2009
Check this.

Code:
 
myfile=`ls Myfile*`
sed -e '2,1d' $myfile >file.tmp&&mv file.tmp $myfile

# 7  
Old 07-23-2009
Quote:
Originally Posted by lathavim
Check this.

Code:
 
myfile=`ls Myfile*`
sed -e '2,1d' $myfile >file.tmp&&mv file.tmp $myfile

Unfortunately this doesn't appear to work for me. It looks as though $Myfile is actually set to 'ls Myfile*' instead of the ls command being executed and $Myfile being set to the returned file name. Error given is..

'Can't open ls'
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #969
Difficulty: Medium
The Linux kernel is a free and open-source, Unix-like operating system microkernel.
True or False?

6 More Discussions You Might Find Interesting

1. Post Here to Contact Site Administrators and Moderators

Our Apologies for the Down Time This Weekend (Servercraft Migration)

In case you did not notice, our server was not available for about 10 hours this weekend. Our service provider (Servercraft) moved their entire data center from Houston to Dallas, TX. They sent numerous emails to me but because of how I have configured gmail, none of the messages made... (0 Replies)
Discussion started by: Neo
0 Replies

2. Post Here to Contact Site Administrators and Moderators

Help with deleting post, apologies about the earlier post.

Apologies about the earlier post, i didnt realise, could i delete that post? I apologise again, SynGc (1 Reply)
Discussion started by: SynGc
1 Replies

3. Shell Programming and Scripting

perl newbie . &&..programming newbie (question 2)

Hello everyone, I am having to do a lot of perl scripting these days and I am learning a lot. I have this problem I want to move files from a folder and all its sub folders to one parent folder, they are all .gz files.. there is folder1\folder2\*.gz and there are about 50 folders... (1 Reply)
Discussion started by: xytiz
1 Replies

4. UNIX for Dummies Questions & Answers

UNIX newbie NEWBIE question!

Hello everyone, Just started UNIX today! In our school we use solaris. I just want to know how do I setup Solaris 10 not the GUI one, the one where you have to type the commands like ECHO, ls, pwd, etc... I have windows xp and I also have vmware. I hope I am not missing anything! :p (4 Replies)
Discussion started by: Hanamachi
4 Replies

5. Post Here to Contact Site Administrators and Moderators

apologies

sorry, I recently posted twice, thinking it didn't go through. didn't mean to make you hassle with redundancy. thanks for your good work. (1 Reply)
Discussion started by: protienplant
1 Replies

6. Post Here to Contact Site Administrators and Moderators

apologies to Perderabo

Sorry Perderabo, really was only asking advice on where best to post, and if I should post. Didn't intend for that missive to be posted. (0 Replies)
Discussion started by: keith.m
0 Replies

Featured Tech Videos