LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-06-2007, 01:21 AM   #1
chris_looks
LQ Newbie
 
Registered: Aug 2007
Posts: 5

Rep: Reputation: 0
I am learning sed / grep working and am stuck with a problem


Hi, I recently started learning sed/grep commands; that are required for me to work better..

Now my problemmme is this:-

I have 2 files that have contents in these two formats:-
"From x.rar/Vee/six.jpg" "SM/ded/.." "SMW"
..

and

"From x.rar/Vee/six.jpg" "SM/ded/.."

I want my sed command to delete anything afterthe 1st space i.e.
desired o/p for both the cases is
"From x.rar/Vee/six.jpg"

Can anyone tell me how to do this??

I am a beginner so any help on this would be much appreciated!

Thanks all
Veena
 
Old 08-06-2007, 01:45 AM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
I'm sure someone will jump in and give the simple answer, but you will benefit more if you do these two things.

First, at the command line:

Code:
man sed
Second, give it a shot. If it doesn't work, come back to this thread and show us what you did. You'll learn more this way.
 
Old 08-06-2007, 02:43 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
awk 'BEGIN{FS="\" \""}{print $1"\""}' file
 
Old 08-06-2007, 03:28 AM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Code:
sed -r 's@("[^"]+") .+@\1@' file

Cheers,
Tink
 
Old 08-06-2007, 11:49 AM   #5
chris_looks
LQ Newbie
 
Registered: Aug 2007
Posts: 5

Original Poster
Rep: Reputation: 0
Thank you all;
wjevans_7d1 I just tried what you said it showed me all the options that i can use with sed.
well I started learning by seeing code from my seniors and trying to understand what those did.
The most basic one that i started with was
ls C:/Veen | sed 's+.*+"C:/Veen/&","&" > C:/Veen.txt
Well you might have understood what it does..
Though i still don't understand (.*\) used and \1\2 etc used in sed..
I am trying to Figure that out.
and yes also the answer to my own qs.
But definitely for my sudden req the other posts have helped.

Last edited by chris_looks; 08-06-2007 at 11:51 AM.
 
Old 08-06-2007, 11:57 AM   #6
chris_looks
LQ Newbie
 
Registered: Aug 2007
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks a lot

Last edited by chris_looks; 08-06-2007 at 12:23 PM.
 
Old 08-06-2007, 05:59 PM   #7
Quigi
Member
 
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 31
Quote:
Originally Posted by chris_looks
I want my sed command to delete anything afterthe 1st space
Easy: cut with delimiter space and take the first field:
Code:
cut -d " " -f1
No sed/grep, though.
 
Old 08-06-2007, 07:48 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
And not the result he asked for, either. Your solution would return
Code:
"From
instead of
Code:
"From x.rar/Vee/six.jpg"

Cheers,
Tink
 
Old 08-13-2007, 07:41 PM   #9
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
He implies that what he wants is contained w/in the 1st pair of quotes. If this is true, then cut can work:
Code:
cut -d\" -f2 <file>
Of course, this code does discard the quotes, which may be undesirable.


egrep -o would also work:
Code:
egrep -o '^"[^"]+"' <file>

In addition to the sed man page, I also recommend the ones for grep & regex. Especially regex -- they are the key to it all.
 
Old 08-14-2007, 08:15 AM   #10
chris_looks
LQ Newbie
 
Registered: Aug 2007
Posts: 5

Original Poster
Rep: Reputation: 0
I did not know about regex; I will try that thank you
 
Old 08-14-2007, 08:30 AM   #11
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
"man regex" does not work on this box (PCLOS)
MANY good tutorials out there--I would look at the various docs on http://tldp.org and also http://www.grymoire.com/Unix/
 
Old 08-14-2007, 11:40 AM   #12
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
That's strange, it does on mine: SimplyMEPIS 3.3.2 & 6.0, wonder what it would take to add it. Should someone send texstar a bug report?

I assume that most other man pages are.
 
Old 08-14-2007, 12:13 PM   #13
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
It works here (office--Kubuntu 606)
There is hope......
 
  


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
I am learning sed / grep working and am stuck with a problem chris_looks Linux User Groups (LUG) 2 08-06-2007 02:31 PM
I need some help with SED and/or GREP... TheEngineer Linux - Newbie 1 02-02-2007 09:17 PM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 12:36 PM
sed & grep script? dolvmin Linux - Software 20 09-22-2003 07:30 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:01 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration