Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

To check the file and remove header before copying

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #15  
Old Unix and Linux 06-17-2017   -   Original Discussion by rohit_shinez
rohit_shinez rohit_shinez is offline
Registered User
 
Join Date: Jul 2013
Last Activity: 17 November 2017, 11:40 PM EST
Posts: 187
Thanks: 59
Thanked 0 Times in 0 Posts
Hi Don,

I agree with your point so is there a best way to tune the code, Like remove header from only the file which ends with _01.txt and copy to respective folders before i copy to destination folder and also i am using multiple if conditions because i need to copy the files to respective destination folders based on property file.Below code which is making redundant


Code:
if [ "$i" == "aus" ]
then
if [ -f $file ]
then

      	cp $file /output/aus/
else 
	echo "No aus files"

fi

elif [ "$i" == "nz" ]
if [ -f $file ]
then

      	cp $file /output/nz/
else 
	echo "No nz files"

fi

Sponsored Links
    #16  
Old Unix and Linux 06-17-2017   -   Original Discussion by rohit_shinez
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 24 November 2017, 6:08 AM EST
Location: San Jose, CA, USA
Posts: 10,682
Thanks: 575
Thanked 3,739 Times in 3,191 Posts
Quote:
Originally Posted by rohit_shinez View Post
Hi Don,

I agree with your point so is there a best way to tune the code, Like remove header from only the file which ends with _01.txt and copy to respective folders before i copy to destination folder and also i am using multiple if conditions because i need to copy the files to respective destination folders based on property file.Below code which is making redundant


Code:
if [ "$i" == "aus" ]
then
if [ -f $file ]
then

      	cp $file /output/aus/
else 
	echo "No aus files"

fi

elif [ "$i" == "nz" ]
if [ -f $file ]
then

      	cp $file /output/nz/
else 
	echo "No nz files"

fi

If you keep showing us the same nested if ladder that you know doesn't work when you have had several posts explaining why that code doesn't work and you seem to ignore all of the suggestions provided, it makes the volunteers here who are trying to help you wonder if there is any reason to respond...

You could start by defining the variables i and file before you use them.

Then you could replace the if ladder used to determine what country code to use with a case statement.

Then you could replace the cp statements with sed or tail commands that have been suggested earlier in this thread.

And, you're probably going to want a while loop reading in the values from your country code file surrounding your case statement.

If I understand what you're trying to do, and I am not at all convinced that I do, it would seem that a more direct approach might help:

Code:
cd /country || exit 1
exit_code=0
while read -r country_code
do	file="${country_code}_01.txt"
	if [ -f "$file" ]
	then	sed 1d "$file" > "/output/$country_code/$file" || exit_code=2
	else	printf 'No %s file (%s) found.\n' "$country_code" "$PWD/$file" >&2
		exit_code=2	# omit this line and remove the redirection in the
				# above printf if the above output is a warning rather
				# than an error.
	fi
done < property.txt
exit $exit_code

Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Check for empty line at end of a dynamic header in each file tpk UNIX for Dummies Questions & Answers 6 04-16-2015 04:54 AM
Remove the file content based on the Header of the file i150371485 Shell Programming and Scripting 5 08-03-2012 03:35 AM
Remove last few characters in a file but keeping Header and trailer intact nvuradi Shell Programming and Scripting 2 04-12-2012 03:16 PM
Check header of file, without opening the file lydiaflamp UNIX for Dummies Questions & Answers 2 10-03-2010 06:41 PM
Copy all the files with time stamp and remove header,trailer from file ksrams UNIX for Dummies Questions & Answers 35 07-30-2007 03:15 PM



All times are GMT -4. The time now is 09:03 AM.