1752802 Members
5491 Online
108789 Solutions
New Discussion юеВ

Re: deleting first line

 
himacs
Super Advisor

deleting first line

Hi Admins,

I want to delete first line of a line.Manually i can delete.But i want to do it using script.

Actually our tools team facing some issue with a report file.After report generation, first line will always be some junk texts.Now they are manually opening the file and deleting the first line.Now i want to avoid manual interference.

Please suggest


Regards
himacs

7 REPLIES 7
rariasn
Honored Contributor

Re: deleting first line

Hi,

sed 1d infile >> outfile

rgs
himacs
Super Advisor

Re: deleting first line

Hi Rari,

Thanks for the reply.Your suggestion worked fine.

But i dont want to create new file with output.The same file i want to modify.

If i give head -1 , i can print the first line.

But what command can be used to delete..

rm or dd ..?

Regards
himacs

Raj D.
Honored Contributor

Re: deleting first line

Himacs,

You can add 2 line code in the script ,to filter out the file & to have 1st line first line excluded :


sed '1d' report_file > report_file.tmp
mv report_file.tmp report_file
#------------------------------

Now the report_file is the filtered output with first line deleted.




- You can also do with awk:

awk 'NR !=1' report_file > report_file.tmp
mv report_file.tmp report_file
#------------------------------




Enjoy , Have fun!.
Raj.
" If u think u can , If u think u cannot , - You are always Right . "
Raj D.
Honored Contributor

Re: deleting first line

Himacs,


> But i dont want to create new file with output.The same file i want to modify.


- To modify the same file check this out:

# Deleting first line from the report_file :
perl -i -ne "print unless 1 .. 1" report_file




Enjoy, Have fun!,
Raj.
" If u think u can , If u think u cannot , - You are always Right . "
Matti_Kurkela
Honored Contributor

Re: deleting first line

Here's a small script. Name it whatever you like, for example "del-first-line.sh".
Place it into e.g. /usr/local/bin, make it executable (chmod a+x del-first-line.sh) and make sure the directory is listed in your PATH environment variable.
(Edit /etc/PATH if necessary.)

#!/bin/sh
for i in "$@"; do
mv "$i" "$i.firstlinejunk"
sed -e '1d' "$i.firstlinejunk" > "$i" && rm "$i.firstlinejunk"
done

Usage: del-first-line.sh

Example:
del-first-line.sh report*.sh

The script can process as many files as you can fit on the command line. You can use standard shell wildcards.

As the script processes each file, it renames the original with the ".firstlinejunk" suffix, then creates a new file using the original name, with the first line removed. Only if the creation of the new file is successful, the original is removed.

MK
MK
Michal Kapalka (mikap)
Honored Contributor

Re: deleting first line

James R. Ferguson
Acclaimed Contributor

Re: deleting first line

Hi Himacs:

> But i dont want to create new file with output. The same file i want to modify.

I'd use Perl as Raj suggested but with a tweak to handle multiple file specifications at one time.

In your case you want to delete the first line only and do this for one or more files at once:

# perl -i -ne 'print unless $.==1;close ARGV if eof' file1 file2 file3

Regards!

...JRF...