LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Other *NIX Forums > Other *NIX
User Name
Password
Other *NIX This forum is for the discussion of any UNIX platform that does not have its own forum. Examples would include HP-UX, IRIX, Darwin, Tru64 and OS X.

Notices


Reply
  Search this Thread
Old 03-11-2011, 12:11 AM   #1
flamingo_l
Member
 
Registered: Jul 2010
Posts: 41

Rep: Reputation: 0
Need solution to solve the formatting issue


Hi All,

I have written a grep command to grep the files containing Windows directories.
I need to output this to a csv.

So here is the code.
Code:
OUTPUT_FILE="Sample-format.txt"
ACT_OUTPUT_FILE="Sample-format.csv"

grep -wHrnI --exclude=*.log '[A-Za-z]:[/\]\{1,2\}[A-Za-z]*[/\]\{1,2\}*' .>$OUTPUT_DIR/$OUTPUT_FILE
paste -s -d"\n" $OUTPUT_DIR/$OUTPUT_FILE|tr ":" "|" >>$OUTPUT_DIR/$ACT_OUTPUT_FILE

Now the problem is, when i do
Code:
tr ":" "|"
, then the search result which contains windows path like for eg: C:\jfj\ff is getting divided into two columns.

Can anybody please give me some inputs to solve this problem.
 
Old 03-11-2011, 12:19 AM   #2
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
It would help to see a sample of the input and the desired output.

EDIT: If you want to keep the : where it is followed by a \ then you could replace the tr statement:

Old code:
Code:
tr ":" "|"
New code:
Code:
sed "s/:\([^\\]\)/|\1/g"
Second Edit: Just curious

Why are you using a Pipe character as your delimiter in a comma seperated values file?

Last edited by Disillusionist; 03-11-2011 at 12:41 AM.
 
1 members found this post helpful.
Old 03-11-2011, 01:07 AM   #3
flamingo_l
Member
 
Registered: Jul 2010
Posts: 41

Original Poster
Rep: Reputation: 0
hi Disillusionist,

Thank you for the reply.
The input are all java files so i can give u the input.
Expected Ouput is:
Quote:
Filename|Line no|String Found
Regardinig your second question,
Am using "|" as a delimiter because my search files can contain ",".

Your given code is working for windows directory containg c:\.. in some files i have directories with c:/...
Also taking a lot of time..
Quote:
sed "s/:\([^\\]\)/|\1/g"
The above code would not replace with | if it encounters a \. and you have put another \ to escape the meaning of \.(Please do correct if my understanging is wrong)

Can you please tell me how to include / as well.??

Also, please tell me the usage of
Quote:
,\1
in sed command.(since am new to sed)

Last edited by flamingo_l; 03-11-2011 at 01:11 AM.
 
Old 03-11-2011, 01:29 AM   #4
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
Just add the / character in the list of characters to ignore following the :


Code:
sed "s/:\([^\\/]\)/|\1/g"
There are a number of elements in play in this sed

:[^\\/] will match the : followed by any character other than a \ or /

Because we want to keep the second character in the output, we need to tag it for use later with \( and \) opening and closing brackets.

the \1 is stating that we want the tagged element (whatever it was) after the | which replaces the :

Example:

echo "this is an example"|sed "s:\([^ ]* [^ ]*\) \([^ ]* [^ ]*\):\2 \1:"

would produce "an example this is"
 
1 members found this post helpful.
Old 03-11-2011, 05:08 AM   #5
flamingo_l
Member
 
Registered: Jul 2010
Posts: 41

Original Poster
Rep: Reputation: 0
Thumbs up

hi Disillusionist,

It solved my problem. Thanka a lot.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to solve the smb link lost issue. ayongying Linux - Newbie 2 03-11-2010 08:39 PM
can anyone help me solve this question by giving me the solution?? please~~~ thx anzdyy Programming 9 09-09-2007 03:55 AM
Two linksys routers - can I make a VLAN or create a similar solution to solve this? Echo Kilo Linux - Networking 2 03-04-2007 12:43 PM

LinuxQuestions.org > Forums > Other *NIX Forums > Other *NIX

All times are GMT -5. The time now is 08:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration