Visit Our UNIX and Linux User Community


Print lines based on line number and specified condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print lines based on line number and specified condition
# 1  
Old 11-06-2014
Print lines based on line number and specified condition

Hi,

I have a file like below.

Code:
1,2,3,4,5,6,7,8,9

I 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

Code:
1,2,3
4,5,6
7,8,9


Last edited by jim mcnamara; 11-06-2014 at 08:06 AM..
# 2  
Old 11-06-2014
Hello Anjan,

Following may help you in same.

Code:
awk -F, '{for(i=1;i<=NF;i++){{S=S?S OFS $i:$i} if(i%3==0){print S;S=""}}}' OFS=, Input_file

Output will be as follows.
Code:
1,2,3
4,5,6
7,8,9

EDIT: I am sorry Jim, seems we have posted on same time, if possible kindly delete my post, as you have OP a question.

Thanks,
R. Singh

Last edited by RavinderSingh13; 11-06-2014 at 08:08 AM.. Reason: Adding apology comment to Jim
# 3  
Old 11-06-2014
What have you tried so far?
# 4  
Old 11-06-2014
I have tried it in perl.

Code:
#!/usr/bin/perl

use warnings;
use strict;

my @result;

my $filename = "new_num.txt";

my $i;
unlink $filename;

#START:
while(<>) {

$_ =~ s/,/\n/g;

if (($. == 1) .. ($. == 3)) {

#$_ =~ s/^ //g;

push (@result,$_);


print "@result";

foreach $i (@result) {

$i =~ s/\n/,/g;

open FILE,">>$filename" or die "Cannot read the file $filename: $!\n";

print FILE "$i";

#print FILE "\n";

@result = ();


#goto START;

}

ravinder singh can you please explain me your awk code.

Last edited by Anjan1; 11-06-2014 at 08:37 AM.. Reason: code change
# 5  
Old 11-06-2014
Code:
input=( {a..z} )
C=0
count=0
limit=3
max=${#input[@]}

while [[ $C -lt $max ]];do
	while [[ $count -lt $limit ]];do
		out+=" ${input[$C]}"
		C=$((C+1))
		count=$((count+1))
	done
	echo "$out" ; out=""
	count=0
done

Code:
sh ~/file1.txt 
 a b c
 d e f
 g h i
 j k l
 m n o
 p q r
 s t u
 v w x
 y z

Hope this helps
# 6  
Old 12-30-2014
its not printing entire contents in a file. If a file has 1,2,3,4,5,6,7.

When I use awk code given by ravindersingh its giving output as

Code:
awk -F, '{for(i=1;i<=NF;i++){{S=S?S OFS $i:$i} if(i%3==0){print S;S=""}}}' OFS=, Input_file

Code:
1,2,3
4,5,6

not printing 7
# 7  
Old 12-30-2014
Hello Anjan1,

Kindly try following and let me know if this helps.
Code:
xargs -d"," -n3 Input_file

Output will be as follows.
Code:
1 2 3
4 5 6
7

Thanks,
R. Singh

Previous Thread | Next Thread
Test Your Knowledge in Computers #299
Difficulty: Easy
Novell bought the SUSE (then SuSE) brands and trademarks in 2003.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Reading a file line by line and print required lines based on pattern

Hi All, i want to write a shell script read below file line by line and want to exclude the lines which contains empty value for MOUNTPOINT field. i am using centos 7 Operating system. want to read below file. # cat /tmp/d5 NAME="/dev/sda" TYPE="disk" SIZE="60G" OWNER="root"... (4 Replies)
Discussion started by: balu1234
4 Replies

2. 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

3. Shell Programming and Scripting

Sed print range of lines between line number and pattern

Hi, I have a file as below This is the line one This is the line two <\XMLTAG> This is the line three This is the line four <\XMLTAG> Output of the SED command need to be as below. This is the line one This is the line two <\XMLTAG> Please do the need to needful to... (4 Replies)
Discussion started by: RMN
4 Replies

4. Shell Programming and Scripting

print lines between line number

Hi, Anyone help me to print the lines from the flat file between 879th line number and 1424th line number. The 879 and 1424 should be passed as input to the shell script(It should be dynamic). Can any one give me using sed or awk? I tried using read, and print the lines..Its taking too... (3 Replies)
Discussion started by: senthil_is
3 Replies

5. Shell Programming and Scripting

join based on line number when one file is missing lines

I have a file that contains 87 lines, each with a set of coordinates (x & y). This file looks like: 1 200.3 -0.3 2 201.7 -0.32 ... 87 200.2 -0.314 I have another file which contains data that was taken at certain of these 87 positions. i.e.: 37 125 42 175 86 142 where the first... (1 Reply)
Discussion started by: jackiev
1 Replies

6. Shell Programming and Scripting

Merge two non-consecutive lines based on line number or string

This is a variation of an earlier post found here: unixcom/shell-programming-scripting/159821-merge-two-non-consecutive-lines.html User Bartus11 was kind enough to solve that example. Previously, I needed help combining two lines that are non-consecutive in a file. Now I need to do the... (7 Replies)
Discussion started by: munkee
7 Replies

7. Shell Programming and Scripting

Delete lines based on line number

I have a file with ~200K lines, I need to delete 4K lines in it. There is no range. I do have the line numbers of the lines which I want to be deleted. I did tried using > cat del.lines sed '510d;12d;219d;......;3999d' file > source del.lines Word too long. I even tried... (2 Replies)
Discussion started by: novice_man
2 Replies

8. 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

9. Shell Programming and Scripting

Print selection of line based on line number

Hi Unix gurus Basically i am searching for the pattern and getting the line numbers of the grepped pattern. I am trying to print the series of lines from 7 lines before the grepped line number to the grepped line number. I am trying to use the following code. but it is not working. cat... (3 Replies)
Discussion started by: mohanm
3 Replies

10. Shell Programming and Scripting

searching and storing unknown number of lines based on the string with a condition

Dear friends, Please help me to resolve the problem below, I have a file with following content: date of file creation : 12 feb 2007 ==================== = name : suresh = city :mumbai #this is a blank line = date : 1st Nov 2005 ==================== few lines of some text this... (7 Replies)
Discussion started by: swamymns
7 Replies

Featured Tech Videos