Output formatting of grep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Output formatting of grep
# 1  
Old 07-29-2010
Output formatting of grep

hi,

I am trying to find the word "root" from the follwoing sample file:

Code:
 
#12.12.12.2222
echo "Hai........"
11.11.1.1111
3.23.AS.AA
ab.cd.df.rf
/usr/bin
jhhh 12.12.AF.12
/urf/sss/kk kkk
su sh.s
exec su
root;
,root
sudo -sh  
cosrootex

command used is
Code:
 
 
grep -Hrn '\broot' script1.sh

Output:
Code:
 
 
script1.sh:16:root;
script1.sh:17:,root

Am redirecting the output to a csv file like

Code:
grep -Hrn '\broot' script1.sh|tr ":" "|">out.csv

When i open the .csv file in windows, the second line "root" is appearing in the second column in the excel .

Is there any way to avoid this?
# 2  
Old 07-29-2010
Place the line between quotes, try this:
Code:
grep -Hrn '\broot' script1.sh | awk -F":" '{$0="\"" $0 "\""}$1=$1' OFS="|"

This User Gave Thanks to Franklin52 For This Post:
# 3  
Old 07-30-2010
Thanks Franklin52.. It is working ...

Since am new to UNIX, can you please explain the awk part of the command you have wriiten ... am not getting how you have solved this.
# 4  
Old 07-30-2010
Quote:
Originally Posted by flamingo_l
Thanks Franklin52.. It is working ...

Since am new to UNIX, can you please explain the awk part of the command you have wriiten ... am not getting how you have solved this.
Sure:
Code:
awk -F":" '{$0="\"" $0 "\""}$1=$1' OFS="|"

Set input fieldseparator:
Code:
-F":"

Place quotes around the line:
Code:
{$0="\"" $0 "\""}

Reset the fieldseparator:
Code:
$1=$1

Set output separator:
Code:
OFS="|"

If this is not familiar to you, this could be helpful:

Awk - A Tutorial and Introduction
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Output formatting

Hi , can anyone help me in this Input file Name= XYZ Company= ALPHA City= Stockhomn Equipment=computer Country=Sweden Name=MNC Company=Beta City=Kaulampur Equipment=computer Country=Malaysia Name=JVC Company=gamma City=Kiruna (3 Replies)
Discussion started by: swets
3 Replies

2. Shell Programming and Scripting

Formatting of output

Hi, I have some output in the format below: Col-A Col-B 8781 4319 8781 2332 8781 0269 5550 3282 5550 9465 5550 7607 7064 4456 . . I want to re-format the output so i will get something like this: 8781:4319,2332,0269 5550:3282,9465,7607 7064:4456 for... (6 Replies)
Discussion started by: james2009
6 Replies

3. Shell Programming and Scripting

Formatting grep and awk output

Hi there. I have a very large file and I am trying to format it so that I can pull out certain pieces of data/info and report it in spreadsheet format/style. The file has ###### which will separate each line that will be listed in the spreadsheet. Whenever I find "No" at the end of a line I want... (7 Replies)
Discussion started by: kieranfoley
7 Replies

4. Shell Programming and Scripting

Formatting the Output

Hi, I am trying to use printf command and format certain output in a specific format as under: While the left side (upto |) of the above format is part of a fixed header function, the right side is where i am expecting data to be printed. However, as seen, Row1 value is reflecting on last... (5 Replies)
Discussion started by: EmbedUX
5 Replies

5. Shell Programming and Scripting

Formatting output

I have the output like below: DEV#: 9 DEVICE NAME: hdisk9 TYPE: 1750500 ALGORITHM: Load Balance SERIAL: 68173531021 ========================================================================== Path# Adapter/Path Name State Mode Select Errors 0 ... (4 Replies)
Discussion started by: Daniel Gate
4 Replies

6. Shell Programming and Scripting

Formatting the output

Hi, I have a file which contents entries in this form. Only in /data4/temp abc.000001 Only in /data4/temp abc.000003 Only in /data4/temp abc.000012 Only in /data4/temp abc.000120 Only in /data4/temp abc.000133 Only in /data4/temp abc.001444 i want to read line by line and format... (2 Replies)
Discussion started by: arijitsaha
2 Replies

7. Shell Programming and Scripting

Formatting output

Hi, I have a file like this -------------------------- 1 aaa xxx 55 -------------------------- 1 aaa www 32 -------------------------- 2 bbb yyy 11 -------------------------- 2 bbb zzz 34 ------------------------- 2 bbb ttt ... (3 Replies)
Discussion started by: tdev457
3 Replies

8. UNIX for Dummies Questions & Answers

Formatting df output

Hi all, Can anyone please suggest how best to handle output from running df where some of the information for a volume/filesystem spread over two lines? Some of my volume/filesytem are NFS mounted and when I run a df, the information is spread over the two lines instead of the usual norm... (4 Replies)
Discussion started by: newbie_01
4 Replies

9. Shell Programming and Scripting

Formatting of output

Hi Experts, I have to create a report for certain audit and my output looks as follows I m trying to format my output to look like Any inputs would be highly appreciated Thanks Syed (5 Replies)
Discussion started by: maverick_here
5 Replies

10. Shell Programming and Scripting

Formatting output

Hi, I am new to shell scripting, I ahve written a shell script which would extract me data, but my problem is I want to add a column name to my output. Using grep and sed I am getting my data from the input file. Sample data name : eric name : tom name : sean My output using grep and sed... (3 Replies)
Discussion started by: illur81
3 Replies
Login or Register to Ask a Question