Mainain the files after deleting lines with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Mainain the files after deleting lines with awk
# 1  
Old 03-04-2015
Mainain the files after deleting lines with awk

hello
I want to delete the lines of many files (e.g. file1, file2, ..., file1111) in my directory (e.g. dir1) that they have the 2nd column grater than a value (e.g. 40) placing them to a different directory (e.g. newdir) and keeping the originals. I wrote the code which is functional
Code:
awk '{ if ($20<40) print}' file*.txt > file*.txt

the output of the left part is fine but I cannot achieve to save the output to another directory as files with the same name .
Thank you in advance for your time and help
# 2  
Old 03-05-2015
In your old directory (dir1)

Code:
mkdir ../newdir

for file in *
do
awk '$20<40' $file > ../newdir/$file
done

Note that newdir will be same level as your olddir
This User Gave Thanks to senhia83 For This Post:
# 3  
Old 03-05-2015
Quote:
Originally Posted by senhia83
In your old directory (dir1)

Code:
mkdir ../newdir
 
for file in *
do
awk '$20<40' $file > ../newdir/$file
done

Note that newdir will be same level as your olddir
Hello Senhia83/Phaethon,

Yes script looks to work same as requested, but it is always good practice to mention the complete path in spite of relative path. Otherwise we need to put the script a specific directory only. Following may also help in same.

Code:
for FILE in /my_dir/file*
do
awk '$20<40' $FILE > /tmp/test/$FILE
done

NOTE: I have just taken an example here for paths you can mention the exact path according to your need.


Thanks,
R. Singh

Last edited by RavinderSingh13; 03-05-2015 at 12:46 AM..
This User Gave Thanks to RavinderSingh13 For This Post:
# 4  
Old 03-05-2015
Solved!

thank you both for your time and help! it worked! much appreciated!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Deleting lines based on a condition for a group of files

hi i have a set of similar files. i want to delete lines until certain pattern appears in those files. for a single file the following command can be used but i want to do it for all the files at a time since the number is in thousands. awk '/PATTERN/{i++}i' file (6 Replies)
Discussion started by: anurupa777
6 Replies

2. Shell Programming and Scripting

deleting lines between patterns using sed or awk

hi, Here is excerpt from my xml file <!-- The custom module to do the authentication for LDAP --> </login-module> <login-module code="com.nlayers.seneca.security.LdapLogin" flag="sufficient"> <module-option... (1 Reply)
Discussion started by: sunrexstar
1 Replies

3. Shell Programming and Scripting

awk : deleting specific incorrect lines

Hello friends, I searched in forums for similar threads but what I want is to have a single awk code to perform followings; I have a big log file going like this; ... 7450494 1724465 -47 003A98B710C0 7450492 1724461 -69 003A98B710C0 7450488 1724459 001DA1915B70 trafo_14:3 7450482... (5 Replies)
Discussion started by: enes71
5 Replies

4. Shell Programming and Scripting

Deleting files in awk

Hi All, I have about 10000 files having names of the same pattern I am using awk to match the pattern. Inside the awk block how can i delete all the 10000 files.. Or is there any other way to delete files in bulk... "rm" command fails with the message that argument list is... (9 Replies)
Discussion started by: abhinav192
9 Replies

5. UNIX for Dummies Questions & Answers

deleting lines from a delimited files based on a 2nd file

I need a little help... I have a file with 3 fields, Time/Date, IP address, UniqueID I have a 2nd file of UniqueIDs. I want to either (which ever is easier): 1. delete entries in file 1 that have a UniqueID in file 2 2. create a new file with the fields from File 1, excluding the... (4 Replies)
Discussion started by: goldie363
4 Replies

6. UNIX for Dummies Questions & Answers

deleting specific lines from all files in a directory

I have a directory full of text data files. Unfortunately I need to get rid of the 7th and 8th line from them all so that I can input them into a GIS application. I've used an awk script to do one at a time but due to the sheer number of files I need some kind of loop mechanism to automate... (3 Replies)
Discussion started by: vrms
3 Replies

7. Shell Programming and Scripting

deleting lines from multiple text files

I have a directory full of text data files. Unfortunately I need to get rid of the 7th and 8th line from them all so that I can input them into a GIS application. I've used an awk script to do one at a time but due to the sheer number of files I need some kind of loop mechanism to automate... (3 Replies)
Discussion started by: vrms
3 Replies

8. Shell Programming and Scripting

deleting lines using awk-best way?

Hi all....I'm using awk to validate a csv file, but now I've been told to delete any invalid lines from the file.. Im not sure what the best way to do this is? Would it be to create a temp file say "csv_temp.tmp" file and print all the valid records to that temp file. Then delete the old file... (3 Replies)
Discussion started by: satnamx
3 Replies

9. Shell Programming and Scripting

Deleting Lines from .CSV Files

I have written an script which will excluded some records from .csv file and put it on another excluded file from primary file.This is working very fine.Now the problem is that I want to delete those excluded lines from Primary file but not able to delete it. I have stored the line number in... (1 Reply)
Discussion started by: 009satya
1 Replies

10. Shell Programming and Scripting

deleting a varying amount of lines from a list of files

I did search the posts for info on this and while there were some in the ballpark, none addressed this specifically. (also I tried to post this once it said I was logged out, so hopefully I'm not sending a duplicate here). I have a set of files (250 +/-) where I need to delete the first "$x"... (4 Replies)
Discussion started by: benair
4 Replies
Login or Register to Ask a Question