How to get next 4 lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get next 4 lines
# 1  
Old 08-08-2008
MySQL How to get next 4 lines

Hi Frds,
I am new to scripting.

My Doubt is how to get the next 4 lines If I already know the 1st line.
That means,
bash-2.03$ grep -i "abc" /tmp/us1.txt
abc : 904015467

I will get the above result if I grep for the "abc" in my file.
but I am getting only "abc" content line.But I need next 5 lines after that.
That means if the abc : 904015467 is there in the 2nd line,I need to get 3rd to 8th line.

Could anybody help on this plz.

Thanks&Regards,
Anji
# 2  
Old 08-08-2008
Hmm... you need to write few lines (if not a script) for this.. Some thing like this..

Code:
 n=0
  n=`grep -n 'abc' $file_name | head -1| cut -d: -f1`
  ((n=n+4))
  head -$n $file_name | tail -4

Jope this works for you.

Last edited by guruparan18; 08-08-2008 at 09:40 AM.. Reason: Missed the line n=n+4 [:(] removed '/'
# 3  
Old 08-08-2008
Quote:
Originally Posted by Anji
Hi Frds,
I am new to scripting.

My Doubt is how to get the next 4 lines If I already know the 1st line.
That means,
bash-2.03$ grep -i "abc" /tmp/us1.txt
abc : 904015467
this may work:

Code:
grep -A 5 -i "abc" /tmp/us1.txt

or -A 4 if that is what You want
# 4  
Old 08-08-2008
@OP Please post sample data and requested output.
# 5  
Old 08-09-2008
Code:
$ yes junk | nl -s: | sed 15q > data
$ cat data
     1:junk
     2:junk
     3:junk
     4:junk
     5:junk
     6:junk
     7:junk
     8:junk
     9:junk
    10:junk
    11:junk
    12:junk
    13:junk
    14:junk
    15:junk
$ sed -n '/ 6:/{n;N;N;N;p;}' data
     7:junk
     8:junk
     9:junk
    10:junk
$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to print lines from a files with specific start and end patterns and pick only the last lines?

Hi, I need to print lines which are matching with start pattern "SELECT" and END PATTERN ";" and only select the last "select" statement including the ";" . I have attached sample input file and the desired input should be as: INPUT FORMAT: SELECT ABCD, DEFGH, DFGHJ, JKLMN, AXCVB,... (5 Replies)
Discussion started by: nani2019
5 Replies

2. Shell Programming and Scripting

Merging multiple lines to columns with awk, while inserting commas for missing lines

Hello all, I have a large csv file where there are four types of rows I need to merge into one row per person, where there is a column for each possible code / type of row, even if that code/row isn't there for that person. In the csv, a person may be listed from one to four times... (9 Replies)
Discussion started by: RalphNY
9 Replies

3. UNIX for Dummies Questions & Answers

awk - (URGENT!) Print lines sort and move lines if match found

URGENT HELP IS NEEDED!! I am looking to move matching lines (01 - 07) from File1 and 77 tab the matching string from File2, to File3.txt. I am almost done but - Currently, script is not printing lines to File3.txt in order. - Also the matching lines are not moving out of File1.txt ... (1 Reply)
Discussion started by: High-T
1 Replies

4. Shell Programming and Scripting

ksh sed - Extract specific lines with mulitple occurance of interesting lines

Data file example I look for primary and * to isolate the interesting slot number. slot=`sed '/^primary$/,/\*/!d' filename | tail -1 | sed s'/*//' | awk '{print $1" "$2}'` Now I want to get the Touch line for only the associate slot number, in this case, because the asterisk... (2 Replies)
Discussion started by: popeye
2 Replies

5. UNIX for Advanced & Expert Users

SQL script with 86000 lines: new files with only 10000 lines (per file)

Hi this is my SQL script $ wc -l insert_into_customers.sql 85601 insert_into_customers.sqlI wish to cut this file into 9 files each 10000 lines (the last one less) $ wc -l insert_into_customers_00*.sql 10000 insert_into_customers_001.sql 10000 insert_into_customers_002.sql ... (1 Reply)
Discussion started by: slashdotweenie
1 Replies

6. Shell Programming and Scripting

Summing over specific lines and replacing the lines with the sum using sed, awk

Hi friends, This is sed & awk type question. I have a text file which has numbers spread all over the file. I want to sum the series of numbers whenever i find it and produce an output file with the sum. For example ###start of input text file #### abc def ghi 1 2 3 4 kjld random... (3 Replies)
Discussion started by: kaaliakahn
3 Replies

7. Shell Programming and Scripting

search and replace, when found, delete multiple lines, add new set of lines?

hey guys, I tried searching but most 'search and replace' questions are related to one liners. Say I have a file to be replaced that has the following: $ cat testing.txt TESTING AAA BBB CCC DDD EEE FFF GGG HHH ENDTESTING This is the input file: (3 Replies)
Discussion started by: DeuceLee
3 Replies

8. Shell Programming and Scripting

merging two .txt files by alternating x lines from file 1 and y lines from file2

Hi everyone, I have two files (A and B) and want to combine them to one by always taking 10 rows from file A and subsequently 6 lines from file B. This process shall be repeated 40 times (file A = 400 lines; file B = 240 lines). Does anybody have an idea how to do that using perl, awk or sed?... (6 Replies)
Discussion started by: ink_LE
6 Replies

9. Shell Programming and Scripting

Perl XML, find matching condition and grep lines and put the lines somewhere else

Hi, my xml files looks something like this <Instance Name="New York"> <Description></Description> <Instance Name="A"> <Description></Description> <PropertyValue Key="false" Name="Building A" /> </Instance> <Instance Name="B"> ... (4 Replies)
Discussion started by: tententen
4 Replies

10. UNIX for Dummies Questions & Answers

How to count lines - ignoring blank lines and commented lines

What is the command to count lines in a files, but ignore blank lines and commented lines? I have a file with 4 sections in it, and I want each section to be counted, not including the blank lines and comments... and then totalled at the end. Here is an example of what I would like my... (6 Replies)
Discussion started by: kthatch
6 Replies
Login or Register to Ask a Question