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
# 1  
Old 03-05-2013
Print every 5 lines with special condition

Hi Friends,

I have an input file like this

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

I would like to get the first line's second column and the fifth line's 3rd column as one single line. This should happen for every 5 lines.

So, the output will be

Code:
chr1 100 600
chr1 600 960

Thanks
# 2  
Old 03-05-2013
Code:
awk ' NR == 1 {
                printf "%s %d ", $1, $2
                next
} NR%5 == 0 {
                printf "%d\n", $3
                getline
                printf "%s %d ", $1, $2
} END {
                printf "\n"
} ' file

This User Gave Thanks to Yoda For This Post:
# 3  
Old 03-05-2013
A little simpler is
Code:
awk '{ x=NR%5 }
x==1 { printf "%s %d",$1,$2 }
x==0 { printf " %d\n",$3 }
' file

These 2 Users Gave Thanks to MadeInGermany For This Post:
# 4  
Old 03-05-2013
also:
Code:
awk 'NR%5==1 {a=$2} ! NR%5 {$2=a; print}' infile

This User Gave Thanks to rdrtx1 For This Post:
# 5  
Old 03-05-2013
sed:
Code:
sed 'N;N;N;N;s/[^ ]*\n.* //' file

# 6  
Old 03-05-2013
A Perlish way:
Code:
perl -lane 'if(my $pos = ($.%5 ... $.%5==0)) {
 $start = "$F[0] $F[1]" if $pos==1;
 print "$start $F[2]" if $pos =~ /E0$/; 
}' file

# 7  
Old 03-06-2013
This did not work for me, gives no output
Code:
awk 'NR%5==1 {a=$2} ! NR%5 {$2=a; print}' infile

This does work
Code:
awk 'NR%5==1 {a=$2} NR%5==0 {$2=a; print}' infile
chr1 100 600
chr1 600 960

GNU Awk 3.1.8
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