LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-19-2012, 12:19 PM   #1
sieukid
LQ Newbie
 
Registered: Mar 2012
Posts: 2

Rep: Reputation: Disabled
[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 !
 
Old 03-19-2012, 12:59 PM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,478

Rep: Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888
I am not sure I understand the issue?? Why not use the same command?
 
Old 03-19-2012, 01:10 PM   #3
sieukid
LQ Newbie
 
Registered: Mar 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
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.
 
Old 03-19-2012, 01:16 PM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,478

Rep: Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888
So I am still not understanding? Put as many commands in the script as you like.
 
Old 03-19-2012, 01:27 PM   #5
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 623

Rep: Reputation: 364Reputation: 364Reputation: 364Reputation: 364
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 ...
 
Old 03-20-2012, 01:23 PM   #6
cristalp
Member
 
Registered: Aug 2011
Distribution: Linux Mint
Posts: 103

Rep: Reputation: Disabled
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.

Last edited by cristalp; 03-20-2012 at 01:26 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] awk script to filter and delete crxlurp Linux - Newbie 6 07-05-2011 01:33 PM
sed/awk: Three consecutive blank lines in a file, how to delete two of them? recomboDNA Programming 8 06-17-2010 09:50 AM
delete multiple lines from file using shell script mech123 Linux - Newbie 4 06-09-2010 04:04 AM
need help with awk-script (compare two lines) Mauline Programming 2 11-27-2008 04:12 AM
Need a bash shell script which will delete lines from file scjohnie Linux - Newbie 1 09-13-2008 08:51 PM


All times are GMT -5. The time now is 12:20 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration