LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-06-2015, 04:31 AM   #1
shazo
LQ Newbie
 
Registered: Dec 2014
Posts: 12

Rep: Reputation: Disabled
how i can print a specific range of nubers form a file.


hello,

i am trying to make a table from some files. i used this to record how much "RD_" field i have in my file.
Quote:
grep -o 'RD_' $f|grep -c 'RD_'
forexample i got 5 "RD_" fields now i want to print 5 number of fields from another file starting from 2nd field. i did it mannully like
Quote:
awk 'NR==1{print"{"$2","$3","$4","$5","$6","0.0000",""0.0000""}"","}' $file
i want to make it work together and a bit auto matic like
PHP Code:
awk 'NR==1{print"{"$2"to "$5"," apend zeros to make it total 7 fields"}"","}' $file 
your coments would be apreciated
thanks alot
 
Old 02-06-2015, 10:04 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,516

Rep: Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893
I think the easy answer would be create a loop from 2 to N, where N is your number passed to the script
 
Old 02-09-2015, 03:27 AM   #3
shazo
LQ Newbie
 
Registered: Dec 2014
Posts: 12

Original Poster
Rep: Reputation: Disabled
thanks i will try this!
 
Old 02-09-2015, 06:57 AM   #4
shazo
LQ Newbie
 
Registered: Dec 2014
Posts: 12

Original Poster
Rep: Reputation: Disabled
i tried this
Quote:
a=`grep -o 'RD_' $f|grep -c 'RD_'`

awk 'NR==1{s=0;for(i=2;i<'$a';i++) s=$i;print"{"s ",""0.0000000e+00""}"","}' $f
with this i can print only the last number that is in the position less than $a. what change should i make to print all numbers from position 2 untill postion '$a'.
your coments are highly apreciated.
 
Old 02-09-2015, 10:20 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,516

Rep: Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893
Firstly, do not play with quotes as it only leads to frustration. Use -v options to pass your bash variable to your awk script.

As for your 'for' loop, if you want it to encompass more than a single command/test/argument, place everything in curly braces:
Code:
for(i=2;i<'$a';i++){<your stuff here>}
I would recommend reading through the manual site as it is very informative and generally easy to get around and find things you need:

http://www.gnu.org/software/gawk/man...ode/index.html
 
1 members found this post helpful.
  


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] using Perl's range operation to print from one line to the end of file captainentropy Programming 2 08-25-2013 02:48 PM
SED append to a specific range of lines of a file Gener@l Programming 6 04-24-2013 06:56 PM
print lines form nth line to mth line which fulfill specific condition cristalp Programming 4 11-07-2011 07:39 AM
[SOLVED] sed print range to end of file schneidz Programming 4 04-08-2009 01:30 PM
command or shell script to print line range from file minil Programming 3 12-28-2005 08:05 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:28 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration