sed print range alongwith line numbers.


 
Thread Tools Search this Thread
Operating Systems Solaris sed print range alongwith line numbers.
# 1  
Old 08-01-2011
sed print range alongwith line numbers.

hi working with sed in a shell script
using sed to print a range of lines from a given file
for example to print lines 12-24 from a file
Code:
sed 12,24p <filename>

however i need to print the line numbers, alongwith the actual lines
would this be possible at all?


Thanks

Last edited by radoulov; 08-01-2011 at 11:24 AM.. Reason: Code tags.
# 2  
Old 08-01-2011
I suppose you mean:

Code:
sed -n 12,24p infile

You could print them in separate lines:

Code:
sed -n '2,4{=;p;}' infile

Or use awk:

Code:
awk 'NR == 12, NR == 24 { print NR, $0 }' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check/print missing number in a consecutive range and remove duplicate numbers

Hi, In an ideal scenario, I will have a listing of db transaction log that gets copied to a DR site and if I have them all, they will be numbered consecutively like below. 1_79811_01234567.arc 1_79812_01234567.arc 1_79813_01234567.arc 1_79814_01234567.arc 1_79815_01234567.arc... (3 Replies)
Discussion started by: newbie_01
3 Replies

2. Shell Programming and Scripting

Print range of numbers

Hi I am getting an argument which specifies the range of numbers. eg: 7-15 Is there a way that i can easily (avoiding loop) print the range of number between and including the specified above. The above example should translate to 7,8,9,10,11,12,13,14,15 (3 Replies)
Discussion started by: tostay2003
3 Replies

3. Shell Programming and Scripting

Grep range of lines to print a line number on match

Hi Guru's, I am trying to grep a range of line numbers (based on match) and then look for another match which starts with a special character '$' and print the line number. I have the below code but it is actually printing the line number counting starting from the first line of the range i am... (15 Replies)
Discussion started by: Kevin Tivoli
15 Replies

4. Shell Programming and Scripting

sed command to print first instance of pattern in range

The following text is in testFile.txt: one 5 two 10 three 15 four 20 five 25 six 10 seven 35 eight 10 nine 45 ten 50 I'd like to use sed to print the first occurance of search pattern /10/ in a given range. This command is to be run against large log files, so to optimize efficiency,... (9 Replies)
Discussion started by: uschaafm
9 Replies

5. UNIX for Dummies Questions & Answers

Print the grepped string alongwith the filename

We have C shell and we are executing the below script: #!/bin/csh -f if ($#argv != 2) then echo "Usage $0 DirecotryPath Inputfilename" exit 1 endif set dir=$1 set fname=$2 echo $dir foreach line ( `cat $fname` ) echo \ ======================================== >>... (2 Replies)
Discussion started by: donisback
2 Replies

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

7. Shell Programming and Scripting

print range of lines matching pattern and previous line

Hi all, on Solaris 10, I'd like to print a range of lines starting at pattern but also including the very first line before pattern. the following doesn't print the range starting at pattern and going down to the end of file: cat <my file> | sed -n -e '/<pattern>{x;p;}/' I need to include the... (1 Reply)
Discussion started by: siriche
1 Replies

8. Shell Programming and Scripting

How to print range of a line?

Hi , I have a file content like following. In few lines I have fields with '"' and in few not. "Country" character varying(150), "Region" character varying(60), "Total Page Requests" numeric, "Pages Served" numeric, "Ad request" numeric, Ads Served ... (5 Replies)
Discussion started by: sussus2326
5 Replies

9. Shell Programming and Scripting

read numbers from file and output which numbers belongs to which range

Howdy experts, We have some ranges of number which belongs to particual group as below. GroupNo StartRange EndRange Group0125 935300 935399 Group2006 935400 935476 937430 937459 Group0324 935477 935549 ... (6 Replies)
Discussion started by: thepurple
6 Replies

10. Shell Programming and Scripting

SED: Print range, exclude regular expressions.

Ok, so I get that: sed -n '/START/,/END/p' file ...will print every line from START to END inclusive, but I don't want to see START or END. Apart from the obious: sed -n '/START/,/END/p' file | grep -v "START" | grep -v "END" ...is there a simpler way of doing this? Thanks as always! (2 Replies)
Discussion started by: cs03dmj
2 Replies
Login or Register to Ask a Question