Get "n" number of lines from the specified file and store the output to the new file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get "n" number of lines from the specified file and store the output to the new file
# 1  
Old 07-29-2012
Question Get "n" number of lines from the specified file and store the output to the new file

Hiii.

How are you all. .. I have started to learn bash scripting.. . and I am pretty much trying to execute this script which I am still not successful.. .

This is what I am trying to do. ..

need to get "n" number of lines from the specified file and store the output to the new file in some other specified directory.. .and if the file with the same name exists. .the script should create a new file with date / count to the new file.

Below is the script I came up with, ,,there may be many mistakes. . . . but . .through mistakes you learn

Can someone help me here. . .THanks !!!!!!!!


Code:
#!/bin/bash

export SAVEPATH="/home/zshaikh/"
export FILENAME=$(basename $1)

COUNT=1

if [ -r $FILE ]; then

	tail -n $2 $1 >> ${SAVEPATH}${FILENAME}


elif [ -r ${SAVEPATH}${FILENAME} ]; then

	tail -n $2 $1 > ${PATH}${FILENAME}.$(date +%Y%m%d)


elif [ -r ${SAVEPATH}${FILENAME}.$(date +%Y%m%d) ]; then

	tail -n $2 $1 > ${PATH}${FILENAME}.$(date +%Y%m%d).$COUNT


elif [ -r ${SAVEPATH}${FILENAME}.$(date +%Y%m%d).$COUNT ]; then

	COUNT=`expr $COUNT + 1`

	tail -n $2 $1 > ${PATH}${FILENAME}.$(date +%Y%m%d).$COUNT


fi

# 2  
Old 07-29-2012
Did you try executing this script??

For me it seems OK. Smilie
# 3  
Old 07-29-2012
Hii . . .

When I try executing the script, I get the following errors :

basename : command not found.

and

/home/zshaikh/ is a directory

