LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-31-2008, 02:02 AM   #1
say_hi_ravi
Member
 
Registered: Jan 2008
Posts: 68

Rep: Reputation: 15
Grep words and paste is on the same line


Hi Guys,

I want to write a program which should grep the strings(words) I desire and paste it horizontally with tab/comma separated.

e.g
$ cat test
JOB_NAME="lb2zerborg-xfer"
ftp-pgp \
--job-name="${JOB_NAME}" \
--debug=0 \
--ftp-passwd-file="${PASSWD_FILE}" \
--notify-success=abc@yahoo.com \
--report-failure=lmn@yahoo.com \
--report-success=xyz@yahoo.com \
--notify-success=ravmane@yahoo.com \



$cat test |grep JOB_MANE > test1
$cat test |grep notify-success >> test1
$cat test|grep report-failure >> test1

but this will print grepped stuff on separate lines
$cat test1
JOB_NAME="lb2zerborg-xfer"
--notify-success=abc@yahoo.com
--report-failure=lmn@yahoo.com

And i want all strings to be on same line. i.e
$cat test1
JOB_NAME="lb2zerborg-xfer",--notify-success=abc@yahoo.com,--report-failure=lmn@yahoo.com OR
JOB_NAME="lb2zerborg-xfer"(tab or space)--notify-success=abc@yahoo.com(tab)--report-failure=lmn@yahoo.com

Any help would be appreciated.

Thanks,
Ravi

Last edited by say_hi_ravi; 10-31-2008 at 02:11 AM.
 
Old 10-31-2008, 02:06 AM   #2
burschik
Member
 
Registered: Jul 2008
Posts: 159

Rep: Reputation: 31
Grep can't do that, since grep can not modify the content of the match to introduce tabs or commas. It would be easy to do with awk, or perl, for example, and possible to do with bash or sed.
 
Old 10-31-2008, 02:54 AM   #3
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,013

Rep: Reputation: 83
It's too early to think further than a bash script, so here it is:

Code:
#!/bin/bash
first_entry=$(cat test |grep JOB_NAME)
second_entry=$(cat test |grep notify-success)
third_entry=$(cat test|grep report-failure)

echo "${first_entry},${second_entry},${third_entry}"
 
Old 10-31-2008, 05:06 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Code:
var=$(egrep "^(JOB_NAME|--notify-success)" file)
 
Old 10-31-2008, 06:07 AM   #5
say_hi_ravi
Member
 
Registered: Jan 2008
Posts: 68

Original Poster
Rep: Reputation: 15
Many thanks Guys. Both of above solutions worked.
However I found one more solution...its bit lengthy compared to your solutions :-)


$cat test |grep JOB_MANE > test1
$cat test |grep notify-success >> test1
$cat test|grep report-failure >> test1
$cat test1|tr -s "\n" "," > test2
 
Old 10-31-2008, 06:26 AM   #6
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Quote:
Originally Posted by say_hi_ravi View Post
Many thanks Guys. Both of above solutions worked.
However I found one more solution...its bit lengthy compared to your solutions :-)


$cat test |grep JOB_MANE > test1
$cat test |grep notify-success >> test1
$cat test|grep report-failure >> test1
$cat test1|tr -s "\n" "," > test2
You can make it slightly less lengthy by eliminating all the useless uses of cat
Code:
$ grep JOB_MANE test > test1
$ grep notify-success test >> test1
$ grep report-failure test >> test1
$ tr -s "\n" "," < test1 > test2
grep will happily read a filename as one of its arguments; tr however, won't, but you can use input redirection (<) to read the file in.

Last edited by pwc101; 10-31-2008 at 06:27 AM.
 
Old 10-31-2008, 06:53 AM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by pwc101 View Post
You can make it slightly less lengthy by eliminating all the useless uses of cat[code]
there's also no need to use grep 3 times pass the same file.
 
Old 10-31-2008, 06:56 AM   #8
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Quote:
Originally Posted by ghostdog74 View Post
there's also no need to use grep 3 times pass the same file.
Yes, that's true, but useless uses of cat are one of my real bugbears, so I just had to point it out!

As you showed, a single grep is all that's needed
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
shell script find a line and the next line (grep?) metalx1000 Programming 5 07-24-2007 08:41 PM
GREP - reg exp to find words ending with .V and .TO vikram_cvk Linux - Server 5 12-22-2006 01:47 PM
Need to strip words from front of line. sed/awk/grep? joadoor Linux - Software 6 08-28-2006 04:39 AM
Can grep filter out words? extrasolar Linux - General 1 07-20-2006 03:14 PM
cut , paste or grep ziox Programming 1 12-15-2004 10:51 PM


All times are GMT -5. The time now is 11:39 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration