To check the file and remove header before copying


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To check the file and remove header before copying
# 15  
Old 06-17-2017
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

# 16  
Old 06-17-2017
Quote:
Originally Posted by rohit_shinez
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find header in a text file and prepend it to all lines until another header is found

I've been struggling with this one for quite a while and cannot seem to find a solution for this find/replace scenario. Perhaps I'm getting rusty. I have a file that contains a number of metrics (exactly 3 fields per line) from a few appliances that are collected in parallel. To identify the... (3 Replies)
Discussion started by: verdepollo
3 Replies

2. UNIX for Dummies Questions & Answers

Check for empty line at end of a dynamic header in each file

Hi Folks, I have a requirement to develop a shell script. PFB my requirement, Requirement: I need to check an empty line after the end of each header in respective file and if a empty line is present simply echo file OK and if empty line is not present echo "Adding empty line" and add an... (6 Replies)
Discussion started by: tpk
6 Replies

3. UNIX for Advanced & Expert Users

How to check or remove IP address from a hashed known_hosts file?which

Hi, In my server, the hostname and IP addresses are added to the known_hosts which then be hashed by ssh-keygen -H -f. Is it possible to check if an IP address has already been added to the hashed known_hosts to avoid duplications (I think there will be problems if there are duplicated IP... (3 Replies)
Discussion started by: hce
3 Replies

4. Shell Programming and Scripting

Remove the file content based on the Header of the file

Hi All, I want to remove the content based on the header information . Please find the example below. File1.txt Name|Last|First|Location|DepId|Depname|DepLoc naga|rr|tion|hyd|1|wer|opr Nava|ra|tin|gen|2|wera|opra I have to search for the DepId and remove the data from the... (5 Replies)
Discussion started by: i150371485
5 Replies

5. Shell Programming and Scripting

Remove last few characters in a file but keeping Header and trailer intact

Hi All, I am trying write a simple command using AWK and SED to this but without any success. Here is what I am using: head -1 test1.txt>test2.txt|sed '1d;$d' test1.txt|awk '{print substr($0,0,(length($0)-2))}' >>test2.txt|tail -1 test1.txt>>test2.txt Input: Header 1234567 abcdefgh... (2 Replies)
Discussion started by: nvuradi
2 Replies

6. Shell Programming and Scripting

Copying the Header & footer Information to the Outfile.

Hi I am writing a perl script which checks for the specific column values from a file and writes to the OUT file. So the feed file has a header information and footer information. I header information isaround107 lines i.e. Starts with START-OF-FILE ....... so on .... ... (11 Replies)
Discussion started by: filter
11 Replies

7. UNIX for Dummies Questions & Answers

Check header of file, without opening the file

if we happen to have a very big .csv file we cannot or dont care to edit, is there a way to check if the file contains a header row? (2 Replies)
Discussion started by: lydiaflamp
2 Replies

8. Shell Programming and Scripting

Check file size and remove files

Hi, Here we have a situation where we have to check the file size and if the file size is greater than 0 bytes then remove the files from the directory. 1)EdwTrxn 2)EdwPost 3)EdwTndr 4)EdwSls 5)EdwSlsRej 6)EdwTndrRej Files will be created in the directory in the following manner. ... (5 Replies)
Discussion started by: srivsn
5 Replies

9. UNIX for Dummies Questions & Answers

Copy all the files with time stamp and remove header,trailer from file

All, I am new to unix and i have the following requirement. I have file(s) landing into input directory with timestamp, first i want to copy all these files into seperate directory then i want to rename these files without timestamp and also remove header,trailer from that file.. Could... (35 Replies)
Discussion started by: ksrams
35 Replies

10. Shell Programming and Scripting

Remove header(first line) and trailer(last line) in ANY given file

Hi, I need some help in removing the header (first line) and the trailer (last line) in a give file... The data file actually comes in EBCDIC format and I converted it into ASCII.. Now I need to strip off the first line and the last line.. I think we can use sed to do something like this:... (2 Replies)
Discussion started by: madhunk
2 Replies
Login or Register to Ask a Question