LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   [awk script] Help me delete lines in a file using script ? (https://www.linuxquestions.org/questions/programming-9/%5Bawk-script%5D-help-me-delete-lines-in-a-file-using-script-935317/)

sieukid 03-19-2012 01:19 PM

[awk script] Help me delete lines in a file using script ?
 
Dear everyone.

I just start to learn awk script, I meet a problem.
I have a data as:
Quote:

1 3 5 7 8 10
4 5 6 7 8
0 7 8
1 2
6 3 8 3
4 6 2 8 0 4 8 10
7 4 8 2 9
3 5 8 2 2 0
9 3 5 2 1
10 9 4 5 7 2
I would like to delete some lines which (length < 10 char), if in terminal I can use the command:
Quote:

$ awk 'length >= 10' $file
==> result as:
1 3 5 7 8 10
4 6 2 8 0 4 8 10
3 5 8 2 2 0
10 9 4 5 7 2
But, I am learning awk script, and I would like to delete lines using script as:

Quote:

#!/usr/bin/awk -f
.....# command here #
Would you please help me which command i can use !

Thank you very much !

grail 03-19-2012 01:59 PM

I am not sure I understand the issue?? Why not use the same command?

sieukid 03-19-2012 02:10 PM

Quote:

Originally Posted by grail (Post 4630756)
I am not sure I understand the issue?? Why not use the same command?

Because I work with many files like that, assume that I receive them every week; beside the problem above, there are some others commands that I would like to put all in a script. so everytime i receive them, i just need to run a script.

grail 03-19-2012 02:16 PM

So I am still not understanding? Put as many commands in the script as you like.

firstfire 03-19-2012 02:27 PM

Hi.

Your script may look like this:
Code:

#!/usr/bin/awk -f
length >= 10

Put this text to a file, say 'filter.awk' and run
Code:

$ chmod +x filter.awk
to make it executable. Then run as
Code:

$ ./filter.awk file1 ...

cristalp 03-20-2012 02:23 PM

Hi my friend,

You can also just open any editer and type
Code:

awk 'length >= 10' $*
Then save it to a file.
Then chmod +x filename and run it as ./filename inputfile

Or, if you have many input files as you said, you may need a shell script with awk command in it. Suppose you use bash, you can do something like
Code:

#!/bin/bash
for file in $@
do
awk 'length >= 10' $file
done

You can also put all your other commands in this file and then run it like above. Try to google shell script for more help.


All times are GMT -5. The time now is 04:02 AM.