LinuxQuestions.org
Visit Jeremy's Blog.
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 03-18-2014, 11:41 AM   #1
tabbygirl1990
Member
 
Registered: Jul 2013
Location: a warm beach, cool ocean breeze, nice waves, and a Margaritta
Distribution: RHEL 5.5 Tikanga
Posts: 63

Rep: Reputation: 1
need to mod an awk script for multiple occurences


hi guys,

i have this very simple awk script

Code:
awk 'NR==1{print $1}{print $2}' RS="" OFS="," filename.txt > out.txt
that prints a header line and the first like of data. i want to modify it to repeat the print statement every 413 lines after the header line. so i could write:

Code:
awk 'NR==1{print $1}{print $2}{print $415}{print $828}{print $1241}' RS="" OFS="," filename.txt > out.txt
but that only gives me back the header and the first four occurances in the file, my file my have 4000 occurances, idk

so i need to have a loop that increments the print statement over and over adding 413 each time.

is this easy to do, or not so much?

thnks much,

tabby
 
Old 03-18-2014, 11:52 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
You may try a loop like this:
Code:
awk 'NR==1{print $1}{for (i = 2; i <= NF; i = i+413) print $i}' RS="" OFS="," filename.txt > out.txt
The third field in the loop syntax is the increment rule and it can be any expression that changes the value of the loop variable, not necessarily the increment by one i++ as mentioned in most of the examples of a for loop.
 
1 members found this post helpful.
Old 03-18-2014, 11:57 AM   #3
tabbygirl1990
Member
 
Registered: Jul 2013
Location: a warm beach, cool ocean breeze, nice waves, and a Margaritta
Distribution: RHEL 5.5 Tikanga
Posts: 63

Original Poster
Rep: Reputation: 1
awesome awesome awesome!

i'll give it a try...

 
Old 03-18-2014, 12:00 PM   #4
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 (pre-systemd)
Posts: 2,798

Rep: Reputation: 749Reputation: 749Reputation: 749Reputation: 749Reputation: 749Reputation: 749Reputation: 749
How about:

Code:
awk 'NR%413==1{print $1}' RS="" OFS="," filename.txt > out.txt
 
  


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] reporting with AWK script ?? pattern search multiple em31amit Programming 4 06-17-2012 04:55 AM
[SOLVED] Using BASH to Rename files with last mod date using stat & awk Liquid_Squelch Programming 4 12-11-2011 06:27 PM
perl matching multiple occurences on one line ShaqDiesel Programming 2 10-11-2009 07:13 AM
AWK/SED Multiple pattern matching over multiple lines issue GigerMalmensteen Programming 15 12-03-2006 05:08 PM
Multiple Occurences of a Program / SSL / Ensim yorkshiresteve Linux - Software 0 07-27-2003 04:07 PM

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

All times are GMT -5. The time now is 07:30 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