Finding a flatfile & deleting first line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding a flatfile & deleting first line
# 1  
Old 07-21-2009
Finding a flatfile & deleting first line

I have a small script where I want to see if a file exists & then delete the first line from it.

I have code to help me find if the file exists, but I am unsure as to how to then take in the answer and remove the first line from the flatfile:

This is what I have so far just to output if the file exists:

for a in $(cat ~/scripts/test.txt)
do

if [ -f $DEST/$a ]
then
echo file $a exists >> ~/scripts/test2.txt
else
echo file $a does not exist >> ~/scripts/test3.txt
fi
done
# 2  
Old 07-21-2009
You can take the 2nd to last line of the file with below command

Code:
sed -n '2,$ p' filename

# 3  
Old 07-21-2009
the problem is that I don't know how I can do sed to remove a line when the file name is only specified by "-f $DEST/$a" as I don't know how to put that into a variable.
# 4  
Old 07-21-2009
I have added the sed statement in your script

Code:
for a in $(cat ~/scripts/test.txt)
do

if [ -f $DEST/$a ]
then
echo file $a exists >> ~/scripts/test2.txt
sed -n '2,$ p' $DEST/$a > $DEST/$a.tmp
mv $DEST/$a.tmp $DEST/$a
else
echo file $a does not exist >> ~/scripts/test3.txt
fi
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Deleting a pattern in UNIX without deleting the entire line

Hi I have a file: r58778.3|SOURCES={KEY=f665931a...,fw,221-705}|ERRORS={16_1:T,30_1:T,56_1:C,57_1:T,59_1:A,101_1:A,115:-,158_1:C,186_1:A,204:-,271_1:T,305:-,350_1:C,368_1:G,442_1:C,472_1:G,477_1:A}|SOURCE_1="Contig_1092402550638"(f665931a359e36cea0976db191ff60ff09cc816e) I want to retain... (15 Replies)
Discussion started by: Alyaa
15 Replies

2. Shell Programming and Scripting

Finding a match and deleting the line above to it..

Hi All, I have a file with the data: Sun is Hot Moon is cool ; -- Mon Sep 10 08:54:10 CDT 2012 -- Mon Sep 11 08:54:10 CDT 2012 -- Mon Sep 12 08:54:10 CDT 2012 revoke connect from SREE; delete from = 'SREE'; grant connect to SREE with 'fastcar8'; I want to remove the line above... (8 Replies)
Discussion started by: raosr020
8 Replies

3. Shell Programming and Scripting

help with finding text and deleting line

HI All, I need to search for a particular pattern input by the user in order to delete the line. My username.txt has username@email.com:John:149.0.3.4:1 username1@email.com:Harry:149.0.3.4:1 username1@email.net:Alex:149.0.3.4:1 username1@email.edu:Nemo:149.0.3.4:1 The program i written ... (3 Replies)
Discussion started by: ichar
3 Replies

4. Shell Programming and Scripting

Deleting a line from a flatfile using Shell Script

Hi All, Can Anyone please tell me,how can I delete a line from a file. I am reading the file line by line using whil loop and validating each line..Suppose in the middle i found a particular line is invalid,i need to delete that particular line. Can anyone please help. Thanks in advance,... (14 Replies)
Discussion started by: dinesh1985
14 Replies

5. Shell Programming and Scripting

Finding duplicate lines and deleting folders based on them

Hi, I have research data, which is organized to 100 folders numbered 00-99. I have many sets of 100 folders, for different values of initial parameters. For some reason, the computer that ran the program to gather the data, didn't always create a unique seed for each folder. I anticipated that... (1 Reply)
Discussion started by: Jopi
1 Replies

6. Shell Programming and Scripting

Deleting column from a flatfile with delimiter

I have a set of flatfiles which have columns delimited by #. How can a particular column be deleted in all the flatfiles. All flatfiles have same number of columns. (5 Replies)
Discussion started by: rsprabha
5 Replies

7. Shell Programming and Scripting

Is there an efficient way in finding and deleting files?

Hi I have a script to find and delete the files which are say, noDaysOld, I am interested to find the number of such files I am fniding for deleting and then deleting it. So, the script I wrote, first finds the number of such files and then deletes, clearly this is two different steps. ... (3 Replies)
Discussion started by: guruparan18
3 Replies

8. UNIX for Dummies Questions & Answers

Help with finding certain files, and then deleting

I must really be bad at life today, because I couldn't even find a search option on the forums before asking for help, so I apologize if this is already listed somewhere. I'm pretty new to UNIX, and basically only know enough to be dangerous. I have been appointed a task to basically go to a... (2 Replies)
Discussion started by: Puck
2 Replies

9. Shell Programming and Scripting

Finding pattern & prepending a line with text

Hello Dudes, I have a task to make a unix shell script that should search for a specific TEXT in a file.If that TEXT is found, shell script should add a comment statement before that TEXT line. Ex : LINE 1 xxxxx LINE 2 xxxx CALL xxxx LINE 3 xxxx PERFORM UNTIL if i... (1 Reply)
Discussion started by: kirrushna
1 Replies

10. UNIX for Dummies Questions & Answers

Finding a column in a flatfile

I have a file which is fixed width columns. This is an offset buffer - rather than space or tab delimited. There are upto about 8 columns and I need to get all of the column 5's values into another file. The problem is that because the delimiter is a space - and some fields are blank - the 5th... (3 Replies)
Discussion started by: peter.herlihy
3 Replies
Login or Register to Ask a Question