i m still not able to figure it out whats wrongSmilie
# 4  
Old 07-29-2012
I think the second error is because there are nothing in $FILENAME because of the first error with basename. This is how to get basename another way: ${1##*/} (if you want it from $1).

I don't understand PATH and FILE in your script, they don't get any values?

I also think you must have a loop in the last part:
Quote:
Originally Posted by zsycho
...
Code:
...
elif [ -r ${SAVEPATH}${FILENAME}.$(date +%Y%m%d).$COUNT ]; then
  COUNT=`expr $COUNT + 1`
  tail -n $2 $1 > ${PATH}${FILENAME}.$(date +%Y%m%d).$COUNT
fi

should be:
Code:
...
else
  while [ -r ${SAVEPATH}${FILENAME}.$(date +%Y%m%d).$COUNT ]; do
    COUNT=`expr $COUNT + 1`
  done
  tail -n $2 $1 > ${PATH}${FILENAME}.$(date +%Y%m%d).$COUNT
fi


Last edited by 244an; 07-29-2012 at 11:48 AM..
# 5  
Old 07-29-2012
ok. .. so I re-edited the script. . .

Code:
#!/bin/bash

# USE :
#script_name log_path no_of_lines

export PATH="/home/xosh/"
export FILENAME=$(/usr/bin/basename $1)
export dt=`/bin/date +%Y%m%d`

COUNT=1

if [ -r $1 ]; then

     /usr/bin/tail -n $2 $1 > ${PATH}${FILENAME}


     elif [ -e ${PATH}${FILENAME} ]; then

     /usr/bin/tail -n $2 $1 > ${PATH}${FILENAME}.${dt}
	
     elif [ -e ${PATH}${FILENAME}.${dt} ]; then

     /usr/bin/tail -n $2 $1 > ${PATH}${FILENAME}.${dt}.${COUNT}
			
     elif [ -e ${PATH}${FILENAME}.${dt}.${COUNT} ]; then

     COUNT=`/usr/bin/expr $COUNT + 1`

     /usr/bin/tail -n $2 $1 > ${PATH}${FILENAME}.${dt}.${COUNT}
fi


Now , all the files are getting created at once,.. .

the if condition is not working.. .
# 6  
Old 08-03-2012
I figured out the script.. .

Here it is . .. Thanks !!

Code:
#!/bin/bash

#USE : ./script_name path_to_file no_of_lines
#Change the SAVEPATH where you need to save

export SAVEPATH="/home/zshaikh/"
export FILENAME=$(basename $1)
COUNT=1

if ! [ -r "$1" ]; then
    	echo "File does not exist!!"
    	exit 1
fi

if ! [ "$2" ]; then
    	echo "Specify no of lines!!!"
fi

if  ! [ -e "${SAVEPATH}${FILENAME}" ]; then
    	tail -n $2 $1 > ${SAVEPATH}${FILENAME}
    	echo "File saved at ${SAVEPATH}${FILENAME}"

elif ! [ -e "${SAVEPATH}${FILENAME}.$(date +%Y%m%d)" ]; then
    	tail -n $2 $1 > ${SAVEPATH}${FILENAME}.$(date +%Y%m%d)
    	echo "File saved at ${SAVEPATH}${FILENAME}.$(date +%Y%m%d)"

else
    	while [ -e "${SAVEPATH}${FILENAME}.$(date +%Y%m%d).${COUNT}" ]; do
            	COUNT=`expr $COUNT + 1`
    	done
    	tail -n $2 $1 > ${SAVEPATH}${FILENAME}.$(date +%Y%m%d).$COUNT
    	echo "File saved at ${SAVEPATH}${FILENAME}.$(date +%Y%m%d).$COUNT"
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

3. Shell Programming and Scripting

Deleting "user input line number" from a file using sed

Hi I want to delete a line from a txt file for which the line number is user input. Say when user selects 19, the 19th line would be deleted from the file. Can anyone please provide me with a sed one liner for the same... I tried sed -i. The interaction would be like this Enter the line... (4 Replies)
Discussion started by: sudeep.id
4 Replies

4. UNIX for Dummies Questions & Answers

Deleting "user input line number" from a file using sed

Hi I want to delete a line from a txt file for which the line number is user input. Say when user selects 19, the 19th line would be deleted from the file. Can anyone please provide me with a sed one liner for the same... I tried sed -i. The interaction would be like this Enter the line to... (1 Reply)
Discussion started by: sudeep.id
1 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

Store date-of-birth as "1#7#0#2#" in file

Hello, My input files are having header/body/footer. The first three digits finds whether the data is header/body/footer. For example, 010AAAAA 20100507 234KB BBBBBBBBBB_20100506.DAT 020CCCCC DDDDDDDDD 373983983 19750426 456.90 ... (1 Reply)
Discussion started by: nvkuriseti
1 Replies

7. Shell Programming and Scripting

ksh script that echo " please insert your name " and store the output to a login.log file.

Hello All Nice to meet you all here in this forum, it's my 1rst time here i'm asking about a little issue that i face i added a ksh script that echo " please insert your name " and store the output to a login.log file. the script is working fine with normal telnet but Xstart is not working... (8 Replies)
Discussion started by: islam.said
8 Replies

8. Shell Programming and Scripting

store the output of "find" command in a variable?

I intend to find the path/full location of a file(filename given by user thru "read filenme") using "find" or any other command and then store it's output in a variable for some other processing. But struggling to put all things together (i.e finding the fully qualified location of that file and... (4 Replies)
Discussion started by: punitpa
4 Replies

9. Shell Programming and Scripting

"sed" to check file size & echo " " to destination file

Hi, I've modified the syslogd source to include a thread that will keep track of a timer(or a timer thread). My intention is to check the file size of /var/log/messages in every one minute & if the size is more than 128KB, do a echo " " > /var/log/messages, so that the file size will be set... (7 Replies)
Discussion started by: jockey007
7 Replies

10. Shell Programming and Scripting

How to store the output of "time dd if= of=" in a variable

Hi All, I need to store the real seconds of the following command in a variable. How could it be done? time $(dd if=/dev/zero of=/dev/sda1 bs=512 count=2048;sync) Thanks, Amio (12 Replies)
Discussion started by: amio
12 Replies
Login or Register to Ask a Question