print multiple lines using the grep command.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting print multiple lines using the grep command.
# 8  
Old 08-26-2009
[aaaa@bbbb] $ cat a.txt
ddddddddddddddddd ffffffffffffffffffffffffff dddddddddddddddddddddddddddd ssssssssssssssssssssssssssss ggggggggggggggggggggggggg dddddddddddddddddddddd fffffffffffffffffffffffff ddddddddddddddddddddddddddd sssssssssssssssssssssssss fffffffffffffffffffffffff ssssssssssssssssssssssssssss ffffffffffffffffffff vvvvvvvvvvvvvvvvvvvvvvvvvvvv xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb ssssssssssssssssssssssssssssssssss dddddddddddddddddddddddddddddddddddddd eeeeeeeeeeeeeeeeeeeeeeeeee ccccccccccccccccccccccc



b=`grep f a.txt`
start_pos=0
increment=100
counter=1
while(true)
do
print_line=`echo $b | cut -c$start_pos-$increment`
start_pos=`expr $start_pos + 100`
increment=`expr $start_pos + 100`
if [ -z "$print_line" ]
then
break
fi
echo $counter " " $print_line
counter=`expr $counter + 1`
done




O/p:


1 ddddddddddddddddd ffffffffffffffffffffffffff dddddddddddddddddddddddddddd ssssssssssssssssssssssssss
2 sss ggggggggggggggggggggggggg dddddddddddddddddddddd fffffffffffffffffffffffff dddddddddddddddddddddd
3 dddddd sssssssssssssssssssssssss fffffffffffffffffffffffff ssssssssssssssssssssssssssss fffffffffffff
4 ffffffff vvvvvvvvvvvvvvvvvvvvvvvvvvvv xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
5 b ssssssssssssssssssssssssssssssssss dddddddddddddddddddddddddddddddddddddd eeeeeeeeeeeeeeeeeeeeeeeee
6 ee ccccccccccccccccccccccc

---------- Post updated at 07:27 AM ---------- Previous update was at 07:21 AM ----------

If you want complete words, you can use the field separator:

b=`grep f a.txt`
start_pos=1
increment=4
counter=1
while(true)
do
print_line=`echo $b | cut -d " " -f$start_pos-$increment`
start_pos=`expr $start_pos + 4`
increment=`expr $start_pos + 4`
if [ -z "$print_line" ]
then
break
fi
echo $counter " " $print_line
counter=`expr $counter + 1`
done
# 9  
Old 08-26-2009
The code work perfect in hp-ux with sh.
I'm not sure if i undertand you, if you put and example think is better.
This is what i'm think is you problem:
Code:
Script started on Wed Aug 26 15:23:51 2009
$more file
Koption1 Value1
option2 Value2
option3 Value3
option4 Value4
$grep option file
option1 Value1
option2 Value2
option3 Value3
option4 Value4
$buffer=`grep option file`
$echo $buffer
option1 Value1 option2 Value2 option3 Value3 option4 Value4
$ls new_file
new_file no encontrado
$buffer2=`grep option file|tee -a new_file`
$echo $buffer2
option1 Value1 option2 Value2 option3 Value3 option4 Value4
$more new_file
option1 Value1
option2 Value2
option3 Value3
option4 Value4

script done on Wed Aug 26 15:25:50 2009

the problem is the echo of variable is in one line and you want the 4lines?


Quote:
Originally Posted by intiraju
Hi The above code dint work.

Please see my output below. The SUCCESS word has come twice in the log file. but when i am trying to mail that logfile, its coming in one line.
How to display it in two lines.

Checking the validity for Affinity daily backup on Wed Aug 26 07:43:25 EDT 2009
-------------------------------------------------------------------------------
SUCCESS- with the start time of the Paris daily backup box SUCCESS- with the Paris split date
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parsing OSX UNIX command results which print in multiple lines

from the CLI on a Mac, if you type networksetup -listallnetworkservices then you get results in a multi-line paragraph that look something like this: networksetup -listallnetworkservices An asterisk (*) denotes that a network service is disabled. Wi-Fi Display Ethernet Bluetooth DUN... (7 Replies)
Discussion started by: hungryd
7 Replies

2. AIX

Print output of grep command in multuple lines

HI All, I am using grep command to serach a pattern in a list of files and storing the output in a variable. Then i am applying some logic on that variable to get the required output. But Thing is that when the pattern is present mutiple times in a file, i am getting the output of grep in a... (3 Replies)
Discussion started by: goutam sahoo
3 Replies

3. Shell Programming and Scripting

Print lines between two strings multiple occurencies (with sed, awk, or grep)

Hello, I can extract lines in a file, between two strings but only one time. If there are multiple occurencies, my command show only one block. Example, monfichier.txt contains : debut_sect texte L1 texte L2 texte L3 texte L4 fin_sect donnees inutiles 1 donnees inutiles 2 ... (8 Replies)
Discussion started by: theclem35
8 Replies

4. Shell Programming and Scripting

Grep and print next 10 Lines separated by ,

Hi All, I need to grep through a file for a string and print the next ten lines to a file separating the lines with a , and save it as a csv file to open it as a XL file. The 10 lines should be on a sigle row in xl. Any suggesstions please. Note; I dont have a GNU Grep to use -A flag. ... (6 Replies)
Discussion started by: Nani369
6 Replies

5. Shell Programming and Scripting

Print lines before and after..not grep -A

Hi I have this in my file 2011-04-18 15:32:11 system-alert-00012: UDP flood! From xxxxxx to yyyyyyyyyy, int ethernet0/2). Occurred 1 times. 2011-04-18 15:32:11 system-alert-00012: UDP flood! From xxxxxx to yyyyyyyyyy, int ethernet0/2). Occurred 1 times. 2011-04-18 15:32:11... (9 Replies)
Discussion started by: zorrox
9 Replies

6. UNIX for Dummies Questions & Answers

grep command to find multiple strings in multiple lines in a file.

I want to search files (basically .cc files) in /xx folder and subfolders. Those files (*.cc files) must contain #include "header.h" AND x() function. I am writing it another way to make it clear, I wanna list of *.cc files that have 'header.h' & 'x()'. They must have two strings, header.h... (2 Replies)
Discussion started by: ritikaSharma
2 Replies

7. Shell Programming and Scripting

Print lines between two lines after grep for a text string

I have several very large file that are extracts from Oracle tables. These files are formatted in XML type syntax with multiple entries like: <ROW> some information more information </ROW> I want to grep for some words, then print all lines between <ROW> AND </ROW>. Can this be done with AWK?... (7 Replies)
Discussion started by: jbruce
7 Replies

8. Shell Programming and Scripting

AIX equivalent to GNU grep's -B and -A [print lines after or before matching lines]

Hi folks I am not allowed to install GNU grep on AIX. Here my code excerpt: grep_fatal () { /usr/sfw/bin/gegrep -B4 -A2 "FATAL|QUEUE|SIGHUP" } Howto the same on AIX based machine? from manual GNU grep ‘--after-context=num’ Print num lines of trailing context after... (4 Replies)
Discussion started by: slashdotweenie
4 Replies

9. Shell Programming and Scripting

Print lines after grep

Hi all, I need help in following scenario. I have a file with about 10,000 lines. There are several lines which have word "START" (all upper case) in them. I want to grep line with word "START" and then do the following 1. Print the line number having word "START" 2. Print the next 11 lines. ... (4 Replies)
Discussion started by: jakSun8
4 Replies
Login or Register to Ask a Question