Removal of first line in text file using unix script

Asked By 0 points N/A Posted on -
qa-featured

I have a script which creates text file whose content is as below and this file gets automatically created by putting the output of  some 'find' command into this text file :

/home/emilly

/home/emilly/abc.dat

/home/emilly/cds.txt

i want to add the piece of code in the same script which removes the first line from the file i.e. /home/emilly only.

Please suggest some useful command.

SHARE
Best Answer by willsmith
Best Answer
Best Answer
Answered By 0 points N/A #115658

Removal of first line in text file using unix script

qa-featured

Look emily it is very easy thing to do.

Suppose you have a file named 'my_book.txt'. and you want to remove the first line from it.

Type the following command/script

# Line 1. To change the name of file 'my_book.txt'

 mv my_book.txt my_book_temp.txt  

# Line 2 . To delete first line from 'my_book_temp.txt' and send the rest of the content in the file 'my_book.txt'

sed 1d my_book_temp.txt > mky_book.txt

# Line 3. To remove the temporary file named 'my_book_temp.txt'

rm my_book_temp.txt

The text written in bold are the command which can be written in your script file.

The file name 'my_book.txt' can be replaced with the file name from which you want to delete one line.

Answered By 0 points N/A #115659

Removal of first line in text file using unix script

qa-featured

Hi Emily

Removing first line from a file can be done easily using shell command.

‘tail’ command can be used for that purpose.

To display the file without first line, use this command

tail –n +2 /home/emilly

In case you are not aware of tail command, tail will accept the number of lines to be displayed from the end of a file.

For example, if you give tail -5 filename, last 5 lines of the file will be displayed.

Please follow these steps to achieve your goal.

1) tail –n +2 /home/emilly > /home/emilly.bk

2) mv /home/emilly.bk /home/emilly

Related Questions