Print every 5 lines with special condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print every 5 lines with special condition
# 8  
Old 03-06-2013
Indeed it is better to use !(NR%5) rather than ! NR%5
# 9  
Old 03-06-2013
!(NR%5)
This works for me to Smilie
# 10  
Old 03-06-2013
I would suggest a better test file, to make sure the operation is correct. If all the lines start with chr1, it seems hard to tell if a script is really working.
Code:
chr01 100 199
chr02 200 299
chr03 300 399
chr04 400 499
chr05 500 599
chr06 600 699
chr07 700 799
chr08 800 899
chr09 900 939
chr10 940 960

Correct output is:
Code:
chr01 100 599
chr06 600 960

For example, when I ran this improved (I think) test file against the following proposed solution, it did not work correctly:
Code:
awk 'NR%5==1 {a=$2} NR%5==0 {$2=a; print}' infile
chr05 100 599
chr10 600 960

# 11  
Old 03-06-2013
But this is the original input, that you have now changed and its need a new code.
Code:
chr1 100 200
chr1 200 300
chr1 300 400
chr1 400 500
chr1 500 600
chr1 600 700
chr1 700 800
chr1 800 900
chr1 900 920
chr1 940 960

---------- Post updated at 08:49 ---------- Previous update was at 08:39 ----------


This should then work
Code:
awk 'NR%5==1 {printf "%s %s ",$1,$2;getline;getline;getline;getline;print $3}'
chr01 100 600
chr06 600 960


Last edited by Jotne; 03-06-2013 at 05:24 AM..
# 12  
Old 03-06-2013
The OP said "first line's second column and the fifth line's 3rd column as one single line. This should happen for every 5 lines."

I just changed the test input to make sure that condition is being met.

Of course, if the first column doesn't matter, then the original input is OK. The OP didn't say.
# 13  
Old 03-06-2013
All above reflect this. Data from first line second column and fifth line 3rd column. Since data on column #1 was not important, you get what you get.

That's why its always important to post real data or as close to as possible.
# 14  
Old 03-06-2013
Can't argue with that. You're right. OP didn't say anything special about first column. I was trying to "read his mind". It would seem kind of silly if first column ALWAYS had same data. But silly data files happen all the time.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

If condition fails for special charecter

I have a sample server name listed in variable as below: var="server-13" I need to check if the 7th character on $var is number 1 whichenv=`echo "$var"| head -c 7 | tail -c 1` if ]; then echo "9 found" else echo "9 Not Found" fi Output: This works... (3 Replies)
Discussion started by: mohtashims
3 Replies

2. UNIX for Beginners Questions & Answers

How To Retreive Files With a Special Condition?

Everyday I have to get a list of files in a directory with a special condition and feed this list to a for loop to be processed. Since I do not use Unix all the time, it is tricky for me to get that list of files. So, the question is whether there are commands that will give me the file names... (12 Replies)
Discussion started by: april
12 Replies

3. Shell Programming and Scripting

Print lines based on line number and specified condition

Hi, I have a file like below. 1,2,3,4,5,6,7,8,9I would like to print or copied to a file based of line count in perl If I gave a condition 1 to 3 then it should iterate over above file and print 1 to 3 and then again 1 to 3 etc. output should be 1,2,3 4,5,6 7,8,9 (10 Replies)
Discussion started by: Anjan1
10 Replies

4. Shell Programming and Scripting

Print certain lines based on condition

Hi All, I have following listing Filesystem GB blocks Free Used Iused Iused Mounted on /dev/hd2 4.00 0.31 93 63080 43 /usr Filesystem GB blocks Free Used Iused Iused Mounted on Filesystem GB blocks Free Used Iused Iused... (11 Replies)
Discussion started by: ckwan
11 Replies

5. Shell Programming and Scripting

If condition matching with special chars

Hi, I have file #cat drivers.txt fcs0 fcs1 vscsi1 vscsi2 In this i need to check the availabality of "fcs" or "vscsi" alone not vscsi0,fcs1 I tried with "if condition" but it is not working. cat drivers.txt| while read ADAP do echo "Checking for $ADAP" if ;then echo "FC... (9 Replies)
Discussion started by: ksgnathan
9 Replies

6. Shell Programming and Scripting

awk to print lines based on string match on another line and condition

Hi folks, I have a text file that I need to parse, and I cant figure it out. The source is a report breaking down softwares from various companies with some basic info about them (see source snippet below). Ultimately what I want is an excel sheet with only Adobe and Microsoft software name and... (5 Replies)
Discussion started by: rowie718
5 Replies

7. Shell Programming and Scripting

How to print range of lines using sed when pattern has special character "["

Hi, My input has much more lines, but few of them are below pin(IDF) { direction : input; drc_pinsigtype : signal; pin(SELDIV6) { direction : input; drc_pinsigtype : ... (3 Replies)
Discussion started by: nehashine
3 Replies

8. UNIX for Dummies Questions & Answers

Strings with Special chars in IF condition

I was trying to run a code to check if a fax number is empty or not. for that, I've written the following code which is throwing an error. #!/bin/ksh fax= "999-999-9999" if ; then fax_no="000-000-0000" else fax_no=$fax fi echo $fax_no And I get the... (7 Replies)
Discussion started by: hooaamai
7 Replies

9. Shell Programming and Scripting

gawk print some special lines

Hi every body, i have this file example : TD1 TD2 TD3 . . .TDn <DIE_1> xxxxxx <\DIE_1> <TD1> information 1 inormation n <\TD1> <TDq> information (0 Replies)
Discussion started by: kamel.kimo
0 Replies

10. Shell Programming and Scripting

How to filer a line in special condition?

Hi, I hava log file which has the following output : Created RELEASE in dist/filename_release_1_0_20090210.zip Created RELEASE in dist/filename_release_1_1_20090210.zip Created RELEASE in dist/filename_release_1_3_20090210.zip Created RELEASE in... (6 Replies)
Discussion started by: bhaskar_m
6 Replies
Login or Register to Ask a Question