LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-16-2010, 08:08 AM   #1
Thakowbbery
Member
 
Registered: Mar 2005
Posts: 138

Rep: Reputation: 17
Use of "sed" command


Greetings,

I'm working on a script to filter the output of a file.
If I explain the whole thing is gonna take a while, but to sum up:

I have a .csv file with lots of lines, with the following format:

username,someotherinfo

I need to prepare a script so:

1) The content of 'cat file.csv' goes to a variable (DONE)
2) Only the "someotherinfo" from the "cat" command above goes inside a second variable (NOT DONE)

For that I was wondering if I could use "sed", but I have no idea how to use it.

Any ideas?

Thank you very much
 
Old 11-16-2010, 08:15 AM   #2
BenCollver
Rogue Class
 
Registered: Sep 2006
Location: OR, USA
Distribution: Slackware64-15.0
Posts: 375
Blog Entries: 2

Rep: Reputation: 172Reputation: 172
Yes, it is possible with the sed command.
Code:
someotherinfo=$(echo "$contents" | sed 's/.*,//')
It would also be possible to use the cut command.
Code:
someotherinfo=$(echo "$contents" | cut -d , -f 2)
Neither of these will work with data that has commas within quotes. For that there is Perl's Text::CSV.
 
1 members found this post helpful.
Old 11-16-2010, 08:18 AM   #3
Thakowbbery
Member
 
Registered: Mar 2005
Posts: 138

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by BenCollver2 View Post
Yes, it is possible with the sed command.
Code:
someotherinfo=$(echo "$contents" | sed 's/.*,//')
It would also be possible to use the cut command.
Code:
someotherinfo=$(echo "$contents" | cut -d , -f 2)
Neither of these will work with data that has commas within quotes. For that there is Perl's Text::CSV.
That worked, thank you very much.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bad: Want to insert a line into a text file using "sed" command eliote Linux - General 7 09-19-2010 02:55 AM
sed - use sed to replace multiple instances from "x" to "y" in a line mrodmac Linux - General 4 02-02-2010 11:37 AM
sed error "command c expects \ followed by text" under OS X (but works in Linux) srunni Programming 3 12-02-2009 02:52 AM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
bash Problem with "sed" command blubbfish Linux - Newbie 3 06-01-2004 10:14 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:30 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