Computing.Net > Forums > Unix > script to remove some lines in all

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

script to remove some lines in all

Reply to Message Icon

Name: vmtrt
Date: April 27, 2004 at 20:41:00 Pacific
OS: unix
CPU/Ram: pentium 4, 256MB ram
Comment:

Hi everyone,

I am trying to remove some lines from all the files in a directory, for example I am looking at the fields 25-27 and if it is 19 I am trying to remove the whole line from that file, similarly from all the other files in that directory. The files are named as abcxxxxxxD.txt and abcxxxxxxDM.txt. I tried to use grep -v to find the needed lines where it is not matched and tried to write in a temp file and replace the original file with the temporary file, but this is taking long time as there are 6500 files in the directory. Is there any simpler way to do it.
And also I am unable to run on all the files, I can only run the script on one file at a time, is there a way to run the script on all the files in the directory.

Thank you

lp




Sponsored Link
Ads by Google

Response Number 1
Name: Wolfbone
Date: April 28, 2004 at 03:41:28 Pacific
Reply:

You can easily run a command on all the files you want by piping 'ls' into 'xargs command' or by using the 'for name in $(/bin/ls) ; do command $name ; done' construct at the commandline.

The command:

awk '{ if (substr($0,25,3) != "019") print $0 > ARGV[1]}'

might do the example job for you but your "...fields 25-27...if it is 19..." is ambiguous: Do you mean fields 25-27 contain a 3 digit +ive integer?


0

Response Number 2
Name: vmtrt
Date: April 28, 2004 at 06:46:41 Pacific
Reply:

Thank you wolfbone,

my fields are 25-26 two digit number, I will try it out.


0

Response Number 3
Name: vmtrt
Date: April 28, 2004 at 08:02:57 Pacific
Reply:

Hi,
I have used it in the following way.

Src=/MonthlyData
Tgt=/MarketData

ls $Tgt | xargs -i awk '{ if (substr($0,7,2) != "14") print $0 > ARGV[1]}'

I got the following error,

awk: A print or getline function must have a file name
The input line number is 1.
The source line number is 1.

Any help is really appreciated. Thank you


0

Response Number 4
Name: Wolfbone
Date: April 28, 2004 at 09:06:50 Pacific
Reply:

I don't know why that particular error came up but on my system, if I use the -i option to xargs like that, nothing at all happens.

Try without the -i option. Also 'ls $Tgt' may not be printing /MarketData/file_name but just file_name. Make sure ls is printing out the correct absolute or relative paths to each file (and nothing else).

Note that the command as it stands will modify the files immediately so I also suggest you test it out on a copy of a subset of the files first and perhaps remove the '> ARGV[1]' bit to make the output go to stdout until you're happy with the results.

Finally - what is the Src=/MonthlyData for?


0

Response Number 5
Name: vmtrt
Date: April 28, 2004 at 09:55:48 Pacific
Reply:

There are two sets of files (*D.txt and *DM.txt that have the value to be checked at 25-26 and 7-8 fields respectively) in the folder so I separated the files into MonthlyData and MarketData folders, so that i can work on one set at a time.

I made sure the ls is giving the right set of the file list.

The script is running, but unable to get the desirable result, the records have 19 in 7-8 position are not getting eliminated. And I tried the script on just five files and the script is not coming out of the loop.

This is what i used.
xargs awk '{if(substr($0,7,2)!="14") print $0}'

Thanks for your help


0

Related Posts

See More



Response Number 6
Name: Wolfbone
Date: April 28, 2004 at 10:37:28 Pacific
Reply:

Well it can't be the awk bit because it's too simple but at least try it out on a single file like so:

awk '{if(substr($0,7,2)!="14") print $0}' datafile

If that doesn't work I can't explain it but it certainly looks to me like xargs is behaving differently on your system so I'd use the shell construct instead.


0

Response Number 7
Name: vmtrt
Date: April 28, 2004 at 11:56:00 Pacific
Reply:

Thanks for all your help.

I tried to run it on one file but the rows that satisfy the condition are not deleted.

Any suggestion or how to use the shell is greatly appreciated.

Thank you again


0

Response Number 8
Name: vmtrt
Date: April 28, 2004 at 12:51:53 Pacific
Reply:

Hi,

I tried the following shell,

for name in $(ls)
do
awk '{if(substr($0,7,2)!="19") print $0}' $name
done

I can see the desirable result on the screen, but when i open up the file, the rows are still there, they are not deleted.

Please let me know where i am going wrong.

Thank you very much in advance.


0

Response Number 9
Name: Wolfbone
Date: April 28, 2004 at 13:35:08 Pacific
Reply:

Sorry - I think maybe I missed a step in the explanation of what the command is doing.

In it's original form the command will read in the lines from the input file and print them back out, skipping the unwanted ones and printing straight back into the input file.

Then we modified it by removing the '> [ARGV[1]' bit so it would print the lines out to the screen instead - leaving the input file untouched.

So if you are happy with the screen output and you want to make the changes to the real data files (or the test copies), you need to put the redirection; '> [ARGV[1]' back into the command.


0

Response Number 10
Name: Wolfbone
Date: April 28, 2004 at 13:50:49 Pacific
Reply:

That should be '> ARGV[1]' not '> [ARGV[1]' of course.


0

Response Number 11
Name: vmtrt
Date: April 28, 2004 at 20:33:35 Pacific
Reply:

When i use the .....print $0 >ARGV[1].. the script is going into some kind of the loop and is not exiting out.

Any help is greatly appreciated, thank you


0

Response Number 12
Name: Wolfbone
Date: April 29, 2004 at 01:12:15 Pacific
Reply:

Looks like another difference between our systems. You'll have to do it by making an intermediate file:

for name in $(ls)
do
awk '{if(...) print $0}' $name > work_file
mv -f work_file $name
done

or something similar.



0

Response Number 13
Name: vmtrt
Date: April 30, 2004 at 05:20:12 Pacific
Reply:

Thank you that works....

It takes a long time, couple of hours. Is there any other way so that the time taken is reduced.

Thanks for your time wolfbone.


0

Response Number 14
Name: Wolfbone
Date: April 30, 2004 at 06:33:03 Pacific
Reply:

Unfortunately the whole point of the redirection that we had to remove was to save time. There are other possibilities - double opening the file in the shell, for example but that may not work on your system either. Is it something you need to do often?


0

Response Number 15
Name: vmtrt
Date: April 30, 2004 at 12:33:59 Pacific
Reply:

No once every month....


0

Response Number 16
Name: Wolfbone
Date: May 1, 2004 at 01:00:20 Pacific
Reply:

You could try "sed -i '/.\{6\}19/d' $filename" or similar next month. It might be faster on your system but on mine it creates a temporary file. A c programme would be the fastest way.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Awk from two files into o... apache error



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: script to remove some lines in all

Script to Remove lines from a file www.computing.net/answers/unix/script-to-remove-lines-from-a-file/5268.html

how to replace a line in a file www.computing.net/answers/unix/how-to-replace-a-line-in-a-file/7214.html

Removing a lines in a file www.computing.net/answers/unix/removing-a-lines-in-a-file/7453.html