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
# 8  
Old 06-15-2017
Quote:
Originally Posted by rohit_shinez
Hi Rudic,

Thanks for your inputs, can this be modified in my code which runs in bash

Code:
something like this

sed '1d' $file > $file

The tail command in RudiC's suggestion copies your source files to their destinations removing "$HDCNT" lines of headers during the copy.

Your code above will do two things:
  • The shell will see the redirection and truncate the file named by the expansion of $file to size 0, and then
  • The sed command will delete the 1st line (of the empty file) named by the expansion of $file and copy the remaining (non-existent) lines to the output.
Do you really want to change your input files before you copy their contents to their destinations, or do you just need to remove the header lines from the copied files? If you don't need to modify the source files:
Code:
sed 1d "$file" > /output/aus/"$file"

will do what you want. If you need to change the source files too:
Code:
sed 1d "$file" > "$file.$$" && cp "$file.$$" "$file" && rm "$file.$$"

is a safe way to do what you want without breaking links to your source file (if any existed).
# 9  
Old 06-15-2017
Quote:
Originally Posted by Don Cragun
The tail command in RudiC's suggestion copies your source files to their destinations removing "$HDCNT" lines of headers during the copy.

Your code above will do two things:
  • The shell will see the redirection and truncate the file named by the expansion of $file to size 0, and then
  • The sed command will delete the 1st line (of the empty file) named by the expansion of $file and copy the remaining (non-existent) lines to the output.
Do you really want to change your input files before you copy their contents to their destinations, or do you just need to remove the header lines from the copied files? If you don't need to modify the source files:
Code:
sed 1d "$file" > /output/aus/"$file"

will do what you want. If you need to change the source files too:
Code:
sed 1d "$file" > "$file.$$" && cp "$file.$$" "$file" && rm "$file.$$"

is a safe way to do what you want without breaking links to your source file (if any existed).
Hi don,

Before I copy I need to change the source files which ends with _01.txt I.e. To remove the header
# 10  
Old 06-15-2017
Quote:
Originally Posted by rohit_shinez
Hi don,

Before I copy I need to change the source files which ends with _01.txt I.e. To remove the header
Fine. And, since you copy the modified source file to the destination (not move it), when you run your script again a few seconds (or an hour, or day, or week, or month, or year) later, it will update that same file again (this time removing a line of data instead of the header). And then it will copy the updated file again to its destination discarding a line of data there as well. And, after you have run your script enough times, your source and destination files will both be empty.

This doesn't seem like a logical way to handle things to me, but you can do it that way if you want to.
# 11  
Old 06-16-2017
Hi don,

Once i have copied i wil remove the files or i will move the files from filepath to temp path and remove the header from that temp path
# 12  
Old 06-16-2017
Quote:
Originally Posted by rohit_shinez
Hi Guys,

I have below directory where there are certain files. Something like below
Code:
country_dir

aus_01.txt
nz_01.txt
aus_02.txt
bd.txt

property.txt
aus
nz
bd

I need to remove the header of the file which ends with _01.txt while copying from country directory to another directory

Code which i am using

Code:
for i in `cat property.txt`
do
file=$(ls -t1 $filepath|  grep -i $file)

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

fi
....
....
done

in the above code i need to remove the header for the files aus_01.txt
nz_01.txt before copying to output directory. Can the changes be done in same code
Hi,

I'm quite new to scripting too.

just curious if the current code you are using is already working and you wanted to enhanced the code to omit the header in individual file_0X.txt?

may i also check if the header string in each individual *.txt is unique or different?

if its unique, maybe this might work?

Code:
for i in `cat property.txt | grep -v <HEADER>`
do
file=$(ls -t1 $filepath|  grep -i $file)

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

fi
....
....
done

just a thought
# 13  
Old 06-16-2017
Thanks
But yours wont work because there is no header like unique and in property file there is no header string like that i need to remove the header from source file before copying
# 14  
Old 06-16-2017
Quote:
Originally Posted by rohit_shinez
Hi don,

Once i have copied i wil remove the files or i will move the files from filepath to temp path and remove the header from that temp path
Huh? You have to remove the header before you copy the source file and then you will copy the source file and then you will copy the source file (to a country directory) and then you will remove the source file OR you will remove the header before you copy the source file and then you will copy the source file (to a country directory) and then you will move the source file to a temporary file an thend you will remove the header from the temporary file again and then you will remove the temporary file. If either of these multistep processes are interrupted and you restart the process, zero of more lines of data will be lost.

Either of those sound like a lot of extra work when it sounds like you get the same results with MUCH less processing if you just copy the source file except for the header to a country directory and then remove the source file. If this processing is interrupted and restarted, no data will be lost.

Why is it that you want to make this so complicated? Why do you need to remove the header BEFORE copying the file? Why isn't removing the header WHILE you are copying the file sufficient?
This User Gave Thanks to Don Cragun For This Post:
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