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
# 1  
Old 06-15-2017
To check the file and remove header before copying

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
# 2  
Old 06-15-2017
Not sure I fully understand. You want to copy all files whose names contain a string from the property.txt file, and remove the header line(s) from those who end in _01.txt?

Your sample code is not of great help to deduct in detail what you're after. I'm afraid it won't copy anything as $file's doesn't seem to be predictable because a) $filepath is undefined and thus empty b) the ls result to be assigned to file is grepped for $file itself, mayhap shooting yourself in the foot. $i, the variable looping through property.txt, isn't used for copying, just for testing for a filename existence. And, no provision is made to handle _01.txt files differently.

How many lines are the headers to be suppressed?
# 3  
Old 06-15-2017
Hi Rudic,

To answer your below question

You want to copy all files whose names contain a string from the property.txt file, and remove the header line(s) from those who end in _01.txt? - Yes i need this and wanted to apply the same in my code

Code:
filepath=/country/

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

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

# 4  
Old 06-15-2017
If your shell (which you failed to mention) is a recent bash, try (yes, admittedly deploying the unloved and sometimes dangerous eval)
Code:
X=$(<property.txt)
for PN in $(eval echo $filepath/*{${X//$'\n'/,}}*)
   do   FN=${PN##*/}
        DIR=${FN%%[_.]*}
        [ $FN = ${FN/_01.txt} ] && HDCNT=0 || HDCNT=2
        [ -f $FN ] && tail -n+$HDCNT $FN > /output/$DIR/$FN || echo "No $FN file"
   done

and comment on the result
This User Gave Thanks to RudiC For This Post:
# 5  
Old 06-15-2017
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

# 6  
Old 06-15-2017
Why that sed? What don't you like in the proposal, which should run unmodified in (a recent) bash?
# 7  
Old 06-15-2017
Quote:
Originally Posted by RudiC
Why that sed? What don't you like in the proposal, which should run unmodified in (a recent) bash?
The thing is I wanted to modify in my existing code itself where sed '1d' will remove the header file or similar to any other approach. Moreover I am not able to see copying commmand in your 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